Dockerfile and Dockerimage

Create a Dockerimage using a Dockerfile

Repo: https://gitlab.cern.ch/ebocchi/gridka_docker -- docker/myapache


A Dockerfile defines what goes on in the environment inside your container. Given the isolation provided by Docker, all the resources (e.g., storage devices, network interfaces or ports, etc.) must be defined and mapped to the outside world. Similarly, additional files must be copied in the container, software packages must be installed ad-hoc, and external Git repositories must be cloned, independently on what is running on the host system.

Here, the official reference page for Dockerfiles provided by Docker: https://docs.docker.com/engine/reference/builder

And also the best practices: https://docs.docker.com/develop/develop-images/dockerfile_best-practices


Let's create the Dockerfile for myapache image:

# Use the official centos image as parent image
FROM centos:latest

# Define who is responsible for this Dockerifle
MAINTAINER Enrico Bocchi <enrico.bocchi@cern.ch>

# Install Apache HTTP Server
RUN yum -y install httpd

# Install debugging tools
RUN yum -y install \
        net-tools \
        bind-utils \
        curl \
        tcpdump \
        vim \
        nano

# Add the homepage to be served by Apache
ADD index.html /var/www/html/index.html

# Tell the container to listed to port 80 at runtime
EXPOSE 80/tcp

# Define the executable to run when starting the container
CMD ["httpd", "-DFOREGROUND"]


Now we are ready to create the Dockerimages, which will be:

  • Layered: One command in the Dockerfile corresponds to one layer of the image
  • Stored locally or in remote (public // private) repositories

To create the Dockerimage, we use the build command and we need to specify a tag, i.e., a label that is attached to the image.

A tag is typically made of two parts:

  • The image name (myapache, in the example)
  • The tag itself (dockerfile, in the example), which defaults to 'latest' if unspecified.
> # docker build -t myapache:dockerfile .
Sending build context to Docker daemon  3.584kB
Step 1/7 : FROM centos:latest
 ---> 5182e96772bf
Step 2/7 : MAINTAINER Enrico Bocchi <enrico.bocchi@cern.ch>

[...cut...]

Step 7/7 : CMD ["httpd", "-DFOREGROUND"]
 ---> Running in 7b5d68594b66
Removing intermediate container 7b5d68594b66
 ---> 73d5df19b770
Successfully built 73d5df19b770
Successfully tagged myapache:dockerfile

By listing the available images, we see our myapache:dockerfile is there:

> # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
myapache            dockerfile          73d5df19b770        About a minute ago   482MB
myapache            snapshot            d63ed44df2b9        27 hours ago         387MB
centos              latest              5182e96772bf        2 weeks ago          200MB
hello-world         latest              2cb0d9787c4d        5 weeks ago          1.85kB

... and we can also see all the layers created at build time:

> # docker images --all
myapache            dockerfile          73d5df19b770        47 hours ago        482MB
<none>              <none>              b948076778cc        47 hours ago        482MB
<none>              <none>              6bdd760471c8        47 hours ago        325MB
<none>              <none>              55a1b143d760        47 hours ago        200MB


Docker is also provided with a separate command to tag already-existing images:

> # docker tag myapache:dockerfile myapache:2ndtag

> # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myapache            2ndtag              73d5df19b770        47 hours ago        482MB
myapache            dockerfile          73d5df19b770        47 hours ago        482MB
myapache            snapshot            d63ed44df2b9        3 days ago          387MB
centos              latest              5182e96772bf        2 weeks ago         200MB
hello-world         latest              2cb0d9787c4d        6 weeks ago         1.85kB

In this case, the same image will have two different tags, but the content is left unchanged:

  • Same Image ID (unique identifier for an image, not defined by the user), creation time, size
  • No rebuild needed

Warning: Images can be deleted with docker rmi.

  • When deleting by <image_name>:<tag>, the image will be simply untagged.
  • When deleting by <image_id>, all the replicas with the same Image ID will be removed.


Now we can take the just-built Dockerimage myapache:dockerfile and run it!

> # docker run --name builtapache -P -dt myapache:dockerfile
aedb60549bf530f4f8533c8215bf594bb276be6ebe04cd8204d1241bbbc345aa

> # docker ps -a
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS              PORTS                   NAMES
aedb60549bf5        myapache:dockerfile   "httpd -DFOREGROUND"   4 seconds ago       Up 3 seconds        0.0.0.0:32770->80/tcp   builtapache
58fe7a143bd1        centos                "bash"                 2 minutes ago       Up About a minute   0.0.0.0:8080->80/tcp    myapache


Proceed to Exercises 4, 5, and 6: Course:Docker Container Hands-On/Docker Foundations/Exercises

 PreviousNext