Getting to Know Dockerfile Instructions: Part 3

Dockerfile RUN

RUN has 2 forms:

RUN (shell form, the command is run in a shell, which by default is /bin/sh -c)

RUN “executable”, “param1”, “param2”

RUN mkdir ...
RUN useradd ..
RUN apt install ...
RUN copy ...
RUN unzip ...
nano Dockerfile
FROM alpine:3.8
RUN echo $HOME
RUN ["echo", "$HOME"]
docker build --no-cache --tag tutorial:demo --file Dockerfile  .
Sending build context to Docker daemon    258kB
Step 1/3 : FROM alpine:3.8
---> 196d12cf6ab1
Step 2/3 : RUN echo $HOME
---> Running in f9aed6be9bb4
/root
Removing intermediate container f9aed6be9bb4
---> dd939eaa9e43
Step 3/3 : RUN ["echo", "$HOME"]
---> Running in 38e244f781bc
$HOME
Removing intermediate container 38e244f781bc
---> 3872c058a5bc
Successfully built 3872c058a5bc
Successfully tagged tutorial:demo

Dockerfile CMD

The CMD instruction has three forms:

The exec form is parsed as a JSON array, which means that you must use double-quotes (“) around words not single-quotes (‘).

FROM alpine:3.8
CMD ["/bin/echo","hello from CMD - using exec form"]
docker build --tag tutorial:demo --file Dockerfile  .
docker run --name tutorial tutorial:demo
hello from CMD - using exec form
FROM alpine:3.8
CMD /bin/echo "hello from CMD using shell form"
docker build --tag tutorial:demo --file Dockerfile  .
docker stop -t 0 tutorial ; docker container prune -f;docker ps -a
docker run --name tutorial tutorial:demo
hello from CMD using shell form
nano Dockerfile
FROM alpine:3.8
CMD /bin/echo $HOME
docker build --tag tutorial:demo --file Dockerfile  .
docker stop -t 0 tutorial ; docker container prune -f;docker ps -a
docker run --name tutorial tutorial:demo
/root
docker run --name tutorial tutorial:demo
docker stop -t 0 tutorial ; docker container prune -f;docker ps -a
docker run -ti -d --name tutorial tutorial:demo /bin/sh -c 'echo ECHO text from run command'
ECHO text from run command

Dockerfile ENTRYPOINT

ENTRYPOINT has two forms:

An ENTRYPOINT allows you to configure a container that will run as an executable.

For example, the following will start nginx with its default content, listening on port 80:

docker run -i -t — rm -p 80:80 nginx

FROM alpine:3.8
ENTRYPOINT /bin/echo ECHO from ENTRYPOINT - shell form
docker build --tag tutorial:demo --file Dockerfile  .
docker stop -t 0 tutorial ; docker container prune -f;docker ps -a
docker run --rm --name tutorial tutorial:demo
docker run --rm --name tutorial tutorial:demo
ECHO from ENTRYPOINT - shell form
docker run --rm --name tutorial tutorial:demo
FROM alpine:3.8
ENTRYPOINT ["/bin/echo", "ECHO from ENTRYPOINT - exec form"]
docker build --tag tutorial:demo --file Dockerfile  .
docker run --rm --name tutorial tutorial:demo
ECHO from ENTRYPOINT - exec form

Dockerfile VOLUME

The VOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers.

docker volume list
FROM alpine:3.8
RUN mkdir /myvol
RUN echo "hello world" > /myvol/greeting
VOLUME /myvol
docker build --tag tutorial:demo --file Dockerfile  .
docker stop -t 0 tutorial ; docker container prune -f;docker ps -a
docker run --rm -ti -d --name tutorial tutorial:demo /bin/sh -c 'while true; do sleep 60; done'
docker exec -it tutorial /bin/sh
hello world
docker volume list
DRIVER              VOLUME NAME
local de82d92daf539b7147770877704ed438b053c50744e874e7a6b86655cd50cf44
docker volume inspect de82d92daf539b7147770877704ed438b053c50744e874e7a6b86655cd50cf44
[
{
"CreatedAt": "2018-10-23T15:19:14+02:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/de82d92daf539b7147770877704ed438b053c50744e874e7a6b86655cd50cf44/_data",
"Name": "de82d92daf539b7147770877704ed438b053c50744e874e7a6b86655cd50cf44",
"Options": null,
"Scope": "local"
}
]
ls /var/lib/docker/volumes/de82d92daf539b7147770877704ed438b053c50744e874e7a6b86655cd50cf44/_data/
docker stop -t 0 tutorial ; docker container prune -f;docker ps -a
docker volume list

More About anon Volumes

docker run -ti -d --name tutorial tutorial:demo /bin/sh -c 'while true; do sleep 60; done'
docker run -d --name alpine1 -v /myvol alpine:3.8  /bin/sh -c 'while true; do sleep 60; done'
docker run -d --name alpine2 -v /myvol alpine:3.8 /bin/sh -c 'while true; do sleep 60; done'
docker run -d --name alpine3 -v /myvol alpine:3.8 /bin/sh -c 'while true; do sleep 60; done'
docker volume list
Expected output :  DRIVER              VOLUME NAME
local 0a43eb2703d4a1638e1a466a9cbae5aba1071939a3b047dc7c27d383b83cebea
local 8edd9ff9f14a6038df3013ea7b581aeda41e6045584b6c350ba4b20e60e6f6a2
local b02d0f7b3c5be418718b1732be0e46c735806b22791dd70fa44036bb2ffa520e
local f5a2bb741141616255333c54c421926a8a06ac59f5b054e4619a1dfef4da2fbc
docker stop -t 0 tutorial
docker stop -t 0 alpine1
docker stop -t 0 alpine2
docker stop -t 0 alpine3
docker container prune -f;docker ps -a
docker volume list
docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
51742ef4c728f01870e49edcf29ffa0407eee3363c21fd90493a9c786b36f430
0670752370af7bc92e4fbf1ce513dc8b5472169c4d7e8023e6dc207c510a023f
0c797c8a480e12c450870a78a3cc3de3c22e483162882e1972ed399148e3c00e
0e36fa721df75a9bbcf16fbe2aa809845aee016f7f00c2c6bebefbc27b2832c0
Total reclaimed space: 11B

Dockerfile LABEL

nano Dockerfile
FROM alpine:3.8
LABEL version="demo label"
LABEL description="This text illustrates \
that label values can span multiple lines."
CMD echo "container with labels"
docker build --tag tutorial:demo --file Dockerfile  .
docker inspect tutorial:demo
"Labels": {
"description": "This text illustrates that label values can span multiple lines.",
"version": "demo label"
}
docker images --filter "label=version=demo label"
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tutorial demo 16d6a13eb022 3 minutes ago 4.41MB

Dockerfile ARG

The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the — build-arg = flag.

nano Dockerfile
FROM alpine:3.8
ARG target_dir=dir1
WORKDIR /root/${target_dir}
RUN pwd
RUN echo ${target_dir}
COPY gamefile /root/${target_dir}
COPY anothergame /root/${target_dir}
docker build --tag tutorial:demo --file Dockerfile  .
Sending build context to Docker daemon    258kB
Step 1/7 : FROM alpine:3.8
---> 196d12cf6ab1
Step 2/7 : ** ARG target_dir=dir1 88
---> Running in 15ef296ddf95
Removing intermediate container 15ef296ddf95
---> fe6be5de15bb
Step 3/7 : ** WORKDIR /root/${target_dir} 88
---> Running in a64dbe343a98
Removing intermediate container a64dbe343a98
---> e41509ae6f7b
Step 4/7 : ** RUN pwd 88
---> Running in 3b886f0637b4
88 /root/dir1 88
Removing intermediate container 3b886f0637b4
---> 08468a2fec81
Step 5/7 : ** RUN echo ${target_dir} 88
---> Running in a6a8a8be8252
88 dir1 88
Removing intermediate container a6a8a8be8252
---> 48a9ffe90e28
Step 6/7 : COPY gamefile /root/${target_dir}
as ---> a09e827ec1c7
Step 7/7 : COPY anothergame /root/${target_dir}
---> d14edc1f440d
Successfully built d14edc1f440d
Successfully tagged tutorial:demo

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store