Show Menu
Cheatography

Docker cheat sheet Cheat Sheet (DRAFT) by

This is a chat sheet for docker commands

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Basic commands

To display the list of images
docker images
To display the available container
docker ps
To login in to dockerhub
docker login
To find out images in the hub
docker search <image name>
To tag image in dockerhub
docker tag <im­age­_na­me> <do­cke­rhu­b_u­ser­nam­e>/­<re­pos­ito­ry_­nam­e>:­<ta­g>
To push an image to dockerhub
docker push <do­cke­rhu­b_u­ser­nam­e>/­<re­pos­ito­ry_­nam­e>:­<ta­g>
To pull an image from dockerhub
docker pull <im­age­_na­me>:tag
To build a docker file
docker build <pa­th_­to_­doc­ker­fil­e_d­ire­cto­ry> -t <im­age­_na­me>
To run a container in detached mode
docker run -d <im­age­_na­me>:tag
To run a container in detached mode and bind it to specific host port
docker run -d -p <host port>:­<co­ntainer port> <im­age­_na­me>:tag
To stop a container
docker stop <co­nta­ine­r_n­ame>
To remove a container
docker rm <co­nta­ine­r_n­ame>
To check the status of container
systemctl status docker
To start docker
systemctl start docker
To start docker automa­tically when the system restarts
systemctl enable docker
To use docker without sudo
sudo usermod -aG $USER
To run docker container in an intera­ctive terminak
docker run -it <im­age­_na­me> /bin/bash
To exit from a docker intera­ctive terminal
exit
To get container details
docker container inspect <co­nta­ine­r_n­ame>
To show the logs of a container
docker logs <co­nta­ine­r>
To show stats of running containers
docker stats
 

Docker Compose

To start services in docker compose
docker­-co­mpose up
To stop services in docker compose
docker­-co­mpose down

Docker volumes

To display all the volumes
docker volume ls
To create a docker volume
docker volum create <vo­lum­e_n­ame>
To delete a docker volume
docker volume rm <vo­lum­e>
To remove all unused docker volumes
docker volume prune
To get volume details
docker volume inspect <vo­lum­e_n­ame>
To attach volume to container
docker run -v <ho­st_­pat­h>:­<co­nta­ine­r_p­ath> <im­age­_na­me>

Docker Swarm

Initialise swarm in master node
sudo docker swarm init
Display docker swarm nodes
sudo docker node ls
Create a docker service
sudo docker service create --name <se­rvi­ce_­nam­e> --replicas <no of replicas needed> --publish hostnp­ort­:co­ntainer port <im­age>
Show running services
sudo docker service ls
Remove a service
sudo docker service rm <se­rvi­ce_­nam­e>
Join a swarm
sudo docker swarm join --token <to­ken>
Generte a token from master
sudo docker swarm join-token worker
Leave a swarm
sudo docker swarm leave