Docker Cheatsheet


General Commands


Start the docker daemon :

docker -d

Display system-wide information :

docker info

List local images :

docker images

List containers :

docker ps
docker ps -a

Display memory/CPU usage for all containers :

docker container stats

Display disk usage for all images / containers / volumes :

docker system df

Delete all unused images, unused networks and stopped containers :

docker system prune


Images


List all available images :

docker image ls

Build an Image from a Dockerfile :

docker build <image_name> <folder_to_build>
docker build -t <image_name>(:<tag>) <folder_to_build>
docker build -t app:dev .

Delete an Image :

docker image rm <image>

Remove all unused images :

docker image prune


Containers


Create and run a container from an image :

docker run --name <container_name> <image>

Start/stop a container :

docker start|stop <container_name> (or <container-id>)

Run a container with and publish a container’s port(s) to the host :

docker run -p <host_port>:<container_port> <image>

Run a container in background-mode :

docker run -d <image>

Remove a stopped container :

docker rm <container>

Remove all stopped containers :

docker container prune

Open a shell inside a running container :

docker exec -it <container> bash

Display the logs of a container :

docker logs <container>
docker logs -f <container>                    # tail -f
docker logs --tail 100 <container>            # tail -100
docker logs --follow --until=30m              # last 30m
docker logs --since 2023-01-01 <container>    # since 2023/01/01

Copy a local file into a container :

docker cp <local_file> <container>:<target_folder>

Copy a file from a container :

docker cp <container>:<file> <local_destination>

Inspect a running container (full JSON export) :

docker inspect <container>


Docker Compose


Executes a docker-compose.yml file (includes build if containers do not exist yet) :

docker-compose up -d

Start/stop an existing docker-compose :

docker-compose start
docker-compose stop

Executes only 1 specific task from the docker-compose.yml file :

docker-compose pull <task_name>
docker-compose run -d <task_name>
docker-compose up -d <task_name>

Display logs for 1 specific task :

docker-compose logs -f -t --no-log-prefix --tail=500 <task_name>


Docker Hub / Repository


Login into a private repository, to avoid copying SSH keys everywhere !

docker login <private_repo_url>

Login :

docker login -u <username>

Pull an image from a Docker Hub :

docker pull <image_name>

Search Hub for an image :

docker search <image_name>

Publish an image to Docker Hub :

docker push <username>/<image_name>