Skip to content

Instantly share code, notes, and snippets.

@anhphamt
Last active December 24, 2020 01:02
Show Gist options
  • Save anhphamt/821e997b138faec9a6166df8ef2339df to your computer and use it in GitHub Desktop.
Save anhphamt/821e997b138faec9a6166df8ef2339df to your computer and use it in GitHub Desktop.
Top useful docker commands

Top 16 docker commands

To remove all images which are not used by existing containers

docker image prune -a

List running containers.

docker ps  

List all container including stopped container

docker ps -a 

Download a image from Docker Hub registry. Link to the docker image is always shown on the right at dockerhub.

docker pull

Build your own container based on a Dockerfile. Common use is docker build . to build a container based on the Dockerfile in the current directory (the dot). docker build -t "myimage:latest" . creates a container and stores the image under the given name

docker build  

Shows all local storage images

docker images
docker image ls

Run a docker container based on an image, i. e. docker run myimage -it bash. If no local image can be found docker run automatically tries to download the image from Docker hub.

docker run  

Display the logs of a container, you specified. To continue showing log updates just use docker logs -f mycontainer

docker logs 

Lists the volumes, which are commonly used for persisting data of Docker containers.

docker volume ls  

List all networks available for docker container

docker network ls 

Removes one or more containers. docker rm mycontainer, but make sure the container is not running

docker rm   

Removes one or more images. docker rmi myimage, but make sure no running container is based on that image

docker rmi  

Stops one or more containers. docker stop mycontainer stops one container, while docker stop $(docker ps -a -q) stops all running containers.

docker stop   

Starts a stopped container using the last state

docker start 

Updates container policies, that is especially helpful when your container is stuck in a crash loop

docker update --restart=no 

to copy files from a running container to the host or the way around. docker cp :/etc/file . to copy /etc/file to your current directory.

docker cp 

Some combinations that help a lot:

  • kill all running containers with docker
kill $(docker ps -q)
  • delete all stopped containers with docker
rm $(docker ps -a -q)
  • delete all images with docker
rmi $(docker images -q)
  • update and stop a container that is in a crash-loop with docker
update --restart=no && docker stop
  • bash shell into container
docker exec -i -t /bin/bash 

** if bash is not available use /bin/sh

  • bash shell with root if container is running in a different user context
docker exec -i -t -u root /bin/bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment