Open In App

What Is Dockerfile Extension ?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Docker is an open-source containerization platform. It helps the developers automate the process-related deployment that enables demand-based scaling, and easy management of applications. Dockerfile enables docker to build images and spin containers from those images. In this article, we’ll understand Dockerfile, extensions, syntax, and sample dockerfile.

What Is Dockerfile Extension?

A Dockerfile is a simple text file that contains instructions for building a Docker image. It’s like a recipe. Each line in the Dockerfile represents a command. When you run the Docker build command, it reads the Dockerfile and generates a Docker image. This image can then be used to launch new containers. Unlike traditional files that have an extension at the end, like report.pdf, draft.docx, and image.jpg. Traditionally, The docker file has no extension. It simply named Dockerfile without any extension type.

Additionally, Dockerfiles can be created using the .dockerfile extension. This is used when there is a need to store multiple docker files in one single directory.

Image Showing Relation b/w Dockerfile, Image & Container

Dockerfile with no extension VS file with .dockerfile extension

There is no difference between a Dockerfile with no extension and a file with a .dockerfile extension. They both serve the same purpose to build Docker images. Using .dockerfile is useful when working with different docker files in the same project. These different docker files could be used for different environments as tests. dockerfile, dev. dockerfile, uat. dockerfile, prod. dockerfile.

Creating and Using Dockerfile

Using File Explorer and Notepad

Navigate to a folder of your choice. Right click and select New > Text Document

Dockerfile Creation

Rename the file to Dockerfile and remove the extension. Confirm the prompt

Prompt to change the file extension

Using Dockerfiles in VS Code

Opening the same folder in VS Code, we can see that our Dockerfile has a docker logo with it.

VS Code identified Dockerfile

If we want to create a Dockerfile from VS Code, just open the explorer pane and click on the new file. This file could be named dev.dockerfile.

Creating a New Dockerfile

Additionally, VS Code provides features like syntax highlighting, linting, and IntelliSense to dockerfiles. If the file has a .dockerfile extension or is name Dockerfile, VS Code automatically recognizes it as a Dockerfile and provides these features.

Syntax of a Dockerfile

Each line of the dockerfile is just a command, these comamnds need to follow a certain syntax and pattern. There are a lot of dockerfile commands that can be used to create a docker image. Some of the commonly used commands to write dockerfiles are

  • FROM: To choose a base layer which acts as a starting point.
  • RUN: To perform a task on the container.
  • CMD: To set the default behavior when the container is run.
  • EXPOSE: Ports that are exposed to be used.
  • COPY: Copy files from the host os file system to the image file system.
  • VOLUME: Shared folder space to store and exchange data.
  • USER: To set the user’s access and privilage management.
  • WORKDIR: From where the commands are executed.

Example Dockerfile

This is a very simple Dockerfile that can be used to run a python script inside a container.

# Choose python as base layer

FROM python:latest

# Set the working directory

WORKDIR /app

# Copy the Python script

COPY script.py .

# Run the Python script when the container is run

CMD [“python”, “./script.py”]

Dockerfile Extension – FAQs

Why doesn’t Dockerfile have an extension?

When building an image, docker just looks for “Dockerfile” and it doesn’t really need an extension. This also helps to keep it easily recognizable and consistent.

Can I give Dockerfile an extension?

Yes. Both Dockerfile without extension and a file with .dockerfile extension are valid dockerfiles that can be used to build images.

Can I have more than one Dockerfile in a project?

Yes, There can be multiple Dockerfiles in a project. Generally, one dockerfile should be sufficient but there might be cases where test, dev and prod each has its own Dockerfile.

How do I use a Dockerfile to build an image?

You can use the docker build command to create images. You can read more about building docker images here.

Can I comment in a Dockerfile?

Yes. Comments can be easily added to dockerfile using # at the start of a line. This is similar to python comments.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads