Docker Cheat Sheet

Deshani Geethika Poddenige
2 min readSep 30, 2018
  1. Create an image from Dockerfile
  2. Push docker image to Docker Hub
  3. Pull docker image
  4. See all the docker images
  5. See all the docker images including intermediate images
  6. Run a docker image
  7. Remove an image
  8. Remove all the images at once
  9. See all the containers
  10. See running containers
  11. Inspect a running container (See the details such as IPAddress)
  12. Start a container
  13. Stop a running container
  14. Stop all the running containers at once
  15. Remove a container
  16. Stop all the running containers at once
  17. Bash into a running container

1. Create an image from Dockerfile

docker build . -t IMAGE_NAME

2. Push docker image to Docker Hub

docker tag IMAGE_ID MY_DOCKER_ID/IMAGE_NAME
Eg: docker tag 7916deb8689c tony/static_scanner
docker push MY_DOCKER_ID/IMAGE_NAME
Eg: docker push tony/static_scanner

3. Pull docker image

docker pull IMAGE_NAME

4. See all the docker images

docker images

5. See all the docker images including intermediate images

docker images -a

When an image is built from a Dockerfile, intermediate layers are created. These intermediate layers increase re-usability, decrease disk usage, and speed up docker build by allowing each step to be cached. When we run docker images -a , we can see all the intermediate images with intermediate layers. These images are shown as <none>:<none> images.

6. Run a docker image

docker run IMAGE_NAME

You don’t have to pull and run an image. Instead, you can use docker run command, and it will pull the image if it is not founded in the local machine, and run.

7. Remove an image

docker rmi IMAGE_NAME

8. Remove all the images at once

docker rmi $(docker images -aq)

When removing an image, you might find an error response like "Error response from daemon: conflict: unable to delete 12c3b9873b07 (must be forced) -image is referenced in multiple repositories” In such a case, --force or -f flag should be used. Therefore, the final command for removing an image would look like as follows

docker rmi -f IMAGE_NAME

9. See all the containers

docker ps -a

10. See running containers

docker ps

11. Inspect a running container (See the details such as IPAddress)

docker inspect CONTAINER_ID/ CONTAINER_NAME

12. Start a container

docker start CONTAINER_ID/ CONTAINER_NAME

13. Stop a running container

docker stop CONTAINER_ID/ CONTAINER_NAME

14. Stop all the running containers at once

docker stop $(docker ps -aq)

15. Remove a container

docker rm CONTAINER_ID/ CONTAINER_NAME

You have to stop a running container before it is removed

16. Remove all the containers at once

docker rm $(docker ps -aq)

17. Bash into a running container

docker exec -it CONTAINER_ID/ CONTAINER_NAME bash

--

--

Deshani Geethika Poddenige

PhD Candidate @Unimelb | Ex-Senior Software Engineer @SyscoLABS | Apache Committer and PMC Member @Apache Allura | Ex-Software Engineering Intern @WSO2