loading...

Learning Docker 002 ~Images~

kojikanao profile image Koji (he/him) ・3 min read

Super Simple Dockerfile

busybox
https://hub.docker.com/_/busybox

FROM busybox
RUN echo "building simple docker image!!!"
CMD echo "hello container"

build

$ docker build -t hello .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox
latest: Pulling from library/busybox
8e674ad76dce: Pull complete
Digest: sha256:c94cf1b87ccb80f2e6414ef913c748b105060debda482058d2b8d0fce39f11b9
Status: Downloaded newer image for busybox:latest
 ---> e4db68de4ff2
Step 2/3 : RUN echo "building simple docker image!!!"
 ---> Running in 1df5fbc4c964
building simple docker image!!!
Removing intermediate container 1df5fbc4c964
 ---> 1ee1020b4fdc
Step 3/3 : CMD echo "hello container"
 ---> Running in ac28127a38e0
Removing intermediate container ac28127a38e0
 ---> 6a252581664b
Successfully built 6a252581664b
Successfully tagged hello:latest

Images

$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
hello                   latest              6a252581664b        25 seconds ago      1.22MB

Run Container

$ docker run --rm hello
hello container

Simple Ubuntu Image

  1. install vim
  2. install git
  3. install curl
  4. show git version when run a container

Dockerfile

FROM ubuntu:18.04
RUN apt-get update && apt-get upgrade -y && apt-get autoremove -y
RUN apt-get install vim -y
RUN apt-get install git -y
RUN apt-get install curl -y
CMD [ "git", "--version"]
$ docker build -t my_test .

$ docker run --rm -it my_test
git version 2.17.1

Way to Create a Good Dockerfile (for beginner)

I think the followings could be a safe way

  1. Choose the base image
  2. Run command on the container
  3. Write down all commands which used in step2
  4. Make a Dockerfile if done step3

Step 1

We can find out good images https://hub.docker.com/

Step 2

Run commands on the container. We need to use -y when we try commands

Step 3

If the command runs successfully, take a note and at some point, we need to commit since if one of the commands messes up what we have done, we will need to the same things again which is painful.
Or we can create an image then add more commands.

Step 4

Write down all commands which tested and modify Dockerfile

What I built

base continuumio/anaconda3
install some software & python packages

Dockerfile

# base image
FROM continuumio/anaconda3:2019.03
MAINTAINER Koji Kanao <koji.kanao@nyu.edu>

RUN apt-get update && apt-get upgrade -y && apt-get autoremove -y
RUN apt-get install vim -y
RUN apt-get install git -y
RUN apt-get install curl -y
RUN apt-get install ffmpeg -y
RUN apt-get install imagemagick -y
RUN conda install pytorch torchvision cudatoolkit=9.0 -c pytorch -y

# python packages
RUN pip install --upgrade \
    pip==19.1.1 \
    && pip install \
    autopep8==1.4.4 \
    setuptools>=41.0.0 \
    opencv-python==3.4.3.18 \
    opencv-contrib-python==3.4.3.18 \
    Pillow \
    numpy \
    pandas \
    scipy \
    matplotlib \
    h5py \
    Keras==2.2.4 \
    scikit-learn \
    scikit-image \
    six \
    sklearn \
    spacy \
    requests \
    beautifulsoup4 \
    tensorflow==1.14.0 

# create work dir
WORKDIR /workdir

# expose port
EXPOSE 8888

# start jupyter notebook
ENTRYPOINT ["jupyter-lab", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root", "--NotebookApp.token=''"]

# set workdir as notbook dir
CMD ["--notebook-dir=/workdir"]

Build Image

$ docker build -t conda_docker .

Run Container

$ docker run -it -p 8888:8888 --name doconda --mount type=bind,src=`pwd`,dst=/workdir  conda_docker

open localhost:8888
jupyter

$ ls
Dockerfile     Untitled.ipynb

This container can save a notebook on my host which is my Mac.

You can pull this image from here
https://hub.docker.com/r/kojikno/conda_docker

I think Docker is great since I take a couple of hours to create this env with VirtualBox. Actually we can use vagrant to save time, but it still uses a virtual machine, so generally so heavy and Mac's fan will need to work very hard lol. However, Docker isn't so heavy and easy to recreate the same env which is very nice. I think I will use Docker more, but still need to keep learning. Especially docker-compose

Posted on by:

kojikanao profile

Koji (he/him)

@kojikanao

Looking for opportunities! Software Engineer in NYC/Research Resident@NYU-ITP18-19 Mostly I work with reactjs and sometimes python. code to do something weird/useless/helpful Japanese

Discussion

markdown guide