Software Testing

Build a Simple Docker Image

Build a Simple Docker Image

Docker makes it easy to package an application and its dependencies into a container, which can then run consistently across different environments. In this guide, we’ll show you how to build a simple Docker image from scratch.

In this guide, you will:

  • Create a simple Python application (app.py).
  • Write a Dockerfile to define how to build the Docker image.
  • Built the Docker image
  • Run it as a container.

A Docker Image is like a blueprint or template used to create Docker containers. It is a read-only file that contains everything needed to run an application, including:

  • The application code
  • Runtime (like Python, Java, Node.js, etc.)
  • Libraries and dependencies
  • Configuration files
  • Operating system files (minimal, only the required parts)

When you run a Docker Image, it becomes a Docker Container €“ an actual running instance of that image.

Create Your Application

For this tutorial, let’s build a very basic Python application. First, create a directory for your project:

$ mkdir docker-app

Now, go into the directory:

$ cd docker-app

Create a Python file called app.py with the following content:


    # app.py
    print("Hello, Docker!")
    

Create a Dockerfile

A Dockerfile is a text file that contains instructions on how to build your Docker image. In the same directory as your app.py file, create a file named Dockerfile (no extension).

$ touch Dockerfile

Here’s what the Dockerfile should look like:


    # Use the official Python base image
    FROM python:3.9-slim

    # Set the working directory inside the container
    WORKDIR /app

    # Copy the Python application into the container
    COPY app.py /app

    # Run the Python application
    CMD ["python", "app.py"]

Create Dockerfile

Let’s break down what each line does:

  • FROM python:3.9-slim: This line tells Docker to use a slim version of Python 3.9 as the base image.
  • WORKDIR /app: This sets the working directory inside the container to /app.
  • COPY app.py /app: This copies your app.py file from your local machine into the container’s /app directory.
  • CMD [“python”, “app.py”]: This tells Docker to run the Python application when the container starts.

Build the Docker Image

Now that you have your Dockerfile, it’s time to build the Docker image. Open your terminal or command prompt and navigate to the directory where your Dockerfile is located. Run the following command to build the image:

$ sudo docker build -t my-docker-app .

Explanation of the command:

  • docker build: This tells Docker to build an image.
  • -t my-docker-app: This tags the image with the name my-docker-app.
  • .: The dot represents the current directory (where the Dockerfile is located).

Once the build process is complete, you’ll see a message that indicates the image was created successfully.

Run the Docker Container

Now that you have built the Docker image, you can run it as a container. To do so, use the following command:

$ sudo docker run my-docker-app

This will run your container and execute the app.py Python script inside the container. You should see the output:

Hello, Docker!

Build a Simple Docker Image

 

That’s it!

Now you know how to build and run a simple Docker image. You can now apply this knowledge to containerize more complex applications.