Creating a Dockerfile for Custom Images

Tutorial 1 of 5

1. Introduction

The goal of this tutorial is to walk you through the process of creating a Dockerfile to build custom Docker images. Docker images are the basis of containers, which we can run to deploy our applications.

By the end of this tutorial, you'll learn how to:

  • Write a Dockerfile
  • Understand Dockerfile instructions
  • Build a Docker image from the Dockerfile

Prerequisites

This tutorial assumes that you have basic knowledge of how Docker works and that Docker is installed on your system.

2. Step-by-Step Guide

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. To start, create a new file in your project directory and name it "Dockerfile".

Dockerfile Instructions

Here are some commonly used instructions you'll use when writing your Dockerfile:

  • FROM: This instruction initializes a new build stage and sets the base image for subsequent instructions.
  • RUN: This instruction will execute any commands in a new layer on top of the current image and commit the results.
  • CMD: This instruction provides defaults for an executing container.
  • COPY: This instruction copies new files or directories from and adds them to the filesystem of the container at the path .

Best practices and tips:

  • Minimize the number of layers: You should avoid having unnecessary layers in your image to make your image lightweight.
  • Clean up after yourself: Any temporary utilities or dependencies that your Dockerfile installs but are not needed by the application should be removed as part of the same RUN command to reduce the image size.

3. Code Examples

Example 1: Simple Dockerfile

Here is an example of a basic Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.7-slim

# Set the working directory in the container to /app
WORKDIR /app

# Add the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Run app.py when the container launches
CMD ["python", "app.py"]

In this Dockerfile:

  • FROM python:3.7-slim sets the base image to Python 3.7.
  • WORKDIR /app sets the working directory in the container to /app.
  • ADD . /app copies the current directory's contents into the container at /app.
  • RUN pip install --no-cache-dir -r requirements.txt installs the necessary dependencies.
  • EXPOSE 80 opens port 80 for this container to the outside world.
  • CMD ["python", "app.py"] runs the command python app.py when the container starts.

4. Summary

In this tutorial, we've covered how to write a Dockerfile, understand Dockerfile instructions, and build a Docker image from the Dockerfile.

Next, you might want to explore how to push your Docker image to a registry like Docker Hub. You could also learn how to use Docker Compose to manage multi-container applications.

5. Practice Exercises

Exercise 1:

Create a Dockerfile for a Node.js application. Your Dockerfile should copy your application code into the container, install dependencies, and start your application.

Exercise 2:

Create a Dockerfile for a multi-stage build. The first stage should build your application, and the second stage should copy the built application from the first stage and run it.

Exercise 3:

Modify the Dockerfile from exercise 2 to remove any unnecessary files or dependencies after building your application.

Remember, practice is key to mastering any concept, so make sure to try out these exercises and experiment with your Dockerfiles. Happy Dockering!