DEV Community

Cover image for Every Day Docker Commands You Need To Learn
Adriano Galello
Adriano Galello

Posted on • Originally published at gdi3d.github.io

Every Day Docker Commands You Need To Learn

Interacting with containers

How to list all your running containers

docker ps
Enter fullscreen mode Exit fullscreen mode

How to list all running and stopped containers

docker ps -a
Enter fullscreen mode Exit fullscreen mode

How to run a command inside a container

docker exec CONTAINER_NAME_OR_ID command
Enter fullscreen mode Exit fullscreen mode

Cool tip: You can use the first 4 characters of the container id instead of the full name or full id. It works with all commands ๐Ÿ˜Ž

How to access a Linux container

docker exec -it CONTAINER_NAME_OR_ID bash

# in case of no bash, like alpine versions for ex., you can try
docker exec -it CONTAINER_NAME_OR_ID sh
Enter fullscreen mode Exit fullscreen mode

How to display logs live

docker logs -f CONTAINER_NAME_OR_ID
Enter fullscreen mode Exit fullscreen mode

How to launch and run a container

docker run CONTAINER_NAME:TAG

# use -d and will be launched in the background
docker run -d nginx:latest
Enter fullscreen mode Exit fullscreen mode

How to stop a container

docker stop CONTAINER_NAME_OR_ID
Enter fullscreen mode Exit fullscreen mode

How to restart a container

docker restart CONTAINER_NAME_OR_ID
Enter fullscreen mode Exit fullscreen mode

Managament Commands

How to list all images

docker image ls
Enter fullscreen mode Exit fullscreen mode

How to delete an image

docker rmi IMAGE_NAME:TAG

# or
docker image rm IMAGE_NAME:TAG
Enter fullscreen mode Exit fullscreen mode

How to delete all images

docker rmi -f $(docker images -qa)
Enter fullscreen mode Exit fullscreen mode

How to remove all stopped containers (-f to force it)

docker container prune -f
Enter fullscreen mode Exit fullscreen mode

How to remove all unused local volumes (-f to force it)

docker volume prune -f
Enter fullscreen mode Exit fullscreen mode

How to remove all unused networks (-f to force it)

docker network prune -f
Enter fullscreen mode Exit fullscreen mode

How to remove unused images (-f to force it)

docker image prune -f
Enter fullscreen mode Exit fullscreen mode

How to stop all containers (-f to force it)

docker stop -f $(docker ps -qa)
Enter fullscreen mode Exit fullscreen mode

How to delete all running and stopped containers (-f to force it)

docker rm -f $(docker ps -qa)
Enter fullscreen mode Exit fullscreen mode

Clean up your environment by running multiple commands

docker stop -f $(docker ps -qa) && docker container prune -f && docker volume prune -f && docker image prune -f && docker network prune -f
Enter fullscreen mode Exit fullscreen mode

Extras

How to build a docker image

docker build -t IMAGE_NAME:TAG location/to/Dockerfile

# Ex.: Running the command inside the of directory the Dockerfile
docker build -t mydockerimg:3.11 .
Enter fullscreen mode Exit fullscreen mode

How to display all containers stats (% CPU, Mem, I/O, PIDS..)

docker stats
Enter fullscreen mode Exit fullscreen mode

Image credit: https://www.pexels.com/@dana-tentis-118658/

Top comments (0)