Docker Series — Cheat Sheet
This is a short cheat sheet of the most important commands to know when working with docker.
Working with containers
Run Container
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG…]
Use docker run to start new containers
https://docs.docker.com/engine/reference/run/
Stop Container
docker stop [OPTIONS] CONTAINER [CONTAINER…]
Use docker stop to stop one or more running containers.
https://docs.docker.com/engine/reference/commandline/stop/
List Containers
docker ps [OPTIONS] [-a]
Use docker ps to list running containers on your machine. Using the -a option will show all containers in any state not just running. Don’t forget when you stop a container it doesn’t always get automatically removed.
https://docs.docker.com/engine/reference/commandline/ps/
Remove Container
docker rm [OPTIONS] CONTAINER [CONTAINER…]
Use docker rm to remove a stopped Docker container. Be careful! Removing a stopped Docker container can cause any data stored within the container to be lost. Do not remove containers unless you are sure the any contained data is either backed up or unnecessary
https://docs.docker.com/engine/reference/commandline/rm/
List Images
docker images [OPTIONS] [REPOSITORY[:TAG]]
Use docker images to list all docker locally downloaded Docker images
https://docs.docker.com/engine/reference/commandline/images/
Remove Image
docker rmi [OPTIONS] IMAGE [IMAGE...]
Use docker rmi remove one or more docker images from your machine
Docker Images
Docker Build
docker build [OPTIONS] PATH | URL | -
Use the docker build command to point to the Dockerfile, name the Docker image, and finally build the Docker image.
We saw this one above when we built the pintail-whoami image.
docker build -t pintail-whoami .
https://docs.docker.com/engine/reference/commandline/build/
Docker List Images
docker images [OPTIONS] [REPOSITORY[:TAG]]
This command is for listing all the docker images currently on your machine. If you want to see all the images stored on your machine.
docker images
https://docs.docker.com/engine/reference/commandline/images/
Docker Tag
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Use this command to add a tag to a docker image. A Docker image tag is a indicator used to differentiate versions of a Docker image. Tags typically will include the version of the code and the base operating system used for the image. For example the official Node.js Docker images are tagged using node:{Node_Version}-{Operating_System}.
For the pintail-whoami image you can run the command below to add a tag to the image.
docker tag pintail-whoami pintail-whoami:CUSTOM_TAG-alpine
Then if you run.
docker images
You should see
REPOSITORY TAG IMAGE ID CREATED SIZE
pintail-whoami CUSTOM_TAG-alpine d797893b23eb 17 seconds ago 458MB
https://docs.docker.com/engine/reference/commandline/tag/
Docker Login
docker login [OPTIONS] [SERVER]
Docker Login is used to login to Docker Hub or a private Docker repository.
https://docs.docker.com/engine/reference/commandline/login/
Docker Push
docker push [OPTIONS] NAME[:TAG]
Docker push is used to push a Docker image to a remote Docker repository. Docker Hub is the default remote Docker repository but if you would like to use a different repository you can include the remote Docker repository url in the tag added to the Docker image.
https://docs.docker.com/engine/reference/commandline/push/
Docker Pull
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Docker Pull is used to pull a specific Docker image from a remote Docker repository. Docker Hub is the default repository unless another is specified in the image name.
https://docs.docker.com/engine/reference/commandline/pull/
Docker Remove Image
docker rmi [OPTIONS] IMAGE [IMAGE…]
This command is used to removing one or more docker images from your machine.
If you want to remove the “pintail-whoami:CUSTOM_TAG-alpine” we created in the Docker Tag section about simply run the command below.
docker rmi pintail-whoami:CUSTOM_TAG-alpine
Docker Compose Commands
Docker Compose Up
docker-compose up [options] [ — scale SERVICE=NUM…] [SERVICE…]
This command is the first one you will use with Docker compose. When you run this command in a directory that contains a docker-compose.yml file Docker compose will find the file and use it to start up the containers.
Options:
--scaleThis setting is used to change the number of containers you would like to have run for a specific service. Simply include the service and the number in the format below and you will be off to the racesdocker-compose up — scale pintail-whoami=2 -d-dThis setting will save you a lot of command line windows and tabs. This setting will tell Docker compose to run in a background daemon rather than locking up your current window.-fThis setting can be used to specify a different Docker compose yml file. Simply use the format below to specify any yml file you would like to use regardless of the name. Without this setting Docker compose will always look for a file named ‘docker-compose.yml’ by default.Docker-compose -f docker-compose-test.yml up -d
https://docs.docker.com/compose/reference/up/
Docker Compose Ps
docker-compose ps [options] [SERVICE…]
Use this command to view all the containers for a specific Docker compose application. This command is similar to the “docker ps” command except it will only show you the images for the current docker-compose.yml file with which you are working.
https://docs.docker.com/compose/reference/ps/
Docker Compose Logs
docker-compose logs [options] [SERVICE…]
This command is used to view the logs for all the Docker containers running for a specific Docker compose application. Use the -f flag to follow the logs as shown below.
Docker-compose logs -f
https://docs.docker.com/compose/reference/logs/
Docker Compose Stop
docker-compose stop [options] [SERVICE…]
This command is used to stop one or more services being run by Docker Compose. This command is similar to the ‘docker stop’ command which will only stop the docker image in its current state. Use this command to stop your Docker Compose application without losing anything. With this command you can always start everything again by simply running ‘docker-compose up -d’ again and all the containers will start again in the same state you left them.
https://docs.docker.com/compose/reference/stop/
Docker Compose Down
docker-compose down [options]
Be very careful with this command. Docker compose down is very destructive and will stop and remove all the container in your Docker compose application without any hope of recovery. Only use this command when you are ready to completely remove all the containers you have been working with including all data within them that isn’t mounted to an external volume.
Docker Swarm & Kubernetes
Coming soon!
Docker Series — What is Docker?
Docker Series — Starting your first container
Docker Series — Creating your first Dockerfile
Docker Series — Building your first Docker image
Docker Series — Moving past one container with Docker Compose
Coming soon!
Docker Series — Moving past one machine with Docker Swarm and Kubernetes