How to create a Dockerfile with some dockerfile examples

Dockerfile is a text file that contains a list of commands that are used to build a docker image automatically. Basically, a docker file acts as a set of instructions that are needed to build a docker image.

We have earlier discussed how to create a docker container & also learned some important commands for managing the containers. In this tutorial, we will learn about how to create a dockerfile, all its parameters/commands with the dockerfile example.

Recommended Read: Complete guide for creating Vagrant boxes with VirtualBox

Also Read: Reference guide for Docker Commands — NEW v/s OLD

Dockerfile Example (Centos )

FROM centos:7

MAINTAINER linuxtechlab

LABEL Remarks=”This is a dockerfile example for Centos system”

RUN yum -y update && \

yum -y install httpd && \

yum clean all

COPY data/httpd.conf /etc/httpd/conf/httpd.conf

ADD data/html.tar.gz /var/www/html/

EXPOSE 80

ENV HOME /root

WORKDIR /root

ENTRYPOINT [“ping”]

CMD [“google.com”]

Parameters

FROM centos:7

FROM tells which base you would like to use for creating your docker image. Since we are using Centos:7, it’s mentioned there. We can use other OS like centos:6, ubuntu:16.04, etc

MAINTAINER linuxtechlab

LABEL Remarks=”This is a dockerfile example for Centos system”

Both fields MAINTAINER & LABEL Remarks are called labels. They are used to pass information like the Maintainer of the docker image, Version number, purpose, or some other remarks. We can add a number of labels but it’s recommended to avoid unnecessary labels.

RUN yum -y update && \

yum -y install httpd && \

yum clean all

Now RUN command is responsible for installing or changing the docker image as we see fit. Here we have asked RUN to update our system & then install apache on it. We can also ask it to create a directory or to install some other packages.

COPY data/httpd.conf /etc/httpd/conf/httpd.conf

ADD data/html.tar.gz /var/www/html/

COPY & ADD command almost serve the same purpose i.e. they are used to copy the files to docker image with one difference. Here we have used the COPY command to copy httpd.conf from the data directory to the default location of httpd.conf on the docker image.

And we then used ADD command to copy a tar.gz archive to apache’s document directory to serve content on the webserver. But you might have noticed we didn’t extract it & that’s the one difference ADD & COPY commands have, ADD command will automatically extract the archive at the destination folder.

Also we could have used ADD in place of copy, “ADD data/httpd.conf /etc/httpd/conf/httpd.conf “.

EXPOSE 80

EXPOSE command will open the mentioned port on the docker image to allow access to the outside world. We could also use EXPOSE 80/tcp or EXPOSE 53/udp.

ENV HOME /root

ENV command sets up environment variables, here we have used it to set HOME to /root. The syntax for using ENV is

ENV key value

Some examples of ENV usage are,

ENV user admin, ENV database=testdb, ENV PHPVERSION 7 etc etc.

WORKDIR /root

With WORKDIR, we can set a working directory for the docker image. Here it has been set to /root.

ENTRYPOINT [“ping”]

CMD [“google.com”]

ENTRYPOINT & CMD are both used to define an executable that should run once docker is up. On ENTRYPOINT, we define an executable & with CMD, we define additional parameters that are required for ENTRYPOINT. Like here, we have used ping with ENTRYPOINT but it requires an additional parameter, which we provided with CMD. Both commands are used in conjunction with each other.

We can also use CMD alone with something like CMD [“bash”].

Note:- Not all these parameters are required to pass while creating a Dockerfile, you can only the ones you need.

Apart from the commands discussed above, there are some other commands as well that can be used in the Dockerfile & that are mentioned below,

USER

With USER, we can define the user to be used to execute a command like USER dan. We can specify USER with RUN, CMD, or with ENTRYPOINT as well.

ONBUILD

ONBUILD command lets you add a trigger that will be executed at a later time when the current image is being used as a base image for another. For example, we have added our own content for the website using the dockerfile but we might not want it to be used for other docker images. So we will add,

ONBUILD RUN rm -rf /var/www/html/*

This will remove the contents when the image is being re-purposed.

So these were all the commands that we can use with our Dockerfiles. Mentioned below Dockerfile examples for Ubuntu & Fedora, for reference,

Ubuntu Dockerfile

# Get the base image

FROM ubuntu:16.04

# Install all packages

RUN \

apt-get update && \

apt-get -y upgrade && \

apt-get install -y apache2 && \

# adding some content for Apache server

RUN echo “This is a test docker” > /var/www/html/index.html

# Copying setting file & adding some content to be served by apache

COPY data/httpd.conf /etc/apache2/httpd.conf

# Defining a command to be run after the docker is up

ENTRYPOINT [“elinks”]

CMD [“localhost”]

Fedora Dockerfile

# Get the base image

FROM docker.io/fedora

MAINTAINER linuxtechlab

LABEL Remarks=”This is a dockerfile example for Fedora system”

# Updating dependencies, installing Apache and cleaning dnf caches to reduce container size

RUN dnf -y update && \

dnf -y install httpd && \

dnf clean all \

mkdir /data

# Copying apache configuration file & adding some content to be served by apache

COPY data/httpd.conf /etc/httpd/conf/httpd.conf

ADD data/html.tar.gz /var/www/html/

# Adding a script & granting it execute permissions

ADD data/script.sh /data

# Open http port for apache

EXPOSE 80

# Set environment variables.

ENV HOME /root

# Defining a command to be run after the docker is up

CMD [“/data/script.sh”]

Now that we know how to create a Dockerfile, we will use this newly learned skill for our next tutorial, to create a docker image & then will upload the same to DockerHub, the official Docker Public Image Registry.

If you think we might have missed something or have some queries regarding this tutorial, please let us know using the comment box below.

http://linuxtechlab.com is a beginner friendly website where you can learn Linux Tips & tricks,Scripting, also has lots of tutorials aimed at making Linux easy.