Skip to content

Instantly share code, notes, and snippets.

@yokawasa
Last active December 20, 2022 02:58
Show Gist options
  • Save yokawasa/3754086b57726fb6add550eb4f15967c to your computer and use it in GitHub Desktop.
Save yokawasa/3754086b57726fb6add550eb4f15967c to your computer and use it in GitHub Desktop.
Docker Cheat Sheet

Images manipulation

Build an image

docker build <OPTIONS> <PATH|URL|->

# you can omit "." if a relevant Dockerfile is in the current dir "."
docker build -t <name>
docker build -t <name>:<tag>

# specify Dockefile with -f
docker build -t <name>:<tag> -f <Dockefile path> 

# no cache
docker build -t <name> . -no-cache

List images

docker images 

Delete an Image

docker rmi <name>

Remove all images

sudo docker system prune -a 

Or you can remove one by one

# remove all stopped containers
docker container prune

# remove all networks not used by at least one container
docker network prune

# remove all dangling images
docker image prune

# all dangling build cache
docker volume prune

Interaction with Container registries

Login into a container registry

docker login -u <username>

Tag an image

# re-tag local image with other name
docker tag <existing-name> <username>/<name>[:<tag>]
docker tag fluentd014:1.1 yoichikawasaki/fluentd014:1.1

Publish an image to the registry

docker push <username>/<name>

Search the registry for an image

docker search <name>

Pull an image from the registry

docker pull <name>

Containers manipulation

Run a container

# Create and run a container from an image, with a custom name:
docker run --name <container_name> <image_name>

# Run a container with and publish a specific container’s port(s) to the host
docker run -p <host_port>:<container_port> <image_name>

# Run a container with mounting local volume to the container
docker run -v <local-vol>:<container-vol> -i -t <image_name>[:<tag>] /bin/bash
docker run -v $HOME/docker/logstash-ubuntu1610:/root/work -i -t ubuntu:16.10 /bin/bash

# Run a container in the background
docker run -d <image_name>

# Run a container with shell
# Point is to run with -i (--interactive) and -t (--tty) options. For example, centos:centos6
docker run -it <image_name>[:<tag>] /bin/bash
# Run a container with a specified name
docker run -d -P --name <container_name>  <image_name> /bin/bash

Start, stop, and rm an existing container

docker start <container_name>
docker stop <container_name>
docker rm <container_name>

Open a shell inside a running container

docker exec -it <container_name> sh
# or 
docker container exec -it <container_name> sh

Show the logs of a container

docker logs -f <container_name>

Inspect a running container

docker inspect <container_name> 

To list currently running containers:

docker ps

# list all containers including stopped ones
docker ps -a

# list the latest containers including stopped ones with -l (--latest)
docker ps -l

# show only container id with-q (--quiet) 
docker ps -q

# show without truncating long commands with --no-trunc 
docker ps --no-trunc

View resource usage stats

docker container stats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment