Carles Andres' avatarHomeBlogReference
Back to reference

docker commands

This is a reference article.
Reference articles are written with the help of AI agents, after we have managed to solve a problem.

TL;DR

  • Use docker ps to see running containers and docker logs <container> to debug them.
  • Use docker exec -it <container> <cmd> for an interactive shell or one-off commands.
  • Use docker compose up/down to start and stop multi-service stacks.

Essential Docker CLI commands for managing containers, images, and services.

ItemDescription
docker run <image>Create and start a container from an image. Use -d for detached mode, -p for port mapping, --rm to auto-remove on exit.
docker build -t <name> .Build an image from a Dockerfile in the current directory and tag it with a name.
docker psList running containers. Add -a to show all containers including stopped ones.
docker stop <container>Gracefully stop a running container by sending SIGTERM, then SIGKILL after timeout.
docker start <container>Start a stopped container without creating a new one.
docker rm <container>Remove a stopped container. Use -f to force remove a running container.
docker imagesList all locally stored images with their tags and sizes.
docker rmi <image>Remove an image from local storage. Fails if containers are using it.
docker pull <image>Download an image from a registry (Docker Hub by default).
docker push <image>Upload a tagged image to a registry. Requires docker login first.
docker tag <source> <target>Create a new tag for an image, useful for preparing images for registry push.
docker exec -it <container> <cmd>Run a command inside a running container. Use -it for interactive terminal access.
docker logs <container>View the stdout/stderr output of a container. Add -f to follow live output.
docker compose upStart all services defined in compose.yaml. Add -d for detached mode.
docker compose downStop and remove all containers and networks defined in the Compose file. Add --volumes to also remove named volumes.
docker loginAuthenticate with a container registry to enable push/pull of private images.