Contents

Dockerfile recipe

Dockerfile

Dockerfile:

1
2
FROM ubuntu:latest
RUN touch test

Dockerfile is good to maintenance the build image (rather than docker commit by a container)

Build docker

Build docker: on the directory for the project (with Dockerfile),

1
docker build .
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
% docker build .
[+] Building 0.7s (6/6) FINISHED                                                                                                                                                    
 => [internal] load build definition from Dockerfile                                                                                                                           0.1s
 => => transferring dockerfile: 75B                                                                                                                                            0.0s
 => [internal] load .dockerignore                                                                                                                                              0.0s
 => => transferring context: 2B                                                                                                                                                0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                                                                               0.0s
 => [1/2] FROM docker.io/library/ubuntu:latest                                                                                                                                 0.0s
 => [2/2] RUN touch test                                                                                                                                                       0.4s
 => exporting to image                                                                                                                                                         0.0s
 => => exporting layers                                                                                                                                                        0.0s
 => => writing image sha256:d6d8116cf6839173dab14b9a5b0bca89ba55230def590630920d41f453da5c40     

Filter dangling image (With “none”)

1
2
3
$ docker images -f dangling=true
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
<none>       <none>    d6d8116cf683   50 seconds ago   69.2MB

Build docker image with name

1
2
3
4
5
$ docker build -t new-ubuntu:latest .

$ docker images
REPOSITORY                       TAG         IMAGE ID       CREATED         SIZE
new-ubuntu                       latest      2b9384f7377a   4 seconds ago   69.2MB

Dockerfile instruction

FROM, RUN, CMD

  • FROM: set the image
  • RUN : set commands to run and create image layer
    • one RUN creates one layer
  • CMD : set the default command of the container
    • CMD [“executable”, “param1”, “param2”]
    • CMD does NOT create a layer

Dockerfile:

1
2
3
4
5
6
FROM ubuntu:latest
RUN apt-get update && apt-get install -y \
    curl \
    nginx \
    cvs
CMD ["/bin/bash"]

Build a container

1
dockker build .

Run container

1
docker run 8bedd9090ee8

Best practice

  • Minimize a base image (by FROM)
  • Minimize the number of layer (one RUN command aligns to one layer)
    • 3 layer : RUN, COPY,ADD
    • connect commands by “&&”
    • line feed by \
  • Utilize cash effectively
    • If already built (RUN command in the docker file), that layer is cashed.
    • When creating the docker image, split RUN command for using cash (step by step)
    • After completion in docker image building, unify the RUN command at one line.

COPY

1
COPY <src> <dest>

Dockerfile:

1
2
3
FROM ubuntu:latest
RUN mkdir /new_dir
COPY something /new_dir/

Build and run a container

1
2
3
4
$ docker build .
$ docker run -it --rm 8efaf4e1470341 bash
root@46719dea9e21:/# ls /new_dir/
something

ADD

ADD allows compress large file to .tar file and decompress it on container

1
ADD <src> <dest>

Dockerfile:

1
2
FROM ubuntu:latest
ADD compressed.tar /

Build and run a container. Differently from COPY, ADD decompress the tar file on the container.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ docker build .
$ docker run -it --rm 1d6c0226b339925 bash 

root@81c520e35e5b:/# ls
bin  boot  dev  etc  home  lib  media  mnt  opt  proc  root  run  sample_folder  sbin  srv  sys  tmp  usr  var

root@81c520e35e5b:/# cd sample_folder/

root@81c520e35e5b:/sample_folder# ls
hello

root@81c520e35e5b:/sample_folder# cat hello 
hello

docker build -f “dockerfile”

Build a container with a Dockerfile which does not exist in the build context

1
docker build -f ../Dockerfile.dev .

ENTRYPOINT

ENTRYPOINT is used when you want to use a container as a command:

  • cannot be updated on docker run
  • CMD gets to a list of command parameter; [“param1”, “param2”]
  • CMD can be updated on docker run

Dockerfile:

1
2
3
4
FROM ubuntu:latest
RUN touch test
ENTRYPOINT ["ls"]
CMD ["--help"]
1
2
3
4
5
6
$ docker build . 
$ docker run --rm cfb4cd70ef26757fe10d
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
....

When updating a parameter,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ docker run --rm cfb4cd70ef26757fe10d -1
bin
boot
dev
etc
home
lib
media
mnt
opt
proc
root
run
sbin
srv
sys
test
tmp
usr
var

ENV

Set environment variables on a container

1
2
ENV <key> <value>
ENV <key>=<value>

Dockerfile:

1
2
3
4
FROM ubuntu:latest
ENV key1 test1
ENV key2=test2
ENV key3=t\ e\ s\ t
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
docker build .
docker run -it --rm f3df2f70c5b3 bash

root@dd101e9cbc8f:/# env
HOSTNAME=dd101e9cbc8f
...
key2=test2
key3=t e s t
key1=test1
...

WORKDIR

In a dfault, RUN command works on root (/) directory in a container.

1
WORKDIR <path>

Dockerfile:

1
2
3
4
FROM ubuntu:latest
RUN mkdir sample_folder
WORKDIR /sample_folder
RUN touch sample_file

Run a container

1
2
3
% docker run -it --rm e9466222b0bf8e5 bash
root@7d258146df98:/sample_folder# ls
sample_file