Essential Docker and Docker Compose commands. Commands marked with a warning delete containers, images or volumes (volumes may hold your data), so read them twice.
Click any command to copy it.
| Command | What it does |
|---|
docker ps | List running containers. |
docker ps -a | List all containers including stopped ones. |
docker run -d --name web -p 8080:80 nginx | Run a container in the background, mapping host port 8080 to container port 80. |
docker run -it --rm alpine sh | Run an interactive shell and remove the container on exit. |
docker exec -it web sh | Open a shell in a running container. |
docker stop web | Stop a container. |
docker start web | Start a stopped container. |
docker restart web | Restart a container. |
docker rm web | Remove a stopped container. |
docker rm -f web | Destructive: Force-remove a container, even if it is running. |
| Command | What it does |
|---|
docker images | List local images. |
docker pull image:tag | Download an image. |
docker build -t name:tag . | Build an image from the Dockerfile in the current directory. |
docker tag name:tag registry/name:tag | Add another name to an image. |
docker push registry/name:tag | Upload an image to a registry. |
docker rmi image | Remove an image. |
docker save -o image.tar name:tag | Export an image to a tar file. |
docker load -i image.tar | Import an image from a tar file. |
| Command | What it does |
|---|
docker compose up -d | Create and start all services in the background. |
docker compose down | Stop and remove the stack's containers and networks. |
docker compose down -v | Destructive: Also delete the stack's named volumes. |
docker compose ps | List the stack's containers. |
docker compose logs -f service | Follow one service's logs. |
docker compose pull | Pull newer images for the stack. |
docker compose up -d --build | Rebuild images and recreate changed services. |
docker compose exec service sh | Open a shell in a service container. |
docker compose config | Validate and print the resolved configuration. |