Skip to content

Build an image

In this section, it is explained how to build a Docker image from scratch.

Dockerfile

A Dockerfile is like a recipe. It is a text file containing instructions and their arguments allowing Docker to build automatically images. This is the basic building block of an image.

image-creation-process

Here is a list of instructions (adapted from here):

Instructions Explanations
FROM To specify the base image.
LABEL To specify metadata information of Docker image.
RUN To Execute commands during the image build process (e.g. linux commands...).
COPY Copies local files and directories to the image.
ADD Similar to COPY instruction with extra features like local-only tar extraction and remote URL support. However, prefer COPY instruction to ADD.
ENV Sets environment variables inside the image that will be available during build time and also in the running container. To set only build-time variables, use ARG instruction.
ARG Sets build-time variables with key and value. ARG variables are not available in the running container. For persistent variables in a running container, use ENV.
WORKDIR Sets the current working directory. You can reuse this instruction in a Dockerfile to set a different working directory. If WORKDIR is set, instructions like RUN, CMD, ADD, COPY, or ENTRYPOINT gets executed in that directory. Prefer the use of WORKDIR instead of instructions like RUN cd ..
USER Sets the user name and UID when running the container. Usefull to set a non-root user of the container. The user must be created if it does not exists.
CMD Executes a command in a running container. There can be only one CMD. It can be overridden from the Docker CLI.
ENTRYPOINT Specifies the commands that will be executed when the Docker container starts. ENTRYPOINT can only be overriden with the --entrypoint flag.
EXPOSE Specifies the port to be exposed for the Docker container.
VOLUME It is used to create or mount the volume to the Docker container.

My first build

The goal here is to create an image to run a python program in a container.

According to the official documentation, in most cases, it is best to put each Dockerfile in an empty directory. Then, add to that directory only the files needed for building the Dockerfile; hence:

  • Create an empty directory called build:
mkdir build
  • Copy the python_codes folder in it:
cp -rp python_codes build
cd build
  • Create a file requirements.txt file
matplotlib>=3.7.1
numpy>=1.24.3

and a file named Dockerfile:

FROM python:3.10.11

LABEL maintainer="Aurélien Bailly-Reyre <aurelien.bailly-reyre@lpnhe.in2p3.fr>"

ARG USER_NAME='bob'
ARG CODE_DIR='codes'
ARG REQ_FILE='/tmp/requirements.txt'

RUN apt-get update --yes && apt-get upgrade --yes &&\
    apt-get --quiet clean &&\
    rm -rf /var/lib/apt/lists/* &&\
    useradd --uid 1000 $USER_NAME

WORKDIR /home/$USER_NAME

COPY python_codes $CODE_DIR
COPY requirements.txt /tmp

RUN chown $USER_NAME:$USER_NAME -R /home/$USER_NAME &&\
    pip install --no-cache-dir -r $REQ_FILE &&\
    rm -f $REQ_FILE

USER $USER_NAME
WORKDIR /home/$USER_NAME/$CODE_DIR

RUN id && pwd && ls -l

ENTRYPOINT ["python"]
CMD ["hello_world.py"]

Remark

There are differences between ENTRYPOINT and CMD but there can be many situations in which combining both can be insteresting. For instance, here, the executable python is defined with ENTRYPOINT, while CMD specifies the default parameter hello_world.py.

  • Build the image:
docker build -f Dockerfile -t mypython .

Remark

In this example we could have ommited the -f Dockerfile part. Indeed, by default the docker build command will look for a Dockerfile at the root of the build context. The -f (or --file) option lets you specify the path to an alternative file to use instead.

  • Create containers to test the image with:
docker run -it --rm mypython
docker run -it --rm mypython pi.py
docker run -it --rm mypython bash
docker run -it --rm --entrypoint bash mypython

As a training exercise, to see the difference between ENTRYPOINT and CMD instructions, in the same directory:

  • copy the existing Dockerfile:
 cp -p Dockerfile Dockerfile.cmd
  • replace the two last lines with:
CMD ["python", "hello_world.py"]
  • Create an image mypython with the tag test:
docker build -f Dockerfile.cmd -t mypython:test .
  • Run containers to test the image and conclude:
docker run -it --rm mypython:test
docker run -it --rm mypython:test python pi.py
docker run -it --rm mypython:test bash