Others

2023/04/02

Docker Cheat Sheet

Running and maintance your first Docker container (Centos, Postgres)


Docker Cheat Sheet

Introduction

This Docker cheat sheet provides a quick reference for various Docker commands and configurations to streamline your containerization workflow.

Legend: var nameVariable = container name;

Installation

Install Docker on CentOS: https://technixleo.com/install-docker-ce-on-centos-almalinux/

Mounting Images

Setting up PostgreSQL with Docker: https://www.baeldung.com/ops/postgresql-docker-setup
Running Containers:

docker run -ti -d --net bridge5 --name pgadmin -p 5432:80 -e "PGADMIN..." -e "PGADMIN..." -d dpage/pgadmin4

-p 81:80 Maps container port 80 to Docker host port 81; access the service at dockerhost:81.
-ti Terminak interactive. Allocate a pseudo-TTY, keeping STDIN open even if not attached.
-d Run in detached mode, in the background.
--net network_interface_name: Use a specific network interface.
--name: Assign a name to the container.
-e Set environment variables.
Detached mode means that the container will run in the background without being attached to any input or output stream.

Administering Containers

Displaying Images/Containers, Status

Show all downloaded images: docker images
Show running: docker container ls
All (including stopped): docker container ls -a
Note: docker ps -a is an older, unstructured version of the command.

Start/stop

Start a container: docker start nameVariable
Stop a container: docker stop nameVariable

Connecting to container Shell

docker exec -it nameVariable /bin/bash

Deletion

docker rm nameVariable
Remove all containers: docker rm $(docker ps -a -q)

Verify Container Properties with Inspect

Check IP & Socket

Using grep:

docker inspect nameVariable | grep '"IPAddress"' | head -n

Or using a more elegant way:

docker inspect -f "(( .NetworkSettings.IPAddress ))" nameVariable

Check logs:

docker inspect --format='((.LogPath))' nameVariable

Check used image details:

docker inspect --format='((.Config.Image))' nameVariable

Reference: Docker [Run Command Documentation (https://docs.docker.com/engine/reference/run/)]

Bonus

Daemon Debug

sudo dockerd --debug: Run the daemon with debug output (default location of the configuration file on Linux is /etc/docker/daemon.json)

Centos Network Commands

CentOS network commands:
nmcli Configure network on the host
ip a Show IP
docker network ls Manage networks
docker network inspect <networkname> Display detailed information on one or more networks
docker attach pgadmin Attach local standard input, output, and error streams to a running container.