Docker and Podman CheatSheet for DevOps

Docker and Podman are both tools that allow developers to package applications and their dependencies into isolated environments called containers. These containers are portable and can be deployed easily on any system, making them a popular choice for modern software development.

In this cheatsheet, we will cover the basics of Docker and Podman, including how to build, run, and manage containers, as well as how to work with volumes to persist data. You can use the same commands both for Podman and Docker.

Docker Installation

Install Docker on Linux

curl -sSL https://get.docker.com/ | sh

Start Docker

service start docker

Stop Docker

service stop docker

Docker Lifecycle

Pull (Download) Docker Image fedora

docker pull fedora

Search image fedora

docker search fedora

Run Image Fedora and assign the name “myfedora”

docker run --name myfedora fedora

Run image and removes container when stopped

docker run --rm fedora

Tag the image simple-server as simple-server:0.1

docker image tag simple-server simple-server:0.1

Run a shell on the Container myfedora:

docker exec -it myfedora /bin/bash

Runs a shell on the latest container started:

docker exec -it $(docker ps -q) bash

Build an image on the current directory from a Dockerfile

docker build .

Delete an image

docker rmi <myimage>

Attach to a running Container

docker attach <container>

See the logs of a Container

docker logs <container>

List active containers

docker ps -l

List active running Images

docker ps --format '{{.Image}}'

List available images

docker images

How to fetch only one attribute (f.e. Repository) from the Image List:

docker image ls --format "{{.Repository}}"

How to check all the layers (and their size) of an image

docker image tree my-image

How to stop all containers

docker stop $(docker ps -a -q)

Volumes

Creates a named Volume

docker volume create postgres-vol

How to import a Tar file into a volume (postgres-vol)

podman volume import postgres-vol ~/files/postgres-vol.tar.gz

How to delete a volume

docker volume rm postgres-vol

Start a container mounting a host directory (/home/jboss/deployments) as a volume in the WildFly container

docker run --rm --name wildfly --volume /home/jboss/deployments:/opt/jboss/wildfly/standalone/deployments:Z quay.io/wildfly/wildfly

Start a container mounting a Host directory as named volume in the PostgreSQL Container

docker run --name persisting-db -d --volume postgres-vol:/var/lib/pgsql/data postgresql-13

Ports and IPs

How to collect Socket Statistics from a Container

# First get the Container process ID (PID) for the Container myfedora
podman inspect myfedora --format '{{.State.Pid}}'

# Run the the nsenter command against the PID
sudo nsenter -t PID -n ss -pant

How to map an Host port (8080) with a Container port (8001):

podman run -d -p 8080:8001 --name http-server simple-server

Check mapped ports for the container “http-server”

docker port http-server

Get the IP Address of the last Container started:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' $(docker ps -q)

Check any container’s IP Address:

docker inspect -f '{{ .NetworkSettings.IPAddress }}' http-server

Link Containers

Step 1: Start first CONTAINER

docker run --name CONTAINER

Step 2: Start LINKED container

docker run -d --link CONTAINER:ALIAS --name LINKED <myimag>

Cleanup

Kill running containers

docker kill $(docker ps -q)

Delete old containers

docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs -r docker rm

Delete stopped containers

docker rm -v `docker ps -a -q -f status=exited`

Delete dangling images

docker rmi $(docker images -q -f dangling=true)

Delete all images

docker rmi $(docker images -q)

Conclusion

In this cheatasheet, we’ve covered some of the most important Docker commands and options, including building images, running containers, managing volumes and networks, and doing regular maintenance of your images.

Found the article helpful? if so please follow us on Socials