Interacting with containers

How to list all your running containers

docker ps

How to list all running and stopped containers

docker ps -a

How to run a command inside a container

docker exec CONTAINER_NAME_OR_ID command

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

How to display logs live

docker logs -f CONTAINER_NAME_OR_ID

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

How to stop a container

docker stop CONTAINER_NAME_OR_ID

How to restart a container

docker restart CONTAINER_NAME_OR_ID

Managament Commands

How to list all images

docker image ls

How to delete an image

docker rmi IMAGE_NAME:TAG

# or
docker image rm IMAGE_NAME:TAG

How to delete all images

docker rmi -f $(docker images -qa)

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

docker container prune -f

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

docker volume prune -f

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

docker network prune -f

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

docker image prune -f

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

docker stop -f $(docker ps -qa)

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

docker rm -f $(docker ps -qa)

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

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 .

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

docker stats

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