In this blog post , We will learn about Dockerfile , How to build docker images with Dockerfile and run docker containers out from the image that we build.

build docker images

Before going further , Make sure you have installed Docker on your system.

What is Dockerfile?

Docker can build images automatically by reading the instructions from the Dockerfile.

A Dockerfile is a text document that contains all the commands a user can call on the command-line to assemble an image.

Using docker build command we can create an automated build that executes several command-line in succession.

Before creating your first Dockerfile , We need to understand the Dockerfile instructions.

Understanding Dockerfile Commands

FROM

  • A valid Dockerfile must start with a FROM instruction.
  • The FROM instruction initializes a new build stage and sets the base images for subsequent instrutions.
  • For example : The FROM instruction pulls an ubuntu image from the public docker respository and executes the further commands and creates a new image out of it. Eg: FROM ubuntu:latest
  • FROM can be used multiple times within a single Dockerfile to create multiple docker images.
  • The only instruction that we can use before FROM in the Dockerfile is ARG.
  • We can declare variables to the FROM instruction using ARG instruction.
ARG APP_VERSION=latest
FROM ubuntu:${APP_VERSION}

RUN

The RUN instruction is used to execute commands during the image build process.

RUN apt-get install nginx -y

Using RUN instructions we can install additional packages needed for the docker images that will be built.

Usually the RUN instructions will execute any commands in a new layer on top of the current images and commits results.The resulting committed image will be used for the next steps in the Dockerfile.

CMD

The main purpose of the CMD is to define the default commands to execute when running the container. For example: To start a process or a application.

The CMD instruction can be used only once is a Docker file.

If many CMD instructions are used within a Dockerfile , Then only the last CMD instruction within the Dockerfile will be executed.

CMD ["nginx", "-g", "daemon off;"]

LABEL

The LABEL instruction adds metadata to an image.

The images can have more than one label.

We can specify multiple labels for an Docker images in a single line.

LABEL multi.label1="val1" multi.label2="val2" other="val3"

EXPOSE

The EXPOSE instruction informs docker that the container listens on the specified network ports at runtime.

You can specify whether the port listens on TCP or UDP.But if it is not mentioned , The default is TCP.

EXPOSE 80/tcp
EXPOSE 80/udp

ENV

The ENV instruction is used to define environment variables that can used later during the build stage and can be replaced inline in many as well.

ENV can be passed in two forms.

Form 1:

ENV Myname Fit Devops

The above form will set a single variable to a value.The entire string after first space will be treated a single value including whitespace.

Form 2:

In this form , We can set multiple variables at a time.

In this form , It uses ‘=’ sign to represent the value.

We can use spaces , quotes and backslashes to separate the values.

ENV Myname="user name" mysite=fitdevops

ADD

The ADD instruction is used to copy files and folder from the host server or from remote server and add them to the filesystem of the image on the respective path.

ADD sample.txt /sampledir/
ADD --chown=1000:user files* /samplefolder/

COPY

The COPY instruction is used to copy new files and folders from any source and adds them to the file system of the container at the required destination.

COPY test* /dockertest/

ENTRYPOINT

The ENTRYPOINT allows us to configure a container that will run as an executables.

We have to define the command to start the application with the help of ENTRYPOINT instruction.

ENTRYPOINT has two forms.

Form 1:

ENTRYPOINT ["executable", "param1", "param2"]

Form 2:

ENTRYPOINT command param1 param2

VOLUME

The VOLUME instruction creates a mount point with the specified name (/test) within the container and links it with the host machine.

VOLUME /test

All the contents in the host machine will be populated on the new mount point within the container.

USER

The USER instruction is used to set user name (UID) and user group (GID) to use when running the image and also for any instructions in the Dockerfile.

USER rahulk

WORKDIR

The WORKER instruction sets the working directory for any RUN , CMD , ENTRYPOINT , COPY and ADD instructions in the Dockerfile.

Even If the WORKDIR is not mentioned in the Dockerfile instruction, it will be created automatically.

The WORJDIR instruction can be used multiple times within the Dockerfile.

WORKDIR /test

Prerequistes

Before creating the Dockerfile and building the docker images from it , We need to install Docker on the system.

Check this blog , To install Docker and Docker-Compose.

Creating Dockerfile

In this guide , We will learn to build an docker image to run Docker containers for Nginx using the Docker file.

First create a folder called project and then inside this folder , Create a file named Dockerfile

mkdir project && cd project
touch Dockerfile

Open the Dockerfile and add the below contents.

nano Dockerfile
#Pulls Ubuntu latest image from the Public repository
FROM ubuntu:latest

#Author / Owner of the Dockerfile
LABEL maintainer="[email protected]"

#Update the APT package repository
RUN apt-get update && apt-get upgrade -y

#Installing nginx packages
RUN apt-get install nginx -y

#Nginx will run on the port 80
EXPOSE 80

#After creating the container , keep the nginx process UP and running in the Foreground
CMD ["nginx", "-g", "daemon off;"]

After the adding the contents to the Dockerfile , Save and close the file.

Running the below command will start creating docker images from the ubuntu base image and then use the image we built we can run the docker container.

Building Docker Images

Lets build a docker image from the Dockerfile.

Go to the directory where Dockerfile is located and run docker build command as shown below.

docker build -t nginx-latest .

The above command will download the latest ubuntu as the base image and create a new custom image with the name nginx-latest.

You should see something as shown below.

build docker images

After the build process is completed , To check the lists of available docker images , Execute the following command.

docker images

You can see the docker image with the name which we have specified.

build docker images

We have successfully created a custom docker image , Using which we will create a container.

Creating Container from Docker Image

To create a new docker container from the custom image which we have created , Run the below command.

docker run -it -d -p 8080:80 --name nginx-web nginx-latest

With the help of above command , We are creating a container named nginx-web using nginx-latest docker image.

Port 80 is the container port and Port 8080 is the Host port.

After that , To check the running docker container on the system , Issue the following command.

docker ps
build docker images

The nginx which is running on the container with the port 80 can be accessed from the Host machine from the port 8080.

curl http://localhost:8080
build docker images

With the help of Dockerfile , We can quickly build docker images and run applications.

Using the same procedure you can setup Dockerfile for your own applications and build docker images and launch a container out of it.

Hope you find it helpful.Thanks for reading.

Please do check out my other applications.