Skip to content

What is Dockerfile ?

The operating system (OS) libraries and dependencies required to run the application source code which is not reliant on the underlying operating system (OS) included in the Dockerfile, which is a standardized, executable component. Programmers may design, distribute, launch, run, upgrade, and manage containers using the open-source platform Docker. Enterprise Edition (EE) and Community Edition (CE) of Docker are both available. The Enterprise Version is for businesses and IT teams working on mission-critical production applications, while the Community Edition is suitable for small teams just learning Docker.

Dockerfile

What is Docker ?

Docker is an open-source platform where developers can containerize the application. Containers are accessible before Docker but have gained popularity as a result of Docker. The most crucial aspects of Docker are the Docker Engine and Docker Hub. The first one works on your local system to run your program, and the second one is similar to a cloud service where we can share our docker images with everyone.

What is Dockerfile ?

The Dockerfile uses DSL (Domain Specific Language) and contains instructions for generating a Docker image. Dockerfile will define the processes to quickly produce an image. While creating your application, you should create a Dockerfile in order since the Docker daemon runs all of the instructions from top to bottom.

Note

Dockerfile is the source code of the image

What is Docker Image ?

An artifact with several layers and a lightweight, compact stand-alone executable package that contains all of the components required to run a piece of software, including the code, a runtime, libraries, environment variables, and configuration files is called a Docker image.

What is Docker Container ?

A container is a runtime instance of an image. Containers make development and deployment more efficient since they contain all the dependencies and parameters needed for the application it runs completely isolated from the host environment.

Dockerfile commands/Instructions

1. FROM

Represents the base image(OS), which is the command that is executed first before any other commands.

Syntax:

FROM <ImageName> 

Example: The base image will be ubuntu:19.04 Operating System.

FROM ubuntu:19.04

2. COPY

The copy command is used to copy the file/folders to the image while building the image.

Syntax:

COPY <Source>   <Destination>  

Example: Copying the .war file to the Tomcat webapps directory

COPY target/java-web-app.war  /usr/local/tomcat/webapps/java-web-app.war

3. ADD

While creating the image, we can download files from distant HTTP/HTTPS destinations using the ADD command.

Syntax:

ADD <URL>  

Example: Try to download Jenkins using ADD command

ADD https://get.jenkins.io/war/2.397/jenkins.war

4. RUN

Scripts and commands are run with the RUN instruction. The execution of RUN commands or instructions will take place while you create an image on top of the prior layers (Image).

Syntax:

RUN < Command + ARGS> 

Example:

RUN touch file

5. CMD

The main purpose of the CMD command is to start the process inside the container and it can be overridden.

Syntax:

CMD [command + args] 

Example: Starting Jenkins

CMD ["java","-jar", "Jenkins.war"]

6. ENTRYPOINT

  • A container that will function as an executable is configured by ENTRYPOINT. When you start the Docker container, a command or script called ENTRYPOINT is executed.

  • It can’t be overridden.The only difference between CMD and ENTRYPOINT is CMD can be overridden and ENTRYPOINT can’t.

Syntax:

ENTRYPOINT [command + args] 

Example: Executing the echo command.

ENTRYPOINT ["echo","Welcome to GFG"]

7. MAINTAINER

By using the MAINTAINER command we can identify the author/owner of the Dockerfile and we can set our own author/owner for the image.

Syntax:

MAINTAINER <NAME>

Example: Setting the author for the image as a GFG author.

MAINTAINER  GFG author 

To know more the sytnx of Dokcerfile refer to the Syntax of Dockerfile.

Stages of Creating Docker Image from Dockerfile

The following are the stages of creating docker image form Dockerfile:

  1. Create a file named Dockerfile.

  2. Add instructions in Dockerfile.

  3. Build Dockerfile to create an image.

  4. Run the image to create a container.

Example 1: Steps To Create Dockerfile With Example (Jenkins)

In this example, we will write the Dockerfile for Jenkins and build an image by using Dockerfile which has been written for Jenkins and we will run it as a container.

Step 1

Open Docker and create a file with the name Dockerfile.

Step 2

Open the Dockerfile by using the vi editor and start writing the command that is required to build the Jenkins image.

Example 1 Step 2.1

We used JDK as a base image because Jenkins’s pre-requisite is JDK after that we added a command called MAINTAINER which indicates the author or owner of the docker file and we added the ENV variable where we set the path for the Jenkins and by using RUN command we are creating the path and by using ADD we are downloading the Jenkins and starting the .war file with the help of CMD command.

FROM openjdk:11-jdk
MAINTAINER GFG author
LABEL env=production
ENV apparea /data/app
RUN mkdir -p $apparea
ADD https://get.jenkins.io/war/2.397/jenkins.war $apparea
WORKDIR $apparea
EXPOSE 8080
CMD ["java","-jar","jenkins.war"]

Example 1 Step 2.2

Step 3

Build the image by using the below command with the help of Dockerfile and give the necessary tags. and the dot(.) represents the current directory which is a path for Dockerfile.

docker build -t jenkins:1 .

Example 1 Step 3

Step 4

Run the container with the help image ID or tag of the image by using the below command.

docker run -d -p 8080:8080 <Imagetag/ID>

Example 1 Step 4

Step 5

Accesses the application (Jenkins) from the internet with the help of host port and hostIP (HostIP: Port)

Example 1 Step 5

Example 2: Steps To Create Dockerfile

Step 1

Create a file name called “Dockerfile”.By default when you run the docker build commands docker searches for a file named Dockerfile. However, it is not compulsory, you can also give some different names, and then you can tell the docker that this particular file is local but for now we will go with the Dockerfile.

Example 2 Step 1

Step 2

The very first instruction that a docker file starts with is FROM. Here you have to give a base image. So for example, if you want to get a base image from Ubuntu we will use FROM Ubuntu.

FROM ubuntu
  • Then the other instruction is you have to give a MAINTAINER. This is optional but it’s a best practice that you give the maintainer of this image so that it is very easy to find out who is the maintainer and you can give your name and email as well.

  • And if you want you can just give the email as well without giving the name. But here we are giving the entire thing.

    MAINTAINER YOUR_NAME <YOUR_EMAIL_ID>
    
  • Next, we want to run something so we will say run any command we can use RUN and add the command that you need to run.

    RUN apt-get update
    
  • And if you want to run something on the command line during container creation you can give CMD and inside square brackets, and we add the command. Here it is as shown below:

    CMD ["echo", "Hello Geeks!"]
    
  • At this point the file will have the following commands:

    FROM ubuntu
    MAINTAINER YOUR_NAME <YOUR_EMAIL_ID>
    RUN apt-get update
    CMD ["echo", "Hello Geeks!"]
    

Step 3

Now we have to build the image so here are the commands you can use:

docker build /<FILE_LOCATION> 

or

docker build . -f Dockerfile.txt

It says docker build and you have to give the location of your dockerfile. This will start building the image.

Example 2 Step 3.1

Command to list the images

docker image ls

or

docker images

Example 2 Step 3.2

Benefits of Dockerfile

The following are the benefits of Dockefile:

  • Consistency and Reproducibility: Dockerfile ensures that environment setups and dependencies are consistently facilitated across different setups minimizing the host environment dependent issues.

  • Version Control: Dockerfiles can be used for versioning along with your source code, It helps in tracking the changes and rollbacks.

  • Automation: It provides the automation with the process of building, configuring, and deploying the applciations with reduicng the manual intervention and errors.

Best Practices for writing Dockerfile

The following are the best practices for writing Dockerfile:

  • Use offical base Images: Try on using the offical base images for ensuring reliability, security and comptability.

  • Minimize Layers: Try on minimizing the layers by combining the common commands using && option and with using multi stage builds.

  • Leverage Caching: By ordering the instructions from least to most frequently changing one we can maximize the cache layering and speed up the builds.

  • Keep it Clean: Ensure to remove the unnnecessary files and use the .dockerignore to exclude the files and directories that are not neeced in the image.

Trouble Shooting of Dockerfile Issues

The following are the some of the trouble shooting of Dockerfile Issues:

  • Check Build Logs: Review the build logs for identifying the error through error messages and with log details. It helps in with providing the valuable information.

  • Validate Syntax and Instructions: It helps in ensring the dockerfile syntax and the instructions are in the proper order, It helps in addressing the common issues including the missing commands or in correct parameters.

  • Optimzie Layer Caching: Try to verify whether the caching is using effectively. Through reordering the instructions we can reduce the changes in frequently modified layers to speed up the build process.

  • Dependency Management: Through ensuring all the dependencies correctly placing as accessable, we can avoid the build failures.

Dockerfile vs Docker Compose

The following are the difference between dockerfile and Docker Compose:

Feature Dockerfile Docker Compose
Purpose Defines how to build a single Docker image Defines and runs multi-container Docker applications
File Extension Dockerfile (no extension) docker-compose.yml
Usage Builds an image layer by layer from instructions Manages multi-container setups and networking
Configuration Focus Focuses on image creation Focuses on container orchestration and configuration
Key Commands FROM, RUN, CMD, COPY, ADD services, volumes, networks
Single vs Multi-Container Single-container focus Multi-container focus
Dependencies Each image built individually Handles inter-container dependencies
Example Use Case Creating a reusable environment for an app Running an application stack (e.g., web server, database)

Conclusion

In this post, we’ve covered a few of the crucial commands and instructions that are frequently used in Dockerfile and detailed how Dockerfile works using examples. It provided easy-to-grasp syntax and examples for each and every Dockerfile command.