docker cheatsheet

docker is good

#Getting started

#Getting started

Create and run a container in background

$ docker run -d -p 80:80 docker/getting-started
  • -d - Run the container in detached mode
  • -p 80:80 - Map port 80 to port 80 in the container
  • docker/getting-started - The image to use

Create and run a container in foreground

$ docker run -it -p 8001:8080 --name my-nginx nginx
  • -it - Interactive bash mode
  • -p 8001:8080 - Map port 8001 to port 8080 in the container
  • --name my-nginx - Specify a name
  • nginx - The image to use

#General commands

docker psList running containers
docker ps -aList all containers
docker ps -sList running containers
(with CPU / memory)
docker imagesList all images
docker exec -it <container> bashConnecting to container
docker logs <container>Shows container's console log
docker stop <container>Stop a container
docker restart <container>Restart a container
docker rm <container>Remove a container
docker port <container>Shows container's port mapping
docker top <container>List processes
docker kill <container>Kill a container

Parameter <container> can be container id or name


#Starting & Stopping

docker start nginx-serverStarting
docker stop nginx-serverStopping
docker restart nginx-serverRestarting
docker pause nginx-serverPausing
docker unpause nginx-serverUnpausing
docker wait nginx-serverBlocking a Container
docker kill nginx-serverSending a SIGKILL
docker attach nginx-serverConnecting to an Existing Container


docker psList running containers
docker ps -aList all containers
docker logs nginx-serverContainer Logs
docker inspect nginx-serverInspecting Containers
docker events nginx-serverContainers Events
docker port nginx-serverPublic Ports
docker top nginx-serverRunning Processes
docker stats nginx-serverContainer Resource Usage
docker diff nginx-serverLists the changes made to a container.


docker create [options] IMAGE
  -a, --attach               # attach stdout/err
  -i, --interactive          # attach stdin (interactive)
  -t, --tty                  # pseudo-tty
      --name NAME            # name your image
  -p, --publish 5000:5000    # port map (host:container)
      --expose 5432          # expose a port to containers
  -P, --publish-all          # publish all ports
      --link container:alias # linking
  -v, --volume `pwd`:/app    # mount (absolute paths needed)
  -e, --env NAME=hello       # env vars


$ docker create --name my_redis --expose 6379 redis:3.0.2


Renaming a Container

docker rename my-nginx nginx-server

Removing a Container

docker rm nginx-server

Updating a Container

docker update --cpu-shares 512 -m 300M nginx-server



docker imagesListing images
docker rmi nginxRemoving an image
docker load < ubuntu.tar.gzLoading a tarred repository
docker load --input ubuntu.tarLoading a tarred repository
docker save busybox > ubuntu.tarSave an image to a tar archive
docker historyShowing the history of an image
docker commit nginxSave a container as an image.
docker tag nginx eon01/nginxTagging an image
docker push eon01/nginxPushing an image

#Building Images

$ docker build .
$ docker build
$ docker build - < Dockerfile
$ docker build - < context.tar.gz
$ docker build -t eon/nginx-server .
$ docker build -f myOtherDockerfile .
$ curl | docker build -f - .



Removing a network

docker network rm MyOverlayNetwork

Listing networks

docker network ls

Getting information about a network

docker network inspect MyOverlayNetwork

Connecting a running container to a network

docker network connect MyOverlayNetwork nginx

Connecting a container to a network when it starts

docker run -it -d --network=MyOverlayNetwork nginx

Disconnecting a container from a network

docker network disconnect MyOverlayNetwork nginx

#Creating Networks

docker network create -d overlay MyOverlayNetwork

docker network create -d bridge MyBridgeNetwork

docker network create -d overlay \
  --subnet= \
  --subnet= \
  --gateway= \
  --gateway= \
  --ip-range= \
  --aux-address="my-router=" \
  --aux-address="my-switch=" \
  --aux-address="my-printer=" \
  --aux-address="my-nas=" \


#Docker Hub

Docker SyntaxDescription
docker search search_wordSearch docker hub for images.
docker pull user/imageDownloads an image from docker hub.
docker loginAuthenticate to docker hub
docker push user/imageUploads an image to docker hub.

#Registry commands

Login to a Registry

$ docker login
$ docker login localhost:8080

Logout from a Registry

$ docker logout
$ docker logout localhost:8080

Searching an Image

$ docker search nginx
$ docker search nginx --stars=3 --no-trunc busybox

Pulling an Image

$ docker pull nginx
$ docker pull eon01/nginx localhost:5000/myadmin/nginx

Pushing an Image

$ docker push eon01/nginx
$ docker push eon01/nginx localhost:5000/myadmin/nginx

#Batch clean

docker stop -f $(docker ps -a -q)Stopping all containers
docker rm -f $(docker ps -a -q)Removing all containers
docker rmi -f $(docker images -q)Removing all images


Check volumes

$ docker volume ls

Cleanup unused volumes

$ docker volume prune