|
| 1 | + |
| 2 | +### Docker Build & Run  |
| 3 | + |
| 4 | +- Create image using this directory’s Dockerfile |
| 5 | + |
| 6 | +``` docker build -t image-name . ``` |
| 7 | + |
| 8 | +- Run “image-name” mapping port 8080 to 80 |
| 9 | + |
| 10 | +``` docker run -p 8080:80 image-name ``` |
| 11 | + |
| 12 | +- Run “image-name” mapping port 8080 to 80, but in detached mode |
| 13 | + |
| 14 | +``` docker run -d -p 8080:80 image-name ``` |
| 15 | + |
| 16 | +- See a list of all running containers |
| 17 | + |
| 18 | +``` docker ps ``` |
| 19 | + |
| 20 | +- Gracefully stop the specified container |
| 21 | + |
| 22 | +``` docker stop <hash> ``` |
| 23 | + |
| 24 | +- See a list of all containers, even the ones not running |
| 25 | + |
| 26 | +``` docker ps -a ``` |
| 27 | + |
| 28 | +- Force shutdown of the specified container |
| 29 | + |
| 30 | +``` docker kill <hash> ``` |
| 31 | + |
| 32 | +- Remove the specified container from this machine |
| 33 | + |
| 34 | + ``` docker rm <hash> ``` |
| 35 | + |
| 36 | +- Remove all containers from this machine |
| 37 | + |
| 38 | +``` docker rm $(docker ps -a -q) ``` |
| 39 | + |
| 40 | +- Show all images on this machine |
| 41 | + |
| 42 | +``` docker images -a ``` |
| 43 | + |
| 44 | +- LEGACY: Remove the specified image from this machine |
| 45 | + |
| 46 | +``` docker rmi <imagename> ``` |
| 47 | + |
| 48 | +- LEGACY:Remove all images from this machine |
| 49 | + |
| 50 | +``` docker rmi $(docker images -q) ``` |
| 51 | + |
| 52 | +- LEGACY: Remove all images with dependencies |
| 53 | + |
| 54 | +``` docker images -q | xargs docker rmi –f ``` |
| 55 | + |
| 56 | +- Log in this CLI session using your Docker credentials |
| 57 | + |
| 58 | +``` docker login ``` |
| 59 | + |
| 60 | +- Tag <image> for upload to registry |
| 61 | + |
| 62 | +``` docker tag <image> username/repository:tag ``` |
| 63 | + |
| 64 | +- Upload tagged image to registry |
| 65 | + |
| 66 | +``` docker push username/repository:tag ``` |
| 67 | + |
| 68 | +- Run image from a registry |
| 69 | + |
| 70 | +``` docker run username/repository:tag ``` |
| 71 | + |
| 72 | +- List Docker volume |
| 73 | + |
| 74 | +``` docker volume ls ``` |
| 75 | + |
| 76 | +- List Docker Network |
| 77 | + |
| 78 | +``` docker network ls ``` |
| 79 | + |
| 80 | + |
| 81 | +********************************************* |
| 82 | + |
| 83 | +### Docker Compose  |
| 84 | + |
| 85 | +- Build Docker Images using Docker Compose file |
| 86 | + |
| 87 | +``` docker-compose build ``` |
| 88 | + |
| 89 | +- Run Docker Containers |
| 90 | + |
| 91 | +``` docker-compose up ``` |
| 92 | + |
| 93 | +- Run Docker Containers in background Mode |
| 94 | + |
| 95 | +``` docker-compose up -d ``` |
| 96 | + |
| 97 | +- Build Images before starting Containers |
| 98 | + |
| 99 | +``` docker-compose up --build ``` |
| 100 | + |
| 101 | +- Recreate Containers from existing images |
| 102 | + |
| 103 | +``` docker-compose up --force-recreate ``` |
| 104 | + |
| 105 | +- Stop and Remove Containers, Volumes, Networks, and Images |
| 106 | + |
| 107 | +``` docker-compose down ``` |
| 108 | + |
| 109 | +- List Containers |
| 110 | + |
| 111 | +``` docker-compose ps -a ``` |
| 112 | + |
| 113 | +- Display Log output |
| 114 | + |
| 115 | +``` docker-compose logs ``` |
| 116 | + |
| 117 | + |
| 118 | +****************************************************** |
| 119 | + |
| 120 | +### Docker Swarm  |
| 121 | + |
| 122 | +- Initialize |
| 123 | + |
| 124 | +``` docker swarm init ``` |
| 125 | + |
| 126 | +- Join Docker Cluster |
| 127 | + |
| 128 | +```` docker swarm join --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx <manager/worker>:2377 ``` |
| 129 | + |
| 130 | +- List Docker Nodes in Swarm Cluster |
| 131 | + |
| 132 | +``` docker node ls ``` |
| 133 | + |
| 134 | +- List all running applications on this Docker host |
| 135 | + |
| 136 | +``` docker stack ls ``` |
| 137 | + |
| 138 | +- Run the specified Compose file |
| 139 | + |
| 140 | +``` docker stack deploy -c <composefile> <STACK_NAME> ``` |
| 141 | + |
| 142 | +- List the services associated with an app |
| 143 | + |
| 144 | +``` docker stack services <appname> ``` |
| 145 | + |
| 146 | +- List the running containers associated with an app |
| 147 | + |
| 148 | +``` docker stack ps <appname> ``` |
| 149 | + |
| 150 | +- Tear down an application |
| 151 | + |
| 152 | +``` docker stack rm <STACK_NAME>alias dstr='docker stack rm' ``` |
| 153 | + |
| 154 | +- Docker Swarm Service list |
| 155 | + |
| 156 | +``` docker service ls ``` |
| 157 | +``` alias dsls='docker service ls' ``` |
| 158 | + |
| 159 | +- List the tasks of one or more services |
| 160 | + |
| 161 | +``` docker service ps <service_name> ``` |
| 162 | +``` alias dsp='docker service ps' ``` |
| 163 | + |
| 164 | +- Docker Swarm Service logs |
| 165 | + |
| 166 | +``` alias dsl='docker service logs' ``` |
| 167 | + |
| 168 | +- Remove specific docker swarm service |
| 169 | + |
| 170 | +``` alias dsr='docker service rm' ``` |
| 171 | + |
| 172 | +- Remove unused Containers, Images, Network, etc. |
| 173 | + |
| 174 | +``` alias sprune='docker system prune' ``` |
| 175 | + |
| 176 | +- Remove unused Volumes |
| 177 | + |
| 178 | +``` alias vprune='docker volume prune' ``` |
| 179 | + |
| 180 | +- Create Secret |
| 181 | + |
| 182 | +``` docker secret create <SECRET_NAME> <SECRET_PATH> ``` |
| 183 | + |
| 184 | +- Create Config |
| 185 | + |
| 186 | +``` docker config create <CONFIG_NAME> <CONFIG_FILE_PATH> ``` |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | +### To delete all containers including its volumes use, |
| 192 | + |
| 193 | +```docker rm -vf $(docker ps -a -q)``` |
| 194 | + |
| 195 | +### To delete all the images, |
| 196 | + |
| 197 | +```docker rmi -f $(docker images -a -q)``` |
| 198 | + |
| 199 | +> Remember, you should remove all the containers before removing all the images from which those containers were created. |
| 200 | +
|
| 201 | +### In case you are working on Windows (Powershell), |
| 202 | + |
| 203 | +```bash |
| 204 | +$images = docker images -a -q |
| 205 | +foreach ($image in $images) { docker image rm $image -f } |
| 206 | +``` |
| 207 | +### To delete all images |
| 208 | +
|
| 209 | +```docker rmi $(docker images -a)``` |
| 210 | +
|
| 211 | +### To delete containers which are in exited state |
| 212 | +
|
| 213 | +```docker rm $(docker ps -a -f status=exited -q)``` |
| 214 | +
|
| 215 | +### To delete containers which are in created state |
| 216 | +
|
| 217 | +```docker rm $(docker ps -a -f status=created -q)``` |
| 218 | +
|
| 219 | +> NOTE: Remove all the containers then remove the images |
| 220 | +
|
| 221 | +
|
| 222 | +### Docker run bash shell of image |
| 223 | +```docker run -it --entrypoint bash <image-name>``` |
0 commit comments