This guide covers managing and configuring containers using Docker and Podman, including installation, image handling, and container operations.
Welcome to this comprehensive guide on container management and configuration. In this article, we explore how containers simplify application deployment and migration by encapsulating everything—daemons, configuration files, logs, and databases—in a single, portable unit. Unlike traditional setups (e.g., a conventional MariaDB installation where components are scattered in various directories), containerized applications streamline the process of moving applications between different systems.
In some environments, such as CentOS Stream 8, Docker might not have official support. In these cases, you can install Podman, which offers a Docker-compatible command-line interface. To install Podman using the dnf package manager, execute the following command:
Copy
Ask AI
sudo dnf install podman
After installation, Podman allows you to use familiar Docker commands as it seamlessly translates them under the hood. The installation output might resemble the following:
To pull the official Nginx image, use its fully qualified name:
Copy
Ask AI
docker pull docker.io/library/nginx
For convenience, you can also use the short form:
Copy
Ask AI
docker pull nginx
To pull a specific version (for instance, version 1.20.2), run:
Copy
Ask AI
docker pull nginx:1.20.2
After pulling an image, you can list the available images with:
Copy
Ask AI
docker images
Example output:
Copy
Ask AI
REPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/library/nginx 1.20.2 8f34c303855f 17 hours ago 146 MBdocker.io/library/nginx latest 12766a6745ee 17 hours ago 146 MB
If you wish to remove a specific version, execute:
Copy
Ask AI
docker rmi nginx:1.20.2
Images can also be referenced by their IMAGE ID, using just enough characters to uniquely identify them.
To create and run a new container using the Nginx image, use:
Copy
Ask AI
docker run nginx
This command creates a container and attaches your terminal to its output. If you find that the container’s logs (for example, startup messages from /docker-entrypoint.sh) continuously appear, press Ctrl+C to detach and terminate the container.To run the container in detached mode, use the -d option:
Copy
Ask AI
docker run -d nginx
This command returns a hexadecimal container ID and allows the container to run in the background.
For improved container management, you can assign custom names and set up port mapping between the host and container. To run Nginx in a container named mywebserver with host port 8080 mapped to container port 80, use:
Copy
Ask AI
docker run -d -p 8080:80 --name mywebserver nginx
This configuration directs any connection to port 8080 on your machine to port 80 inside the container. To test the setup, you can use netcat:
Copy
Ask AI
nc localhost 8080
After connecting, type the following command:
Copy
Ask AI
GET /
Then press Enter. This simulates a browser request to Nginx, displaying the default HTML page. Press Ctrl+C to exit the netcat session.Note: Mapping to privileged ports (ports below 1024) requires root privileges. For example, to map host port 80 to container port 80, use:
Copy
Ask AI
sudo docker run -d -p 80:80 --name mywebserver nginx
For detailed information about any Docker command, append the --help option. For example:
Copy
Ask AI
docker container --help
Or to get help for a specific command like docker rm:
Copy
Ask AI
docker rm --help
Below is an example of help output for Podman’s container removal command:
Copy
Ask AI
podman rm [options] CONTAINER [CONTAINER...]Examples: podman rm imageID podman rm mywebserver myflaskserver 860a4b23 podman rm --force --all podman rm -f c684f0d469f2Options: -a, --all Remove all containers --cidfile stringArray Read the container ID from the file --depend Remove container and all containers that depend on the selected container -f, --force Force removal of a running or unusable container -i, --ignore Ignore errors when a specified container is missing -l, --latest Act on the latest container podman is aware of -t, --time uint Not supported with the "--remote" flag Seconds to wait for stop before killing the container -v, --volumes Remove anonymous volumes associated with the container
This guide demonstrated the key steps in managing and configuring containers on Linux using Docker and Podman. We covered installing Podman, configuring its default registry, working with images, running containers in both attached and detached modes, and advanced topics like port mapping and naming containers. Continue exploring these concepts to elevate your container management skills.Happy containerizing!