Create Docker images with Dockerfile?

Posted: | Last updated: | 4 minute read

Inception: Using Docker developers can package and run their applications in a containerized environment. In this article, we will take a detailed look at Dockerfiles and how to use them.

What is a Dockerfile?

  • A Dockerfile is a text configuration file written using a special syntax
  • It describes step-by-step instructions of all the commands you need to run to assemble a Docker Image.
  • The docker build command processes this file generating a Docker Image in your Local Image Cache, which you can then start-up using the docker run command, or push to a permanent Image Repository.

Create a Dockerfile

Creating a Dockerfile is preety easy. Use your choice of text editor to create a file named Dockerfile with some instructions. The name of the file does not really matter. Dockerfile is the default name but you can use any filename that you want (and even have multiple dockerfiles in the same folder).

Dockerfile Syntax

Lets understant the Dockerfile syntax by creating a sample Dockerfile for creating a Nginx docker image.

  • Each instruction in this file generates a new layer that gets pushed to your local image cache
  • Lines preceeded by # are regarded as comments and ignored
  • The line below states we will base our new image on the Latest Official Ubuntu
# Use an official Ubuntu as a parent image
FROM ubuntu:latest

# Identify the maintainer of an image
LABEL maintainer="sushil@timesofcloud.com"

# Update the image to the latest packages
RUN apt-get update && apt-get upgrade -y

# Install NGINX to test.
RUN apt-get install nginx -y

# Expose port 80
EXPOSE 80

# Last is the actual command to start up NGINX within our Container
CMD ["nginx", "-g", "daemon off;"]

Dockerfile Commands

ADD

Defines files to copy from the Host file system onto the Container. ADD ./local/config.file /etc/service/config.file

CMD

This is the command that will run when the Container starts. CMD ["nginx", "-g", "daemon off;"]

ENTRYPOINT

Sets the default application used every time a Container is created from the Image. If used in conjunction with CMD, you can remove the application and just define the arguments there.

CMD Hello World! ENTRYPOINT echo

ENV

Set/modify the environment variables within Containers created from the Image. ENV VERSION 1.0

EXPOSE

Define which Container ports to expose. EXPOSE 80

FROM

Select the base image to build the new image on top of FROM ubuntu:latest

LABEL maintainer

Optional field to let you identify yourself as the maintainer of this image. This is just a label (it used to be a dedicated Docker directive). LABEL maintainer=sushil@vermanotes.com"

RUN

Specify commands to make changes to your Image and subsequently the Containers started from this Image. This includes updating packages, installing software, adding users, creating an initial database, setting up certificates, etc. These are the commands you would run at the command line to install and configure your application. This is one of the most important Dockerfile directives. RUN apt-get update && apt-get upgrade -y && apt-get install -y nginx && rm -rf /var/lib/apt/lists/*

USER

Define the default User all commands will be run as within any Container created from your Image. It can be either a UID or username. USER docker

VOLUME

Creates a mount point within the Container linking it back to file systems accessible by the Docker Host. New Volumes get populated with the pre-existing contents of the specified location in the image. It is specially relevant to mention is that defining Volumes in a Dockerfile can lead to issues. Volumes should be managed with docker-compose or docker run commands. Volumes are optional. If your application does not have any state (and most web applications work like this) then you don’t need to use volumes.

VOLUME /var/log

WORKDIR

Define the default working directory for the command defined in the ENTRYPOINT or CMD instructions. WORKDIR /home

Building and Testing Dockerfiles

To build a Docker image from a Dockerfile, you need to run the docker build command. Here’s an example:

docker build -t ubuntu-nginx .

The above command tells Docker to build an image from the Dockerfile in the current directory (.) and tag it with the name ubuntu-nginx. Docker will read the instructions in the Dockerfile and execute them to build the image.

Once the build is complete, you can run below commands to view the Docker images and then create a Container out of it.

# List the Docker images
$ docker images

# Create a Container
$ docker run -p 4000:80 ubuntu-nginx

The first command tells Docker to list the available Docker images on host and the second command run a container based on the ubuntu-nginx image and map port 4000 on the host machine to port 80 in the container. This will allow you to access the application running inside the container by visiting http://localhost:4000 in a web browser.

Conclusion: The Dockerfile is a powerful tool for building custom Docker images. By using a Dockerfile, you can define the base image, dependencies, environment variables, and commands.