Post

Docker Containers and Management Reference

A primer to get setup with docker and docker compose on ubuntu/debian linux.

Docker Containers and Management Reference

Docker

Ubuntu Docker Setup

See the official instructions to install Docker.

  1. Setup the apt repository
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Add Docker's official GPG key:
sudo apt update
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF

sudo apt update
  1. Install the docker packages
1
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

info

Verify Docker is running or manually start if needed

1
2
sudo systemctl status docker
sudo systemctl start docker
  1. Check the install with hello world
1
sudo docker run hello-world
  1. Post install step to have docker run without root privileges.
    1. Create the docker group if it doesn’t exist sudo groupadd docker
    2. Add your user to the docker group sudo usermod -aG docker $USER
    3. Verify with hello world docker run hello-world

Ubuntu Docker Compose Setup

Install docker-compose (see the official instructions).

1
2
3
sudo apt-get update
sudo apt-get install docker-compose-plugin
docker compose version

Docker Compose

COMMANDDESCRIPTION
docker-compose startStarts existing containers for a service
docker-compose stopStops running containers without removing them
docker-compose pausePauses running containers of a service
docker-compose unpauseUnpauses paused containers of a service
docker-compose psLists containers
docker-compose upBuilds, (re)creates, starts, and attaches to containers for a service
docker-compose downStops containers and removes containers, networks, volumes, and images created by up

Docker

Running Containers

COMMANDDESCRIPTION
docker run <image>Start a new container from an image
docker run -it <image>Start a new container in interactive mode
docker run --rm <image>Start a new container and remove it when it exits
docker create <image>Create a new container
docker start <container>Start a container
docker stop <container>Graceful stop a container
docker kill <container>Kill (SIGKILL) a container
docker restart <container>Graceful stop and restart a container
docker pause <container>Suspend a container
docker unpause <container>Resume a container
docker rm <container>Destroy a container

Container Bulk Management

COMMANDDESCRIPTION
docker stop $(docker ps -q)To stop all the running containers
docker stop $(docker ps -a -q)To stop all the stopped and running containers
docker kill $(docker ps -q)To kill all the running containers
docker kill $(docker ps -a -q)To kill all the stopped and running containers
docker restart $(docker ps -q)To restart all running containers
docker restart $(docker ps -a -q)To restart all the stopped and running containers
docker rm $(docker ps -q)To destroy all running containers
docker rm $(docker ps -a -q)To destroy all the stopped and running containers
docker pause $(docker ps -q)To pause all running containers
docker pause $(docker ps -a -q)To pause all the stopped and running containers
docker start $(docker ps -q)To start all running containers
docker start $(docker ps -a -q)To start all the stopped and running containers
docker rm -vf $(docker ps -a -q)To delete all containers including its volumes use
docker rmi -f $(docker images -a -q)To delete all the images
docker system pruneTo delete all dangling and unused images, containers, cache and volumes
docker system prune -aTo delete all used and unused images
docker system prune --volumesTo delete all docker volumes

Inspect Containers

COMMANDDESCRIPTION
docker psList running containers
docker ps --allList all containers, including stopped
docker logs <container>Show a container output
docker logs -f <container>Follow a container output
docker logs -f <container> 2>&1 \| grep string-to-searchFollow container logs and search for specific string occurrence
docker top <container>List the processes running in a container
docker diffShow the differences with the image (modified files)
docker inspectShow information of a container (json formatted)

Executing Commands

COMMANDDESCRIPTION
docker attach <container>Attach to a container
docker cp <container>:<container-path> <host-path>Copy files from the container
docker cp <host-path> <container>:<container-path>Copy files into the container
docker export <container>Export the content of the container (tar archive)
docker exec <container>Run a command inside a container
docker exec -it <container> /bin/bashOpen an interactive shell inside a container (there is no bash in some images, use /bin/sh)
docker wait <container>Wait until the container terminates and return the exit code

Images

COMMANDDESCRIPTION
docker image lsList all local images
docker history <image>Show the image history
docker inspect <image>Show information (json formatted)
docker tag <image> <tag>Tag an image
docker commit <container> <image>Create an image (from a container)
docker import <url>Create an image (from a tarball)
docker rmi <image>Delete images
docker pull <user>/<repository>:<tag>Pull an image from a registry
docker push <user>/<repository>:<tag>Push and image to a registry
docker search <test>Search an image on the official registry
docker loginLogin to a registry
docker logoutLogout from a registry
docker save <user>/<repository>:<tag>Export an image/repo as a tarball
docker loadLoad images from a tarball

Volumes

COMMANDDESCRIPTION
docker volume lsList all vol1umes
docker volume create <volume>Create a volume
docker volume inspect <volume>Show information (json formatted)
docker volume rm <volume>Destroy a volume
docker volume ls --filter="dangling=true"List all dangling volumes (not referenced by any container)
docker volume pruneDelete all volumes (not referenced by any container)
docker run --rm --volumes-from <container> -v $(pwd):/backup busybox tar cvfz /backup/backup.tar.gz <container-path>Backup a container
docker run --rm --volumes-from <container> -v $(pwd):/backup busybox sh -c "cd <container-path> && tar xvfz /backup/backup.tar.gz --strip 1"Restore a container from backup
This post is licensed under CC BY 4.0 by the author.