Skip to content

Docker Cheatsheet

Info

# Docker version information
$ docker --version

# Displays client version and Server version
$ docker version

# More detailed info
$ docker info

# Docker Manual
$ docker --help

# Docker Manual for specific command
$ docker <command> --help

Docker Resources

# List all images locally stored with Docker engine
$ docker images

# List all images locally stored with Docker engine
$ docker image ls

# List all images (including hidden)
$ docker image ls -a

# Delete an image from local image store
$ docker rmi alpine:3.4

Build

# Build an image from Dockerfile in the current directory with a tag
$ docker build -t myapp:1.0

Build Options

# Show the output of the run commands that were not loaded from the cache
# Alternatively use -> export BUILDKIT_PROGRESS=plain
--progress plain

# Rerun steps that have been cached
--no-cache

Ship

# Pull an image from a registry
$ docker pull alpine:3.4

# Retag a local image with a new image name and tag
$ docker tag alpine:3.4 myrepo/myalpine:3.4

# Log into a registry (Docker Hub by default)
$ docker login my.registry.com:8000

# Push an image to a registry
$ docker push myrepo/myalpine:3.4

Run

# Creates a new container from the image and start it
$ docker run <image>

# Start a Dockerized web server
$ docker run -d -p 80:80 --name webserver nginx

# Start an existing container
$ docker start web

# Stop a running container through SIGTERM
$ docker stop web

# Stop a running container through SIGKILL
$ docker kill web

# Kill running containers
$ docker kill $(docker ps -q)

# List the networks
$ docker network ls

# Create an overlay network
$ docker network create --subnet 10.1.0.0/24 --gateway 10.1.0.1 -d overlay mynet

# List the running containers
$ docker ps

# Delete all running and stopped containers
$ docker rm -f $(docker ps -aq)

# Create a new bash process inside the container and connect it to the terminal
$ docker exec -it web bash

# Print the last 100 lines of a container's logs
$ docker logs --tail 100 web

Prune / Cleanup

# single command that will clean up any resources that are dangling
$ docker system prune
    — images, containers, volumes, and networks

# remove any stopped containers & all unused images (not just dangling images)
$ docker system prune -a

Container Commands

DOCKER_HIDE_LEGACY_COMMANDS=true docker --help

docker image ls

# List all the running containers
docker container ls
docker container ls -a

# Run container in an interactive mode
docker container run -it openjdk

# Run container in a detached mode
docker container run -d jboss/wildfly
docker container run -d --name web jboss/wildfly

# # This is to override the default command and run your own
docker container run -it --name web jboss/wildfly bash

# To let the container pick a random port
docker container run -it --name web -P jboss/wildfly

# To let the container pick the port passed in
docker container run -it --name web -p 8080:8080 jboss/wildfly

# Mount local directory on container
docker container run -it --name web -p 8080:8080
    -v `pwd`/webapp.war:/opt/jboss/wildfly/standalone/deployments/webapp.war jboss/wildfly

docker container logs <container_name>
docker container logs unruffled_easley
# Tailing the logs
docker container logs unruffled_easley -f

# Stop the container
docker container stop <container-id>
docker container stop bb655989064b
# Remove the container
docker container rm bb655989064b
# Stop and remove the container in a single command
docker container rm -f bb655989064b

You need to be able to expose ports and attach volumes, so that it can deploy .war files to it.

Reference


Last update: November 14, 2023