Skip to content

Instantly share code, notes, and snippets.

@kingand
Last active March 12, 2023 19:29
Show Gist options
  • Save kingand/307e605c81f9ded343be220774c66ed3 to your computer and use it in GitHub Desktop.
Save kingand/307e605c81f9ded343be220774c66ed3 to your computer and use it in GitHub Desktop.
List of common Docker commands

Docker Cheatsheet

The purpose of this Gist is to provide a quick list of common Docker commands. This information was pulled from the summary at the bottom of Part 2 of the official Docker tutorial: https://docs.docker.com/get-started/part2

Running containers

Run "friendlyname" mapping port 4000 to 80

$ docker run -p 4000:80 friendlyhello

Same thing, but in detached mode

$ docker run -d -p 4000:80 friendlyhello

Run image from a registry

$ docker run username/repository:tag

List all running containers

$ docker container ls

List all containers, even those not running

$ docker container ls -a

Gracefully stop the specified container

$ docker container stop <hash>

Force shutdown of the specified container

$ docker container kill <hash>

Remove specified container from this machine

$ docker container rm <hash>

Remove all containers

$ docker container rm $(docker container ls -a -q)

Creating container images

Create image using this directory's Dockerfile

$ docker build -t friendlyhello .

List all images on this machine

$ docker image ls -a

Remove specified image from this machine

$ docker image rm <image id>

Remove all images from this machine

$ docker image rm $(docker image ls -a -q)

Publishing container images

Log in this CLI session using your Docker credentials

$ docker login

Tag <image> for upload to registry

$ docker tag <image> username/repository:tag

Upload tagged image to registry

$ docker push username/repository:tag

Creating and managing volumes

Create a new volume

$ docker volume create my-volume

View list of existing volumes

$ docker volume ls

View details of a specific volume

$ docker volume inspect my-volume

Remove a volume

$ docker volume rm my-volume

Using volumes in containers

Mount a volume when starting a container (create if not exist)

$ docker run -d --name my-container --mount type=volume,source=my-volume,target=/mnt/my-volume,ro my-container-image

Removing a volume from a container

$ docker volume rm my-container

Using bind mounts

$ docker run -d --name my-container --mount type=bind,source="$(pwd)"/my-directory,target=/mnt/my-directory,ro my-container

References

  1. https://docs.docker.com/get-started/part2
  2. https://docs.docker.com/storage/volumes/
  3. https://docs.docker.com/storage/bind-mounts/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment