Skip to main content

Command Palette

Search for a command to run...

Docker Cheatsheet.

Published
3 min read
Docker Cheatsheet.
A

Product-first engineer, blogger and open-source contributor with around 4 years of experience in software development, cloud-native architecture and distributed systems.

I build fintech products that process millions of transactions daily and drive substantial revenue. My expertise spans designing, architecting and deploying scalable software, focusing on the business under the code.

I collaborate closely with engineers, product owners, and guilds, known for my clear communication and team-centric approach in dynamic environments. Colleagues appreciate my adaptability, openness and focus on diverse, meaningful contributions. Beyond coding, I’m recognized for my documentation, ownership and presentation skills, which drive clarity and engagement across teams. Bilingual in English and Deustch, I bridge cross-functional teams across geographies, ensuring smooth, efficient communication.

I’m always open to new opportunities for connection and collaboration. Let’s connect and explore ways to create together.

As I was graduating, Docker emerged as a hot topic in the software development world. With just three years of experience under my belt, I’ve seen firsthand how it transformed our workflows.

Before Docker, managing environments was a constant headache. I remember the frustration of code working on my machine but failing in production. Docker changed that by packaging applications with all their dependencies into containers, making the mantra “it works on my machine” a reality everywhere (although this was tackled few times already, Docker & containers made it really possible :D).

What drew me in was its simplicity. A few commands could launch complex applications, allowing for easy collaboration across teams. As Docker evolved, it became integral to microservices architectures, enabling agile scaling of applications.

I’m sharing few handy Docker commands, that may serve as a cheatsheet for quick reference.

Docker Commands:

Working with Containers

Launching Containers

  • Start a Container:

      docker start <container-name>
    
  • Run a New Container:

      docker run alpine
    
  • Name a Container: To assign a specific name to a container, use the --name flag:

      docker run --name ashwinscontainer alpine
    
  • List Running Containers:

      docker container ls
    
  • List All Containers (Including Stopped Ones):

      docker container ls -a
    
  • Interact with a Container (Open a Virtual Terminal):

      docker run -it alpine
    

    or

      docker run --interactive --tty alpine
    
  • Run a Container in the Background:

      docker run -d alpine
    
  • Run a Persistent Container (Always Up):

      docker run -dt alpine
    
  • Restart Container on System Reboot: To ensure a container restarts automatically, use the --restart flag:

      docker run -dt --restart always <image-name>
    

    Other options include:

    • no (do not restart)

    • on-failure[:max-retries] (restart on failure)

    • unless-stopped (restart unless explicitly stopped)

  • Remove a Container After It Stops:

      docker run -it --rm alpine
    

Accessing Containers

  • Execute Commands in a Running Container: To open a shell or execute commands in a running container, use:

      docker exec -it <container-name> ash
    
  • Install Packages Inside a Container:

      docker exec <container-name> apk add nginx
    
  • Copy Files from a Container to Host:

      docker cp <container-name>:<file-location> <destination>
    
  • Copy Files from Host to a Container:

      docker cp <source-location> <container-name>:<destination>
    

Managing Containers

  • Stop a Container:

      docker stop <container-name>
    
  • Restart a Container:

      docker restart <container-name>
    
  • Remove a Container:

    • Remove a specific container:

        docker rm <container-name>
      
    • Remove all stopped containers:

        docker container prune
      
  • Rename a Container:

      docker rename <old-name> <new-name>
    
  • Monitor Container Performance:

      docker stats <container-name>
    
  • Inspect Container Details:

      docker inspect <container-name>
    
  • Create a Container Image from a Running Container:

      docker commit <container-name> <image-name>
    
  • Map Container Port to Host Port:

      docker run -p <host-port>:<container-port> -dt <image-name>
    

Starting a Nginx Server

To kickstart an Nginx server inside a running container:

docker exec -dt <container-name> nginx -g 'pid /tmp/nginx.pid; daemon off;'

More from this blog

A

Ashwin's Personal Blog

20 posts

Sharing my learnings and experience to the tech community.