Dockerfile - Label, Env, Run and Workdir

Label

Label keyword is used to add metadata to docker objects such as images, containers, local daemons, volumes, networks, swarm nodes, and swarm services.

Labels are key-value pairs stored as a string that can be used to organize your images, add licensing information, etc. We can add multiple labels to a docker object.

Let's see how it works.

gaurav@learning-ocean:~/dockerfiles$ cat Dockerfile
FROM ubuntu:14.04
LABEL name="learning-ocean"
LABEL email="[email protected]"
gaurav@learning-ocean:~/dockerfiles$

We have added two labels here one as name and the other as email. Now let's build the image.

gaurav@learning-ocean:~/dockerfiles$ docker image build -t myubuntu:10 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM ubuntu:14.04
 ---> 13b66b487594
Step 2/3 : LABEL name="learning-ocean"
 ---> Running in 7b36fa89fb95
Removing intermediate container 7b36fa89fb95
 ---> 734daaecd75f
Step 3/3 : LABEL email="[email protected]"
 ---> Running in e13b20d9b9ba
Removing intermediate container e13b20d9b9ba
 ---> 7a58826c0a98
Successfully built 7a58826c0a98
Successfully tagged myubuntu:10
gaurav@learning-ocean:~/dockerfiles$

The image is created. To check if the labels are added or not, we will use the following command:

gaurav@learning-ocean:~/dockerfiles$ docker image inspect myubuntu:10 | less

We can see the labels are added in the output of the command

output:

  "Labels": {
                "email": "[email protected]",
                "name": "learning-ocean"
            }

ENV

The ENV instruction is used to provide environment variables to an image which are persisted when a container is run.

Let's see how we can use it.

Below is the dockerfile with environment variables Name and Pass.

gaurav@learning-ocean:~/dockerfiles$ cat Dockerfile
FROM ubuntu:14.04
LABEL name="learning-ocean"
LABEL email="[email protected]"
ENV NAME Gaurav
ENV PASS password
gaurav@learning-ocean:~/dockerfiles$

Now, we build this image.

gaurav@learning-ocean:~/dockerfiles$ docker image build -t myubuntu:11 .

Running the container.

gaurav@learning-ocean:~/dockerfiles$ docker container run -it myubuntu:11

After this we can check if our environment variables are available in this container or not by using the following command.

root@883d24e4bb98:/# env
PASS=password
HOSTNAME=883d24e4bb98
TERM=xterm
NAME=Gaurav
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
SHLVL=1
HOME=/root
LESSOPEN=| /usr/bin/lesspipe %s
LESSCLOSE=/usr/bin/lesspipe %s %s
_=/usr/bin/env
root@883d24e4bb98:/#

We can see above the environment variables we created are available in this container.

RUN

The RUN instruction is used to execute any command in a new layer on top of the current image and the resulting image will be used for the next step in the docker file.

Let's see an example:

RUN statements in the below dockerfile are supposed to create a text file with the current working directory as their content.

gaurav@learning-ocean:~/dockerfiles$ cat Dockerfile
FROM ubuntu:14.04
LABEL name="learning-ocean"
LABEL email="[email protected]"
ENV NAME Gaurav
ENV PASS password
RUN pwd > /tmp/1stpwd.txt
RUN cd /tmp
RUN pwd>/tmp/2ndpwd.txt

build image using below command

gaurav@learning-ocean:~/dockerfiles$ docker image build -t myubuntu:11 .

check the files in containers

gaurav@learning-ocean:~/dockerfiles$ docker container run -it myubuntu:1
root@4be71bf9d507:/# cat /tmp/1stpwd.txt
/
root@4be71bf9d507:/# cat /tmp/2ndpwd.txt
/
root@4be71bf9d507:/#

After building this image, running the container and checking the content of the files we see that the content of both the files is the same whereas the current working directory in the file '2ndpwd.txt' should have been "/tmp". This happened because we were expecting that the three RUN statements will be executed sequentially but actually each RUN instruction is executed independently and in a separate layer by docker. Each RUN instruction will be executed in its own container and each statement will start the execution from its root directory.

WORKDIR

The WORKDIR instruction is used to set or change the working directory for any instruction that follows it in the dockerfile.

Let's see its usage:

We will create a new image from the below docker file and run the container and then check the content of file '3rdpwd.txt' it should contain the current working directory i.e "/tmp" which is changed in the previous step using workdir instruction.

gaurav@learning-ocean:~/dockerfiles$ cat Dockerfile
FROM ubuntu:14.04
LABEL name="learning-ocean"
LABEL email="[email protected]"
ENV NAME Gaurav
ENV PASS password
RUN pwd > /tmp/1stpwd.txt
RUN cd /tmp
RUN pwd>/tmp/2ndpwd.txt
WORKDIR /tmp
RUN pwd>/tmp/3rdpwd.txt

run container from created image

gaurav@learning-ocean:~/dockerfiles$ docker image build -t myubuntu:15 .

checking in container

gaurav@learning-ocean:~/dockerfiles$ docker container run -it myubuntu:15
root@4be71bf9d507:/# cat /tmp/1stpwd.txt
/
root@4be71bf9d507:/# cat /tmp/2ndpwd.txt
/
root@4be71bf9d507:/# cat /tmp/3rdpwd.txt
/tmp

We can see here that the content of the file 3rdpwd.txt is /tmp which was the directory we switched to using workdir instruction.