How to Create Dockerfile step by step and Build Docker Images using Dockerfile

There were days when a organization use to get physical server and a system administrator was asked to make the system ready within months like Installing OS, Adding Software’s and Network configuration and finally applications use to get deployed in months.

Now the same work can be done in literally 5 minutes . Yes it can be done by launching docker containers using Dockerfile ( Layer based ) docker image building file. If you would like to know more follow along.

In this this tutorial we will learn everything about Dockerfile , how to create Dockerfile and commands used inside Dockerfile also known as Docker Instruction. These Dockerfile further can be used to create customized docker image. Lets jump in to understand each bit of it.

Table of content

  1. What is Dockerfile?
  2. Prerequisites
  3. How to Create Dockerfile ( Dockerfile commands or Dockerfile Instructions)
  4. How to build a Docker Image and run a container using Dockerfile
  5. Conclusion

What is Dockerfile?

Docker file is used to create a customized docker images on top of basic docker image. It is a text file that contains all the commands to build or assemble a new docker image. Using docker build command we can create new customized docker images . Its basically another layer which sits on top of docker image. Using newly built docker image we can run containers in similar way.

This image has an empty alt attribute; its file name is image-43.png

Prerequisites

  • You must have ubuntu machine preferably 18.04 version + and if you don’t have any machine you can create a ec2 instance on AWS account
  • Docker must be installed on ubuntu machine. If you don’t have follow here

How to Create Dockerfile ( Dockerfile commands)

  • There are two forms in which docker file can be written
    • Shell form <instruction> command
    • Exec form <instruction> [“executable”, “param1”, “param2”]
# Shell form
ENV name John Dow
ENTRYPOINT echo "Hello, $name"
# exec form
RUN ["apt-get", "install", "python3"]
CMD ["/bin/echo", "Hello world"]
ENTRYPOINT ["/bin/echo", "Hello world"]
  • To build docker Image from Dockerfile
docker build .        or

docker build -f /path-of-Docker-file .
  • Environmental variables inside Docker file can be written as $var_name or ${var_name}
WORKDIR ${HOME}  # This is equivalent to WORKDIR ~
ADD . $HOME      # This is equivalent to ADD . ~
  • FROM command is used when we need to build a new Docker Image using Base Image
    • Below command will set ubuntu:14.04 as the base image.
FROM base:${CODE_VERSION}

FROM ubuntu:14.04
  • RUN command is executed while building the image that is on top of the current image and then creates a new layer. You can run multiple RUN commands in Dockerfile
RUN echo $VERSION
# RUN <command> (shell form)
# RUN ["executable", "param1", "param2"] (exec form)
  • ADD command will add all the files from the host to container
    • Below command will add a file from folder directory kept at host to containers /etc directory
ADD folder/file.txt /etc/
  • CMD command will set the default command if you don’t specify any command while starting an container.
    • It can be overridden by user passing an argument while running the container.
    • If you apply multiple CMD command only last takes effect
CMD ["Bash"]

EXAMPLE

  • Lets assume a single line Docker file containing following code
CMD [“echo”, “Hello World”]
  • Lets create a docker Image
docker build . 
  • Run a container to see CMD command actions
sudo docker run [image_name]
  • Check the Output of the command
O/p:  Hello World
  • Run a container with an argument to see CMD command actions
sudo docker run [image_name] hostname
  • Check the Output of the command
O/P: 067687387283 # Which is containers hostname
  • Maintainer allows you to add author details
MAINTAINER support@automateinfra.com
  • EXPOSE helps to inform docker about the port which container should listen on
    • Below are are setting a container to listen on port 8080
EXPOSE 8080
  • The ENV command sets an environment variable in the new container
    • Below we are setting HOME environments variable to /root
ENV HOME /root
  • USER command Sets the default user within the container
USER ansible
  • VOLUME command creates a shared volume that can be shared among containers or by the host machine
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
  • WORKDIR command set the default working directory for the container
WORKDIR app/
  • ARG command allows users to pass at build-time with the docker build command .
    • Syntax  --build-arg <varname>=<value> 
ARG username
docker build  --build-arg username=automateinfra 
  • LABEL instruction adds metadata to an image and it uses key value pair
LABEL 
  • SHELL command allows to overwrite the use of default shell.
    • SHELL command will overwrite the use of [“/bin/sh”,”-c”] in case of linux shell
    • SHELL command will overwrite the use of [“cmd”,”/S”,”/C”] in case of windows shell

Executed as cmd /S /C echo -command Write Host default
RUN powershell -command Write-Host default


Executed as PowerShell  -command Write-Host hello
SHELL ["PowerShell", "-command"]
RUN Write-Host hello
  • ENTRYPOINT is also used for running the command but with a difference from CMD command
    • In case of ENTRYPOINT command if you give command line argument ENTRYPOINT doesn’t allow to override it.

EXAMPLE

  • Lets assume a single line Docker file containing following code
ENTRYPOINT  [“echo”, “Hello World”]
  • Lets create a docker Image
docker build . 
  • Run a container to see ENTRYPOINTcommand actions
sudo docker run [image_name]
  • Check the Output of the command
O/p:  Hello World
  • Run a container with an argument to see ENTRYPOINT command actions
sudo docker run [image_name] parameter
  • Check the Output of the command
O/P: Hello World parameter

How to Create Docker Image and run a container using Dockerfile

Now we should be good with how Dockerfile is created using different commands. Lets now dive in to see some of the examples to get you started.

EXAMPLE 1

  • Create a folder under opt directory and name it as dockerfile-demo1
cd /opt
mkdir dockerfile-demo1
cd dockerfile-demo1
  • Create a Dockerfile with your favorite editor
vi Dockerfile
  • Command which we will use for Dockerfile
    • FROM: It sets the base image as ubuntu
    • RUN: It runs the following commands in the container
    • ADD: It adds the file from a folder
    • WORKDIR: It tells about the working directory
    • ENV: It sets a environment variable
    • CMD: Its runs a command when the container starts
  • Paste the below content
FROM ubuntu:14.04
RUN \
    apt-get -y update && \
    apt-get -y upgrade && \
    apt-get -y install git curl unzip man wget telnet
ADD folder/.bashrc /root/.bashrc
WORKDIR /root
ENV HOME /root
CMD ["bash"]
  • Now, build a Docker Image using the following command
 docker build -t image1 .
  • Lets verify the Docker Image by running the following command.
docker images
  • Now, its time to check if Docker Image is successfully working . So lets run a container and then verify all the Dockerfile commands inside the container.
docker run -i -t 5d983653b8f4
Looks Great, we can see all the commands which we used in Docker file were executed and Docker Image was created . We tested this on one of the container built using Docker Image.

EXAMPLE 2

  • Create a folder under opt directory and name it as dockerfile-demo2
cd /opt
mkdir dockerfile-demo2
cd dockerfile-demo2
  • Create a Dockerfile with your favorite editor
vi Dockerfile
  • Paste the below content
FROM ubuntu:14.04


ARG LABEL_NAME
LABEL org.label-schema.name="$LABEL_NAME"
SHELL ["/bin/sh", "-c"]


RUN apt-get update && \
    apt-get install -y sudo curl git gcc make openssl libssl-dev libbz2-dev libreadline-dev libsqlite3-dev zlib1g-dev libffi-dev


USER ubuntu
WORKDIR /home/ubuntu


ENV LANG en_US.UTF-8
CMD [“echo”, “Hello World”]


  • Now, build a Docker Image using the following command
 docker build --build-arg LABEL_NAME=mylabel  -t imagetwo .
  • Lets verify the Docker Image by running the following command.
docker images
  • Now, its time to check if Docker Image is successfully working . So lets run a container and then verify all the Dockerfile commands inside the container.
docker run -i -t 2716c9e6c4af
  • CMD command ran successfully
  • As we defined a custom CMD which displays the echo command and exits out of container. Lets go inside the container and check other details.
    • User is ubuntu
    • Working directory is /home/ubuntu
    • Curl is installed
    • ENV LANG is also set.
docker run -i -t 2716c9e6c4af /bin/bash
  • Finally if you want to check LABEL command then you can see it on host by inspecting the docker image.
docker inspect 2716c9e6c4af

Looks Great, we can see all the commands which we used in Docker file were executed and Docker Image was created . We tested this on one of the container built using Docker Image.

Conclusion

In this tutorial we learnt in depth commands used inside Dockerfile to build a Docker Image . There are several commands which we covered using examples in the demonstration.

Also we learnt how to create docker image and run containers and verify if those commands were executed successfully. Dockerfile is very important concept for building new Docker Images on top of Base Docker image.

Now, you’re ready with how to create Dockerfile and using Dockerfile create Image and run containers. Hope this tutorial will help you with Docker concepts. Please share if you like.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s