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.

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


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

[email protected]:~/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
[email protected]:~/dockerfiles$

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

[email protected]:~/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.

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

Now, we build this image.

[email protected]:~/dockerfiles$ docker image build -t myubuntu:11 .

Running the container.

[email protected]:~/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.

[email protected]:/# 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
[email protected]:/#

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.

[email protected]:~/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

[email protected]:~/dockerfiles$ docker image build -t myubuntu:11 .


check the files in containers

[email protected]:~/dockerfiles$ docker container run -it myubuntu:1
[email protected]:/# cat /tmp/1stpwd.txt
/
[email protected]:/# cat /tmp/2ndpwd.txt
/
[email protected]:/#


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.

[email protected]:~/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

[email protected]:~/dockerfiles$ docker image build -t myubuntu:15 .


checking in container

[email protected]:~/dockerfiles$ docker container run -it myubuntu:15
[email protected]:/# cat /tmp/1stpwd.txt
/
[email protected]:/# cat /tmp/2ndpwd.txt
/
[email protected]:/# 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.