10/25/2017 - 11:59 AM

Docker cheat sheet

Docker cheat sheet

#fetch and build container
docker-compose build

#start container
docker-compose up -d

# Run a container app
docker container run <options like -it> <image:tag> <app>

## Some options
--name name of the container to use
-it interactive mode and connect the container TTY to the current terminal
-d run as daemon in the background, the current terminal does not get connected to the container
-p host_port:container_port maps a given host port to a container port

# Run container with a name
docker container run --name <the name to use> -it image:tag <app>

# Run container with restart policy (always | unless-stopped | on-failure)
docker container run --name neverdie -it --restart unless-stopped ubuntu bash

#star stop container
docker stop/start/restart container_id

#Open terminal in container
docker exec -i -t container_id /bin/bash

# Open bash from an image
docker run --rm -it --entrypoint bash <image-name-or-id>

#List all containers(only IDs)
docker ps -aq

#Stop all running containers
docker stop $(docker ps -aq)

# Inspect a container to see for example which command it runs as default
docker container inspect <container_id or name>

#Leave container without terminating it
Ctrl + pq

#Remove all containers
docker rm $(docker ps -aq)

#Remove all images
docker image rm $(docker image ls -q) -f

#Check docker container space
docker system df -v

#See all layers in an image
docker image inspect name:tag

#Pull image by digest
docker image pull image_name@sha256:image_digest

#List the SHA256 of the images
docker images --digests

# Check if daemon is running (std docker only, snap is not working).
sudo systemctl is-active docker

# See docker commands in an image (like Dockerfile)
docker image history <image:version>

# See networks configured
docker network ls

# See volumes mounted
docker volume ls

# See where the volume is mounted in the host
docker volume inspect <name_volume obtained with docker volume ls> | grep Mount

# Get container ID of a given running image
docker ps --filter ancestor=jenkins/jenkins:lts --format "{{.ID}}"

# Prune all volumes not used
docker volume prune 

# Check distro running in linux docker container
docker exec containerId cat /etc/os-release