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)
    

ย