Tag: docker

Posts about Docker, containerization, and building reproducible development and deployment environments.

  • Docker & Docker Compose Commands I Keep Forgetting (Cheat Sheet)

    I recently started building a side project and decided to use Docker Compose to manage my containers. While working on it, I noticed that there are quite a few commands I keep reaching for—and I wanted to have them written down somewhere.

    In my day-to-day (9–5) work, I don’t use Docker extensively beyond the basic up and down commands, so some of the more advanced or less frequent commands tend to get rusty over time.

    While refreshing my Docker and Docker Compose knowledge, I decided to collect all the commands I actually use and put them into one place. Think of this post as a personal cheat sheet—useful for quick debugging, reminders, or getting unstuck when something isn’t behaving as expected. Hopefully, it helps you too. 😊

    Docker Compose Commands

    Docker Compose allows you to define and run multi-container applications using a single configuration file, making local development and orchestration much easier.. These are the commands I use most.

    Starting Services

    These commands are used to start one or more services defined in your docker-compose.yml file, either in the foreground or background.

    Start all services defined in docker-compose.yml:

    docker compose up
    Bash

    Start in background (detached mode) – you get your terminal back:

    docker compose up -d
    Bash

    Start services and force a rebuild of images (useful after changing a Dockerfile or dependencies):

    docker compose up --build
    Bash

    Start specific service only:

    docker compose up -d service-a
    Bash

    or start multiple services at once:

    docker compose up -d service-a service-b
    
    Bash

    Stopping Services

    Use these commands to stop running containers, with options to either keep or completely remove them.

    Stop all services (containers are stopped but not removed):

    docker compose stop
    Bash

    Stop and remove containers (this is what you usually want):

    docker compose down
    Bash

    Stop, remove containers and volumes
    ⚠️ Warning: This deletes persisted data such as databases.

    docker compose down -v
    Bash

    Stop specific service:

    docker compose stop service-a
    Bash

    Restarting Services

    Restarting services is useful after configuration changes or when a container becomes unresponsive.

    Restart all services:

    docker compose restart
    Bash

    Restart specific service:

    docker compose restart service-a
    Bash

    Viewing statuses

    These commands help you quickly check which services are running and their current state.

    List running containers for this project:

    docker compose ps
    Bash

    List all containers (including stopped):

    docker compose ps -a
    Bash

    Docker commands

    In addition to Docker Compose, these core Docker commands are useful for inspecting and interacting with individual containers.

    Executing Commands in Containers

    These commands let you access a running container directly, which is especially helpful for debugging and inspecting the runtime environment.

    Using an interactive shell

    docker exec -it container_name bash
    Bash

    If bash is not available (for example, in Alpine images), use sh instead:

    docker exec -it container_name sh
    Bash

    These commands let you enter a running container with an interactive shell, so you can inspect files, run commands, and debug things directly from inside the container.

    Cleanup commands

    Docker can accumulate unused containers, images, and volumes over time—these commands help keep your system clean.

    Remove stopped containers, images or volumes:

    docker container prune
    
    docker image prune
    
    docker volume prune
    Bash

    or in case you need something more generic

    docker system prune --volumes
    Bash

    which removes all unused resources, including volumes.

    Removing Containers by Name Prefix

    While setting up my project, I found some containers from old projects that i had probably forgotten to cleanup. Instead of removing them one by one, because they were a lot, I tried to find a command that removes all of them, by using their prefix (needless to say they were sharing the same prefix). The following command removed all containers with the prefix si:

    docker rm -f $(docker ps -aq --filter "name=si_")
    Bash

    Viewing Logs

    Logs are often the first place to look when something isn’t working. These commands help you inspect service output, debug errors, and monitor behavior in real time.

    docker compose logs
    docker compose logs -f service-a # -f keeps streaming logs in real time
    docker compose logs --tail=100 # Only the last 100 log lines
    Bash

    So if you wanted to see logs for a specific container, in real time you could use

    docker compose logs -f --tail=100 <container-name>
    Bash

    Copy Files From Container

    docker cp container_name:/path/in/container ./local-path
    Bash

    docker-compose vs docker compose

    You may see Docker Compose used in two different ways:

    • docker-compose
    • docker compose

    They do the same thing, but they’re not the same tool.

    docker-compose (Legacy)

    • Older, standalone CLI
    • Installed separately
    • Common in older projects and tutorials
    docker-compose up -d
    Bash

    docker compose (Recommended)

    • Built into the Docker CLI
    • Installed by default with modern Docker
    • Actively maintained and recommended
    docker compose up -d
    Bash

    Which One Should You Use?

    Existing projects: stick with whatever the project already uses

    New projects: use docker compose

    Quick Reference Card

    This section provides a compact overview of the most commonly used Docker and Docker Compose commands for quick access.

    ╔═══════════════════════════════════════════════════════════════╗
                     DOCKER COMMANDS QUICK REFERENCE               
    ╠═══════════════════════════════════════════════════════════════╣
     COMPOSE                                                       
       docker compose up -d          Start in background           
       docker compose down           Stop and remove               
       docker compose logs -f api    Follow service logs           
       docker compose up --build     Rebuild and start             
       docker compose exec api sh    Shell into service            
    ╠═══════════════════════════════════════════════════════════════╣
     CONTAINERS                                                    
       docker ps                     List running                  
       docker ps -a                  List all                      
       docker stop name              Stop container                
       docker rm -f name             Force remove                  
       docker exec -it name sh       Shell into container          
    ╠═══════════════════════════════════════════════════════════════╣
     IMAGES                                                        
       docker images                 List images                   
       docker rmi name               Remove image                  
       docker build -t name .        Build image                   
       docker pull name:tag          Pull from registry            
    ╠═══════════════════════════════════════════════════════════════╣
     LOGS & DEBUG                                                  
       docker logs -f name           Follow logs                   
       docker logs --tail 100 name   Last 100 lines                
       docker inspect name           Full container info           
       docker stats                  Resource usage                
    ╠═══════════════════════════════════════════════════════════════╣
     CLEANUP                                                       
       docker system prune           Remove unused                 
       docker system prune -a        Remove all unused             
       docker volume prune           Remove unused volumes         
       docker system df              Check disk usage              
    ╚═══════════════════════════════════════════════════════════════╝
    ```
    Bash

    Do you have any Docker or Docker Compose commands that have saved you in tricky situations, or ones you use every day?
    Feel free to share them—we’d love to expand this cheat sheet with more real-world examples.