Day 19: Docker Cheat-sheet

General Usage

  • Start a container in the background:

      $ docker run -d jenkins
    
  • Start an interactive container:

      $ docker run -it ubuntu bash
    
  • Export port from a container:

      $ docker run -p 80:80 -d nginx
    
  • Start a named container:

      $ docker run --name mydb redis
    
  • Restart a stopped container:

      $ docker start mydb
    
  • Stop a container:

      $ docker stop mydb
    
  • Add metadata to a container:

      $ docker run -d --label=traefik.backend=jenkins jenkins
    

Build Images

  • Build an image from Dockerfile in the current directory:

      $ docker build --tag myimage .
    
  • Force rebuild of a Docker image:

      $ docker build --no-cache .
    
  • Convert a container to an image:

      $ docker commit c7337 myimage
    

Debug

  • Run another process in a running container:

      $ docker exec -it c7337 bash
    
  • Show live logs of a running daemon container:

      $ docker logs -f c7337
    
  • Show exposed ports of a container:

      $ docker port c7337
    

Volumes

  • Create a local volume:

      $ docker volume create --name myvol
    
  • Mounting a volume on container start:

      $ docker run -v myvol:/data redis
    
  • Destroy a volume:

      $ docker volume rm myvol
    
  • List volumes:

      $ docker volume ls
    

Manage Containers

  • List running containers:

      $ docker ps
    
  • List all containers (running & stopped):

      $ docker ps -a
    
  • Delete all stopped containers:

      $ docker rm $(docker ps --filter status=exited -q)
    
  • Start a container automatically removed on stop:

      $ docker run --rm ubuntu bash
    
  • Query a specific metadata of a running container:

      $ docker inspect -f '{{ .NetworkSettings.IPAddress }}' c7337
    

Networking

  • Create a local network:

      $ docker network create mynet
    
  • Attach a container to a network on start:

      $ docker run -d --net mynet redis
    
  • Connect a running container to a network:

      $ docker network connect mynet c7337
    
  • Disconnect a container from a network:

      $ docker network disconnect mynet c7337
    
  • List all containers with a specific label:

      $ docker ps --filter label=traefik.backend
    

Cleanup

  • Remove all unused images:

      $ docker rmi $(docker images -q)
    

0
Subscribe to my newsletter

Read articles from Yashraj Singh Sisodiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yashraj Singh Sisodiya
Yashraj Singh Sisodiya

I am Yashraj Singh Sisodiya, a 3rd Year CSE student at SVVV, born and raised in Shujalpur. Currently residing in Indore, I'm passionate about pursuing a career in DevOps engineering. My tech journey began with an internship at Infobyte, honing my skills as an Android Developer intern. Alongside my academic pursuits, I actively participate in co-curriculars, holding roles as Technical Lead at Abhyudaya and Cloud Lead at GDSC SVVV, while also serving as an MLSA of my college. I have a keen interest in Cloud Computing, demonstrated through projects such as User management and Backup using shell scripting Linux, Dockerizing applications, CI/CD with Jenkins, and deploying a 3-tier application on AWS. Always eager to learn, I'm committed to expanding my knowledge and skills in the ever-evolving tech landscape.