Building Containers - docker/getting-started

Building Containers

In this document, we will demonstrate how to create custom container images using Dockerfiles. We will cover the basics of image building and discuss possible options with examples.

Getting Started

Before we begin, it is important to note that building containers can be slow, especially on systems with limited resources. For instance, a Raspberry Pi Model B with 512 MB of memory may take longer to build containers compared to more powerful systems (https://opensource.com/life/15/9/experimenting-docker-raspberry-pi).

What Makes a Good Container

When building containers, it is important to keep in mind the following requirements:

  • Ability to keep it up to date
  • Reproducible builds
  • No compilers in production
  • Stay reasonably small

(https://opensource.com/article/21/11/containers-python)

Terminology

Here are some key terms to understand when working with containers:

  • Container: A running instance of a container image.
  • Image: A lightweight, stand-alone, and executable package that includes everything needed to run a piece of software.
  • Image layer: A filesystem change in a container image.
  • Registry: A service that stores and distributes container images.
  • Repository: A collection of related container images in a registry.
  • Tag: A label applied to a container image to differentiate between multiple images in a repository.
  • Base image: A foundational image that can be built upon through the addition of image layers.

(https://developers.redhat.com/blog/2018/02/22/container-terminology-practical-introduction)

Creating a Container Image

To create a container image, you need to start with a base image. A base image is a foundational image that can be built upon through the addition of image layers. Typically, base images come pre-bundled with a specific Linux distribution.

Here is an example of a Dockerfile that uses the python base image to create a container image for a simple Python application:

# 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"]

(https://docs.docker.com/language/python/develop/)

In this example, the Dockerfile uses the python:3.7-slim base image and sets the working directory to /app. It then adds the contents of the current directory to the container and installs any needed packages specified in requirements.txt. Finally, it exposes port 80 and specifies that app.py should be run when the container launches.

Building the Container Image

To build the container image, run the following command in the same directory as the Dockerfile:

$ docker build -t my-python-app .

This command tells Docker to build an image using the Dockerfile in the current directory and tag it with the name my-python-app.

Running the Container

To run the container, use the following command:

$ docker run -p 4000:80 my-python-app

This command tells Docker to run the my-python-app container and map port 4000 on the host to port 80 on the container.

Conclusion

In this document, we have demonstrated how to create a custom container image using a Dockerfile. We have covered the basics of image building and discussed possible options with examples. By following the steps outlined in this document, you should be able to create and run your own container images.

Additional Resources