Linux 119: Using Containers with Podman and Docker

Linux 119: Using Containers with Podman and Docker

Containers have revolutionized how applications are built, shipped, and run. They allow you to package applications with their dependencies, ensuring consistency across environments. In this article, we'll explore how to use Docker and Podman—two popular containerization tools on Linux.


1. What Are Containers?

A container is a lightweight, isolated environment that includes everything an application needs to run: code, libraries, and system tools—without the overhead of a full virtual machine.

Key benefits:

  • Consistent deployments
  • Lightweight and fast
  • Easy to scale and replicate


2. Docker vs Podman

FeatureDockerPodman
Daemon-basedYesNo (daemonless)
Rootless modeExperimental/stableFully supported
DockerfileSupportedSupported
Compatible CLIDocker CLIDocker-compatible CLI


Podman is often preferred on systems with strict security needs due to its daemonless and rootless nature.


3. Installing Docker

On Ubuntu/Debian


sudo apt update sudo apt install docker.io sudo systemctl start docker sudo systemctl enable docker

On RHEL/CentOS


sudo yum install docker sudo systemctl start docker sudo systemctl enable docker

Add your user to the docker group:


sudo usermod -aG docker $USER

4. Installing Podman

On Ubuntu/Debian


sudo apt update sudo apt install podman

On RHEL/CentOS/Fedora


sudo dnf install podman

Verify installation:


podman --version

5. Basic Container Usage

Run a Container


docker run hello-world # or podman run hello-world

Run a Web Server


docker run -d -p 8080:80 nginx # or podman run -d -p 8080:80 nginx

List Running Containers

docker ps
# or podman ps

Stop a Container


docker stop <container_id> # or podman stop <container_id>

6. Building Your Own Container Image

Create a file named Dockerfile:


FROM ubuntu:22.04 RUN apt update && apt install -y nginx CMD ["nginx", "-g", "daemon off;"]

Build the image:


docker build -t mynginx . # or podman build -t mynginx .

Run it:


docker run -d -p 8080:80 mynginx

7. Managing Images and Containers

List Images


docker images # or podman images

Remove an Image


docker rmi image_id # or podman rmi image_id

Remove a Container


docker rm container_id # or podman rm container_id

8. Using Volumes for Persistent Storage


docker run -v /mydata:/usr/share/nginx/html -p 8080:80 nginx # or podman run -v /mydata:/usr/share/nginx/html -p 8080:80 nginx

This mounts a local directory (/mydata) inside the container.


9. Rootless Containers with Podman

One of Podman’s major features is the ability to run containers without root privileges:


podman run --rm -it alpine sh

Everything runs under your user’s permissions, reducing risk.


10. Conclusion

Containers with Docker and Podman allow you to package, test, and deploy applications more efficiently. Whether you're running a single container or orchestrating many, understanding the basics of containerization is essential for modern Linux administration.


Next Steps:

  • Learn about container orchestration tools like Kubernetes
  • Explore Docker Compose or Podman pods for multi-container setups
  • Practice building and running your own containerized applications


Coming next: Linux 120: Setting Up a Linux Web Server with Apache and Nginx

Post a Comment (0)
Previous Post Next Post

ads