Dockerfile (original) (raw)

Last Updated : 28 Apr, 2026

A Dockerfile is a simple text file that contains a script of instructions for building a Docker image.

docker_image

Dockerfile

Container Creation using Dockerfile

  1. We start by writing the Dockerfile of the application. This contains all the necessary instructions like the runtime environment, the base image needed, files to copy and ports to expose.
  2. The Image is built using the docker file by using the command like docker build -t imagetag .
  3. From the image, we can run the container using the command docker run -- name

dockerfile

Docker containers

Dockerfile Instructions

Each instruction plays a specific role in building images, from defining the base system to copying files, running scripts, and setting container

dockerfile_build_flow

Dockerfile Instructions

1. FROM

This must be the very first instruction in a Dockerfile. It sets the base image for subsequent instructions, essentially choosing the foundational operating system or environment for your application.

Syntax

FROM

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

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

Has all the features of COPY, but can also handle remote file URLs and automatically extract compressed files (like .tar.gz) into the destination directory.

Syntax

ADD

Example: Try to download Jenkins using ADD command

ADD https://ftp.yz.yamagata-u.ac.jp/pub/misc/jenkins/war/2.397/jenkins.war

Best Practice: Always prefer COPY unless you specifically need ADD's tar extraction or remote URL features. COPY is more transparent and predictable

4. RUN

This command executes any commands in a new layer on top of the current image and commits the results. These are build-time commands needed to set up your application, such as installing packages.

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

**Syntax

ENTRYPOINT [command + args]

Example: Executing the echo command.

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

Example 1: 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.

Specifying Docker Version

Build the Jenkins Image

Dockerfile for Jenkins Image

FROM openjdk:11-jdk
MAINTAINER GFG author
LABEL env=production
ENV apparea /data/app
RUN mkdir -p $apparea
ADD https://ftp.yz.yamagata-u.ac.jp/pub/misc/jenkins/war/2.397/jenkins.war $apparea
WORKDIR $apparea
EXPOSE 8080
CMD ["java","-jar","jenkins.war"]

Jenkins Dockerfile

Dockerfile for Jenkins Image

**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 .

Docker image

Give tag to Dockerfiles

**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>

Docker Container

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

containerized application

Troubleshooting of Dockerfile Issues

The following are the some of the troubleshooting of Dockerfile Issues:

Dockerfile Vs Docker Compose

The following are the difference between dockerfile and Docker Compose:

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