Fundamentals of Docker Containers and Images

With Docker, you can run applications inside isolated containers that operate independently of the environment, delivering reproducible results. Docker images serve as the foundation for containers and include all dependencies of an application.

Bernd Fischer

Bernd Fischer

03. Nov 2024 · 4 min read

Fundamentals of Docker Containers and Images

Getting Started with Docker

Docker is a powerful tool that isolates applications within so-called containers and runs them independently of the underlying infrastructure. Containers are lightweight, portable units that enable applications to run consistently with all their dependencies. In the following tutorial, you will learn what Docker containers and images are, and get to know basic Docker commands for managing containers.

What Are Containers and Images?

A container is an isolated environment in which an application and all its dependencies can run. Containers are based on Docker images, which act like templates or blueprints for the container. An image contains everything needed to run the application, including the operating system, libraries, and application files.

Starting a container from an image creates an instance of the application that runs independently from other containers and the host system’s environment. Containers are therefore an ideal solution for consistent and portable applications.

Important Docker Commands

Running Docker Containers

To start a Docker container, you can use the command docker container run. Here is a simple example to run an NGINX web server on host port 8000:

docker container run --publish 8000:80 nginx

This command starts a new container from the NGINX image and maps port 8000 on the host to port 80 inside the container, where the web server runs. The option --publish (or -p) enables the port mapping between the host and container.

Running in Background and Specific Image Versions

To run a container in the background, use the option -d (detached mode). Here is an example that runs a specific version of NGINX (1.11) in the background. The option -T also outputs the NGINX configuration in the terminal:

docker container run --publish 8000:80 -d nginx:1.11 nginx -T

This runs the container based on version 1.11 of the NGINX image in the background. Additionally, the container outputs the NGINX configuration to the console.

Starting and Stopping Containers

To stop a running container, use docker container stop. Specify the container ID or container name to stop the desired container:

docker container stop <CONTAINER_ID>

To start a stopped container again, you can use docker container start:

docker container start <CONTAINER_ID>

These commands allow you to pause a container when needed and restart it later.

Viewing Container Processes

To see the active processes inside a running container, use docker container top:

docker container top <CONTAINER_ID>

This command shows the currently running processes within the container and is useful to verify if the application is running as expected.

Monitoring Container Resource Usage

Docker provides the command docker container stats to monitor the resource consumption of one or multiple containers. The command shows information like CPU and memory usage in real time:

docker container stats <CONTAINER_ID>

This command is especially useful for performance monitoring and identifying containers that might be consuming too much memory or CPU.

Viewing Container Logs

To view the logs of a container, use the command docker container logs. This is useful to check for application errors or output:

docker container logs <CONTAINER_ID>

With this command, you can view the container's output as if you were accessing the container's terminal directly.

Creating a Docker Image from a Container (e.g., with NGINX)

In Docker, you can create an image directly from a running container. This allows you to incorporate changes made within a container into a new image for reuse. Here we show how to create a Docker image from a container using NGINX.

Step 1: Start and Adjust Container with NGINX

Start a container with NGINX and make all desired adjustments inside. For example, you can start the container and enter its shell to make changes.

docker container run -d --name mein_nginx_container nginx

In this example, a new NGINX container is started in the background with the name mein_nginx_container.

If you want to make changes directly inside the container, open the shell:

docker exec -it mein_nginx_container /bin/bash

You can now make changes such as adjusting the NGINX configuration, adding new files, or editing existing ones.

Step 2: Save Changes into an Image

After making the desired adjustments inside the container, you can create an image from it. The command docker commit saves the container state as an image.

docker commit mein_nginx_container mein_nginx_image

In this example, Docker creates a new image named mein_nginx_image, based on the current state of mein_nginx_container.

Step 3: Verify the New Image

After creating the image, you can check if it appears in your list of Docker images.

docker images

The new image mein_nginx_image should now appear in the list. It is a copy of the original container including all adjustments you made.

Step 4: Test the Image

To ensure the image works as expected, you can start a new container from it and verify the configuration.

docker container run -d --name test_nginx_container mein_nginx_image

This command starts a new container named test_nginx_container based on the created image mein_nginx_image. Check if your adjustments have been applied as intended by accessing the container or testing the NGINX configuration.

Alternative Approach: Image Creation Using Dockerfile

A commonly used approach to create a Docker image is to write a Dockerfile, which defines the configuration. This is the preferred method as it allows repeatable builds and better version control. A simple Dockerfile for an NGINX server might look like this:

# Verwende das Basis-Image nginx
FROM nginx:latest

# Kopiere eine benutzerdefinierte Konfigurationsdatei in den Container
COPY nginx.conf /etc/nginx/nginx.conf

# Exponiere den NGINX-Port
EXPOSE 80

Build an image from this Dockerfile:

docker build -t custom_nginx_image .

A Dockerfile is a flexible and recommended method for image building because it automates the build process and is easy to version control.