Skip to main content

How to create a Dockerfile?

Docker is used to create, run and deploy applications in containers. A Docker image contains application code, libraries, tools, dependencies and other files needed to make an application run. When a user runs an image, it can become one or many instances of a container. In this tutorial we will be creating ubuntu image:

Step 1) Create a file in root directory with name Dockerfile

Step 2) In the file you have to mention 3 items which FROM, RUN, CMD

Step 3) Mention following in the Dockerfile

# This is a sample image created using ubuntu
FROM ubuntu

RUN apt-get update
CMD ["echo", "Image created"]

Creating a Dockerfile is a fundamental step in building Docker images. A Dockerfile is a plain text file that contains a set of instructions for Docker to follow when building an image. Here's a step-by-step guide on how to create a Dockerfile:

Choose a Base Image: Start by selecting a base image that serves as the foundation for your application. The base image contains the operating system and other dependencies required to run your application. You can find a wide range of base images on Docker Hub ( or other Docker registries.

Create a New Text File: Open a text editor and create a new file called "Dockerfile" (without any file extension).

Specify the Base Image: In the Dockerfile, specify the base image you chose by using the FROM instruction. For example, if you're using an Ubuntu-based image, the instruction would be: FROM ubuntu:latest.

Install Dependencies: If your application requires any additional dependencies, you can use the RUN instruction to install them. For example, if your application needs Python and pip, you can include the following instructions:

RUN apt-get update && apt-get install -y \
python3 \

Copy Application Files: Use the COPY instruction to copy your application files into the Docker image. Specify the source and destination paths. For example, if your application files are in the current directory, you can use:

COPY . /app

Set the Working Directory: Use the WORKDIR instruction to set the working directory inside the container where subsequent instructions will be executed. For example:


Expose Ports (if needed): If your application listens on a specific port, you can use the EXPOSE instruction to document which ports should be published when running a container based on this image. For example:


Specify the Command to Run: Finally, use the CMD or ENTRYPOINT instruction to define the command that should be executed when a container based on this image is run. For example, if your application is a Python script, you can use:

CMD ["python3", ""]

Save the Dockerfile: Save the Dockerfile in the same directory as your application files.

Once you have created the Dockerfile, you can use the docker build command to build an image based on it. For example:

docker build -t myapp:latest .

This command builds an image named "myapp" with the tag "latest" using the Dockerfile in the current directory.

That's it! You have now created a Dockerfile to define the instructions for building a Docker image for your application.