# Docker Cheatsheet A comprehensive guide to Docker and Docker Compose commands. ## Docker Compose ### Starting and Running Containers **Create and run containers in the foreground** ```bash docker compose up ``` **Create and run containers in the background** ```bash docker compose up -d ``` **Build and start containers using Docker Compose** ```bash docker compose up --build docker compose up -d --build ``` **Scale services defined in a Compose file** ```bash docker compose up -d --scale service_name=num ``` ### Managing Compose Services **List running containers** ```bash docker compose ps ``` **List all containers** ```bash docker compose ps --all docker compose ps -a ``` **Stop containers** ```bash docker compose stop ``` **Stop and remove containers, networks created by up** ```bash docker compose down ``` **Remove stopped containers created by up** ```bash docker compose rm ``` ### Building and Pulling **Pull images defined in a Compose file** ```bash docker compose pull ``` **Create a container using Docker Compose** ```bash docker compose create ``` **Build or rebuild services** ```bash docker compose build docker compose build service_name ``` ### Logs and Execution **View the logs of services defined in a Compose file** ```bash docker compose logs docker compose logs service_name ``` **Execute a command in a running container defined in a Compose file** ```bash docker compose exec service_name command ``` **Run a one-off command in a service container defined in a Compose file** ```bash docker compose run service_name command ``` ## Container Management ### Listing Containers **List running containers** ```bash docker ps docker container list docker container ls ``` **List all containers** ```bash docker ps --all docker ps -a docker container list --all docker container ls --all ``` ### Running Containers **Run a container from *my-image:1.0*** ```bash docker run my-image:1.0 docker container run my-image:1.0 ``` **Run a container from *my-image:1.0* with the name *www*** ```bash docker run --name www my-image:1.0 ``` **Run a container from *my-image:1.0* exposing internal port *3000* to external port *80*** ```bash docker run --name www -p 80:3000 my-image:1.0 ``` **Run a container in the background from *my-image:1.0*** ```bash docker run -d my-image:1.0 docker container run -d my-image:1.0 ``` ### Starting, Stopping, and Restarting **Start a stopped container named *www*** ```bash docker start www docker container start www ``` **Stop a container by the name *www*** ```bash docker stop www docker container stop www ``` **Stop containers by the name *www1* *www2* ...** ```bash docker stop www1 www2 docker container stop www1 www2 ``` **Stop all the containers that match a keyword *example*** ```bash docker stop $(docker ps -a | grep "example" | awk '{print $1}') ``` **Restart a running container named *www*** ```bash docker restart www docker container restart www ``` ### Killing Containers **Kill a container by the name *www*** ```bash docker kill www docker container kill www ``` **Kill containers by the name *www1* *www2* ...** ```bash docker kill www1 www2 docker container kill www1 www2 ``` ### Removing Containers **Remove containers by the name *www1* *www2* ...** ```bash docker rm www1 www2 docker container rm www1 www2 ``` **Remove all the containers that match a keyword *example*** ```bash docker rm $(docker ps -a | grep "example" | awk '{print $1}') ``` ### Pausing and Unpausing **Pause a running container named *www*** ```bash docker pause www docker container pause www ``` **Unpause a paused container named *www*** ```bash docker unpause www docker container unpause www ``` ### Container Interaction **Runs *id* in *www* container** ```bash docker exec www id docker container exec www id ``` **Run an interactive bash shell on the container** ```bash docker exec -it ubuntu bash docker container exec -it ubuntu bash ``` **Attach to a running container named *www*** ```bash docker attach www docker container attach www ``` ### Container Information **Inspect a container named *www*** ```bash docker inspect www docker container inspect www ``` **List logs of a container named *www*** ```bash docker logs www docker container logs www ``` **Rename a container named *www* to *web*** ```bash docker rename www web docker container rename www web ``` ## Image Management ### Building Images **Build an image with the name *my-image* and tag *1.0*** ```bash docker build -t my-image:1.0 . ``` **Build an image in the current directory with the name *my-image*** ```bash docker build -t my-image . ``` ### Listing and Removing Images **List local images** ```bash docker image ls ``` **Delete local image by the name:tag *my-image:1.0*** ```bash docker image rm my-image:1.0 ``` ### Pulling and Pushing Images **Pull image *example* from a registry** ```bash docker pull example docker image pull example ``` **Push image *my-image* to repository *myrepo* under a registry** ```bash docker push myrepo/my-image:2.0 docker image push myrepo/my-image:2.0 ``` ## Network Management **List networks** ```bash docker network ls ``` **Create a network with the name *my-network*** ```bash docker network create my-network ``` **Remove a network with the name *my-network*** ```bash docker network rm my-network ``` **Inspect a network with the name *my-network*** ```bash docker network inspect my-network ``` ## Volume Management **List volumes** ```bash docker volume ls ``` **Create a volume** ```bash docker volume create hello ``` **Remove all unused local volumes** ```bash docker volume prune ``` ### Volume Backup and Restore **Backup *my_volume* to *my_backup.tar*** ```bash docker run --rm \ --volume my_volume:/mount_bkp \ --volume $(pwd):/backup \ ubuntu \ tar cvf /backup/my_backup.tar /mount_bkp ``` **Restore *my_backup.tar* to *my_volume*** ```bash docker run --rm \ --volume my_volume:/mount_bkp \ --volume $(pwd):/backup \ ubuntu \ tar xvf /backup/my_backup.tar -C /mount_bkp --strip 1 ``` ## System Maintenance **Remove all unused containers, networks, images (both dangling and unreferenced)** ```bash docker system prune ``` **Remove all unused containers, networks, images (both dangling and unreferenced), and volumes** ```bash docker system prune --volumes ``` **Remove all unused containers, networks, all unused images, and volumes** ```bash docker system prune --all --volumes ``` --- ## Quick Reference ### Common Workflow 1. **Build an image**: `docker build -t my-app:1.0 .` 2. **Run a container**: `docker run -d -p 80:3000 --name my-app my-app:1.0` 3. **Check running containers**: `docker ps` 4. **View logs**: `docker logs my-app` 5. **Stop container**: `docker stop my-app` 6. **Remove container**: `docker rm my-app` ### Docker Compose Workflow 1. **Start services**: `docker compose up -d` 2. **View logs**: `docker compose logs -f` 3. **Stop services**: `docker compose down` 4. **Rebuild and restart**: `docker compose up -d --build` ### Useful Tips - Use `-d` flag to run containers in detached (background) mode - Use `-it` flags for interactive terminal sessions - Use `--rm` flag to automatically remove containers after they exit - Use `-p` flag to map ports: `-p host_port:container_port` - Use `-v` flag to mount volumes: `-v host_path:container_path` - Use `--name` flag to give containers memorable names