Dockerfile Basics: Creating a Custom Docker Image

Utsav Desai
5 min readMar 26, 2023

--

What is a Dockerfile?

A Dockerfile is a text document that contains a set of instructions that are used to build a Docker image. It specifies the environment for the application, such as the base image, the operating system, libraries, and other dependencies required to run the application.

The Dockerfile is used as a blueprint for creating Docker images that can be used to run containerized applications. By using a Dockerfile, you can automate the process of building and deploying applications in a consistent and reproducible way across different environments.

Some of the common instructions in a Dockerfile include specifying the base image, copying files to the image, installing dependencies, and setting environment variables. Once the Dockerfile is created, it can be used to build an image using the docker build command, which creates a new image based on the instructions specified in the Dockerfile.

Create a Dockerfile

Creating a Dockerfile is as easy as creating a new file named “Dockerfile” with your text editor of choice and defining some instructions.

# Specify the base image to use
FROM <image>

# Set the working directory inside the container
WORKDIR /app

# Copy the application code from the host machine to the container
COPY <src> <dest>

# Install dependencies using the appropriate package manager
RUN <command>

# Expose a port from the container to the host machine
EXPOSE <port>

# Set environment variables inside the container
ENV <key> <value>

# Define a default command to run when the container starts
CMD <command>

Here’s a breakdown of each command:

  • FROM: Specifies the base image to use for the container.
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies files from the host machine to the container.
  • RUN: Runs a command inside the container to install dependencies or perform other setup tasks.
  • EXPOSE: Exposes a port from the container to the host machine.
  • ENV: Sets environment variables inside the container.
  • CMD: Defines the default command to run when the container starts.

Simple Dockerfile for Python Flask application

Here’s a guide to create a Dockerfile:

  1. Choose a base image: A Dockerfile starts with a FROM instruction that specifies the base image to use for the container. For example, to use a Python 3.9 base image, we can use FROM python:3.9.
  2. Set the working directory: We can use the WORKDIR instruction to set the working directory inside the container. For example, WORKDIR /app will set the working directory to /app.
  3. Copy the application code: We can use the COPY instruction to copy the application code from the host machine to the container. For example, COPY . /app will copy all the files in the current directory to the /app directory in the container.
  4. Install dependencies: If our application has any dependencies, we can use the appropriate package manager to install them inside the container. For example, to install the Flask library for a Python application, we can use RUN pip install Flask.
  5. Expose ports: If our application listens on a specific port, we can use the EXPOSE instruction to expose that port from the container. For example, EXPOSE 5000 will expose port 5000 from the container.
  6. Specify the command to run: We can use the CMD instruction to specify the command to run when the container starts. For example, CMD ["python", "flask", "app.py"] will run the app.py script using the Python interpreter.
# Specify the base image to use
FROM python:3.9

# Set the working directory inside the container
WORKDIR /app

# Copy the application code from the host machine to the container
COPY . /app

# Install dependencies using pip
RUN pip install Flask

# Expose port 5000 from the container to the host machine
EXPOSE 5000

# Set the FLASK_APP environment variable
ENV FLASK_APP=app.py

# Define the default command to run when the container starts
CMD ["flask", "python", "app.py"]

To build the Docker image from this Dockerfile, navigate to the directory that contains the Dockerfile and run the following command:

docker build -t my-flask-app .

This will build the Docker image and tag it with the name my-flask-app.

To run the Docker container from the image, we can use the following command:

docker run -p 5000:5000 my-flask-app

This will start the container and map port 5000 from the container to port 5000 on the host machine.

How to create an optimized Docker image from your dockerfile

To create an optimized Docker image from your Dockerfile, you can follow these best practices:

  1. Use a small base image: Choose a small base image like Alpine or BusyBox to reduce the size of the final Docker image.
  2. Avoid installing unnecessary packages: Only install the required packages and dependencies for your application to reduce the size of the Docker image.
  3. Use multi-stage builds: Use multi-stage builds to separate the build environment from the final runtime environment. This reduces the size of the final Docker image by removing build-time dependencies.
  4. Clean up after installation: Remove any temporary files or cache created during the installation process.
  5. Use COPY instead of ADD: Use the COPY instruction instead of ADD as ADD has extra features that are not always required and can increase the size of the Docker image.
  6. Set environment variables: Set environment variables to optimize the performance and memory usage of your application.
  7. Minimize layers: Minimize the number of layers in your Dockerfile to reduce the build time and size of the final Docker image.

--

--

Utsav Desai
Utsav Desai

Written by Utsav Desai

Utsav Desai is a technology enthusiast with an interest in DevOps, App Development, and Web Development.

No responses yet