└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # docker-cheat-sheet 2 | 3 | # Docker Commands, Help & Tips 4 | 5 | ### Show commands & management commands 6 | 7 | ```shell 8 | $ docker 9 | ``` 10 | 11 | ### Docker version info 12 | 13 | ```shell 14 | $ docker version 15 | ``` 16 | 17 | ### Show info like number of containers, etc 18 | 19 | ```shell 20 | $ docker info 21 | ``` 22 | 23 | # WORKING WITH CONTAINERS 24 | 25 | ### Create an run a container in foreground 26 | 27 | ```bash 28 | $ docker container run -it -p 80:80 nginx 29 | ``` 30 | 31 | ### Create an run a container in background 32 | 33 | ```bash 34 | $ docker container run -d -p 80:80 nginx 35 | ``` 36 | 37 | ### Shorthand 38 | 39 | ```bash 40 | $ docker container run -d -p 80:80 nginx 41 | ``` 42 | 43 | ### Naming Containers 44 | 45 | ``` 46 | $ docker container run -d -p 80:80 --name nginx-server nginx 47 | ``` 48 | 49 | ### TIP: WHAT RUN DID 50 | 51 | - Looked for image called nginx in image cache 52 | - If not found in cache, it looks to the default image repo on Dockerhub 53 | - Pulled it down (latest version), stored in the image cache 54 | - Started it in a new container 55 | - We specified to take port 80- on the host and forward to port 80 on the container 56 | - We could do "$ docker container run --publish 8000:80 --detach nginx" to use port 8000 57 | - We can specify versions like "nginx:1.09" 58 | 59 | ### List running containers 60 | 61 | ``` 62 | $ docker container ls 63 | ``` 64 | 65 | OR 66 | 67 | ``` 68 | $ docker ps 69 | ``` 70 | 71 | ### List all containers (Even if not running) 72 | 73 | ``` 74 | $ docker container ls -a 75 | ``` 76 | 77 | ### Stop container 78 | 79 | ``` 80 | $ docker container stop [ID] 81 | ``` 82 | 83 | ### Stop all running containers 84 | 85 | ``` 86 | $ docker stop $(docker ps -aq) 87 | ``` 88 | 89 | ### Remove container (Can not remove running containers, must stop first) 90 | 91 | ``` 92 | $ docker container rm [ID] 93 | ``` 94 | 95 | ### To remove a running container use force(-f) 96 | 97 | ``` 98 | $ docker container rm -f [ID] 99 | ``` 100 | 101 | ### Remove multiple containers 102 | 103 | ``` 104 | $ docker container rm [ID] [ID] [ID] 105 | ``` 106 | 107 | ### Remove all containers 108 | 109 | ``` 110 | $ docker rm $(docker ps -aq) 111 | ``` 112 | 113 | ### Get logs (Use name or ID) 114 | 115 | ``` 116 | $ docker container logs [NAME] 117 | ``` 118 | 119 | ### List processes running in container 120 | 121 | ``` 122 | $ docker container top [NAME] 123 | ``` 124 | 125 | #### TIP: ABOUT CONTAINERS 126 | 127 | Docker containers are often compared to virtual machines but they are actually just processes running on your host os. In Windows/Mac, Docker runs in a mini-VM so to see the processes youll need to connect directly to that. On Linux however you can run "ps aux" and see the processes directly 128 | 129 | # IMAGE COMMANDS 130 | 131 | ### List the images we have pulled 132 | 133 | ``` 134 | $ docker image ls 135 | ``` 136 | 137 | ### We can also just pull down images 138 | 139 | ``` 140 | $ docker pull [IMAGE] 141 | ``` 142 | 143 | ### Remove image 144 | 145 | ``` 146 | $ docker image rm [IMAGE] 147 | ``` 148 | 149 | ### Remove all images 150 | 151 | ``` 152 | $ docker rmi $(docker images -a -q) 153 | ``` 154 | 155 | #### TIP: ABOUT IMAGES 156 | 157 | - Images are app bianaries and dependencies with meta data about the image data and how to run the image 158 | - Images are no a complete OS. No kernel, kernel modules (drivers) 159 | - Host provides the kernel, big difference between VM 160 | 161 | ### Some sample container creation 162 | 163 | NGINX: 164 | 165 | ``` 166 | $ docker container run -d -p 80:80 --name nginx nginx (-p 80:80 is optional as it runs on 80 by default) 167 | ``` 168 | 169 | APACHE: 170 | 171 | ```bash 172 | $ docker container run -d -p 8080:80 --name apache httpd 173 | ``` 174 | 175 | MONGODB: 176 | 177 | ```bash 178 | $ docker container run -d -p 27017:27017 --name mongo mongo 179 | ``` 180 | 181 | MYSQL: 182 | 183 | ```bash 184 | $ docker container run -d -p 3306:3306 --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql 185 | ``` 186 | 187 | ## CONTAINER INFO 188 | 189 | ### View info on container 190 | 191 | ``` 192 | $ docker container inspect [NAME] 193 | ``` 194 | 195 | ### Specific property (--format) 196 | 197 | ```bash 198 | $ docker container inspect --format '{{ .NetworkSettings.IPAddress }}' [NAME] 199 | ``` 200 | 201 | ### Performance stats (cpu, mem, network, disk, etc) 202 | 203 | ``` 204 | $ docker container stats [NAME] 205 | ``` 206 | 207 | ## ACCESSING CONTAINERS 208 | 209 | ### Create new nginx container and bash into 210 | 211 | ``` 212 | $ docker container run -it --name [NAME] nginx bash 213 | ``` 214 | 215 | - i = interactive Keep STDIN open if not attached 216 | - t = tty - Open prompt 217 | 218 | **For Git Bash, use "winpty"** 219 | 220 | ``` 221 | $ winpty docker container run -it --name [NAME] nginx bash 222 | ``` 223 | 224 | ### Run/Create Ubuntu container 225 | 226 | ``` 227 | $ docker container run -it --name ubuntu ubuntu 228 | ``` 229 | 230 | **(no bash because ubuntu uses bash by default)** 231 | 232 | ### You can also make it so when you exit the container does not stay by using the -rm flag 233 | 234 | ``` 235 | $ docker container run --rm -it --name [NAME] ubuntu 236 | ``` 237 | 238 | ### Access an already created container, start with -ai 239 | 240 | ``` 241 | $ docker container start -ai ubuntu 242 | ``` 243 | 244 | ### Use exec to edit config, etc 245 | 246 | ``` 247 | $ docker container exec -it mysql bash 248 | ``` 249 | 250 | ### Alpine is a very small Linux distro good for docker 251 | 252 | ``` 253 | $ docker container run -it alpine sh 254 | ``` 255 | 256 | (use sh because it does not include bash) 257 | (alpine uses apk for its package manager - can install bash if you want) 258 | 259 | # Limiting The Memory Usage For Containers 260 | 261 | In order to limit the amount of memory a docker container process can use, simply set the -m [memory amount] flag with the limit. 262 | 263 | To run a container with memory limited to 256 MBs: 264 | 265 | ##### Example: docker run -name [name] -m [Memory (int)][memory unit (b, k, m or g)] -d (to run not to attach) -p (to set access and expose ports) [image ID] 266 | ``` 267 | $ docker run -m 64m -d -p 8082:80 tutum/wordpress 268 | ``` 269 | 270 | # NETWORKING 271 | 272 | ### "bridge" or "docker0" is the default network 273 | 274 | ### Get port 275 | 276 | ``` 277 | $ docker container port [NAME] 278 | ``` 279 | 280 | ### List networks 281 | 282 | ``` 283 | $ docker network ls 284 | ``` 285 | 286 | ### Inspect network 287 | 288 | ``` 289 | $ docker network inspect [NETWORK_NAME] 290 | ("bridge" is default) 291 | ``` 292 | 293 | ### Create network 294 | 295 | ``` 296 | $ docker network create [NETWORK_NAME] 297 | ``` 298 | 299 | or 300 | ```bash 301 | $ docker network create --driver bridge [NETWORK_NAME] 302 | ``` 303 | 304 | #### Link to container, add to network 305 | 306 | ```shell 307 | $ docker run -d --net=[NETWORK_NAME] --name mongodb mongo 308 | ``` 309 | 310 | 311 | ### Create container on network 312 | 313 | ``` 314 | $ docker container run -d --name [NAME] --network [NETWORK_NAME] nginx 315 | ``` 316 | 317 | ### Connect existing container to network 318 | 319 | ``` 320 | $ docker network connect [NETWORK_NAME] [CONTAINER_NAME] 321 | ``` 322 | 323 | ### Disconnect container from network 324 | 325 | ``` 326 | $ docker network disconnect [NETWORK_NAME] [CONTAINER_NAME] 327 | ``` 328 | 329 | ### Detach network from container 330 | 331 | ``` 332 | $ docker network disconnect 333 | ``` 334 | 335 | ### Delete/Remove network 336 | 337 | To remove the network by name or id, multiple can be deleted: 338 | 339 | ```shelll 340 | $ docker network rm [NETWORK_NAME] [NETWORK_NAME] 341 | ``` 342 | 343 | # IMAGE TAGGING & PUSHING TO DOCKERHUB 344 | 345 | # tags are labels that point ot an image ID 346 | 347 | ``` 348 | $ docker image ls 349 | ``` 350 | 351 | Youll see that each image has a tag 352 | 353 | ### Retag existing image 354 | 355 | ``` 356 | $ docker image tag nginx btraversy/nginx 357 | ``` 358 | 359 | ### Upload to dockerhub 360 | 361 | ``` 362 | $ docker image push bradtraversy/nginx 363 | ``` 364 | 365 | ### If denied, do 366 | 367 | ``` 368 | $ docker login 369 | ``` 370 | 371 | ### Add tag to new image 372 | 373 | ``` 374 | $ docker image tag bradtraversy/nginx bradtraversy/nginx:testing 375 | ``` 376 | 377 | ### DOCKERFILE PARTS 378 | 379 | - FROM - The os used. Common is alpine, debian, ubuntu 380 | - ENV - Environment variables 381 | - RUN - Run commands/shell scripts, etc 382 | - EXPOSE - Ports to expose 383 | - CMD - Final command run when you launch a new container from image 384 | - WORKDIR - Sets working directory (also could use 'RUN cd /some/path') 385 | - COPY # Copies files from host to container 386 | 387 | ### Build image from dockerfile (reponame can be whatever) 388 | 389 | ### From the same directory as Dockerfile 390 | 391 | ``` 392 | $ docker image build -t [REPONAME] . 393 | ``` 394 | ### Benchmarking builds 395 | 396 | ``` 397 | $ DOCKER_BUILDKIT=1 docker image build -t [REPONAME] . 398 | ``` 399 | 400 | #### TIP: CACHE & ORDER 401 | 402 | - If you re-run the build, it will be quick because everythging is cached. 403 | - If you change one line and re-run, that line and everything after will not be cached 404 | - Keep things that change the most toward the bottom of the Dockerfile 405 | 406 | # EXTENDING DOCKERFILE 407 | 408 | ### Custom Dockerfile for html paqge with nginx 409 | 410 | ``` 411 | FROM nginx:latest # Extends nginx so everything included in that image is included here 412 | WORKDIR /usr/share/nginx/html 413 | COPY index.html index.html 414 | ``` 415 | 416 | ### Build image from Dockerfile 417 | 418 | ``` 419 | $ docker image build -t nginx-website 420 | ``` 421 | 422 | ### Running it 423 | 424 | ``` 425 | $ docker container run -p 80:80 --rm nginx-website 426 | ``` 427 | 428 | ### Tag and push to Dockerhub 429 | 430 | ``` 431 | $ docker image tag nginx-website:latest btraversy/nginx-website:latest 432 | ``` 433 | 434 | ``` 435 | $ docker image push bradtraversy/nginx-website 436 | ``` 437 | 438 | # VOLUMES 439 | 440 | ### Volume - Makes special location outside of container UFS. Used for databases 441 | 442 | ### Bind Mount -Link container path to host path 443 | 444 | ### Check volumes 445 | 446 | ``` 447 | $ docker volume ls 448 | ``` 449 | 450 | ### Cleanup unused volumes 451 | 452 | ``` 453 | $ docker volume prune 454 | ``` 455 | 456 | ### Pull down mysql image to test 457 | 458 | ``` 459 | $ docker pull mysql 460 | ``` 461 | 462 | ### Inspect and see volume 463 | 464 | ``` 465 | $ docker image inspect mysql 466 | ``` 467 | 468 | ### Run container 469 | 470 | ``` 471 | $ docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True mysql 472 | ``` 473 | 474 | ### Inspect and see volume in container 475 | 476 | ``` 477 | $ docker container inspect mysql 478 | ``` 479 | 480 | #### TIP: Mounts 481 | 482 | - You will also see the volume under mounts 483 | - Container gets its own uniqe location on the host to store that data 484 | - Source: xxx is where it lives on the host 485 | 486 | ### Check volumes 487 | 488 | ``` 489 | $ docker volume ls 490 | ``` 491 | 492 | **There is no way to tell volumes apart for instance with 2 mysql containers, so we used named volumes** 493 | 494 | ### Named volumes (Add -v command)(the name here is mysql-db which could be anything) 495 | 496 | ``` 497 | $ docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql mysql 498 | ``` 499 | 500 | ### Inspect new named volume 501 | 502 | ```bash 503 | docker volume inspect mysql-db 504 | ``` 505 | 506 | # BIND MOUNTS 507 | 508 | - Can not use in Dockerfile, specified at run time (uses -v as well) 509 | - ... run -v /Users/brad/stuff:/path/container (mac/linux) 510 | - ... run -v //c/Users/brad/stuff:/path/container (windows) 511 | 512 | **TIP: Instead of typing out local path, for working directory use $(pwd):/path/container - On windows may not work unless you are in your users folder** 513 | 514 | ### Run and be able to edit index.html file (local dir should have the Dockerfile and the index.html) 515 | 516 | ``` 517 | $ docker container run -p 80:80 -v $(pwd):/usr/share/nginx/html nginx 518 | ``` 519 | 520 | ### Go into the container and check 521 | 522 | ```bash 523 | $ docker container exec -it nginx bash 524 | $ cd /usr/share/nginx/html 525 | $ ls -al 526 | ``` 527 | 528 | ### You could create a file in the container and it will exiost on the host as well 529 | 530 | ```bash 531 | $ touch test.txt 532 | ``` 533 | 534 | # Link 535 | 536 | ```bash 537 | $ docker run -d --name my-postgres postgres 538 | ``` 539 | now Link it with dot net 540 | 541 | ```bash 542 | $ docker run -d -p 5000:5000 --link my-postgres:postgres btree/dotnet 543 | ``` 544 | 545 | 546 | # DOCKER COMPOSE 547 | 548 | - Configure relationships between containers 549 | - Save our docker container run settings in easy to read file 550 | - 2 Parts: YAML File (docker.compose.yml) + CLI tool (docker-compose) 551 | 552 | ### 1. docker.compose.yml - Describes solutions for 553 | 554 | - containers 555 | - networks 556 | - volumes 557 | 558 | ### 2. docker-compose CLI - used for local dev/test automation with YAML files 559 | 560 | ### Sample compose file (From Bret Fishers course) 561 | 562 | ``` 563 | version: '2' 564 | 565 | # same as 566 | # docker run -p 80:4000 -v $(pwd):/site bretfisher/jekyll-serve 567 | 568 | services: 569 | jekyll: 570 | image: bretfisher/jekyll-serve 571 | volumes: 572 | - .:/site 573 | ports: 574 | - '80:4000' 575 | ``` 576 | 577 | 578 | ### Example 2 579 | ``` 580 | version: '3' 581 | services: 582 | app: 583 | container_name: docker-node-mongo 584 | restart: always 585 | build: . 586 | ports: 587 | - '80:3000' 588 | links: 589 | - mongo 590 | mongo: 591 | container_name: mongo 592 | image: mongo 593 | ports: 594 | - '27017:27017' 595 | ``` 596 | 597 | ### To run 598 | 599 | ``` 600 | docker-compose up 601 | ``` 602 | 603 | ### You can run in background with 604 | 605 | ``` 606 | docker-compose up -d 607 | ``` 608 | 609 | ### To cleanup 610 | 611 | ``` 612 | docker-compose down 613 | ``` 614 | 615 | #### [this has been taken from](https://gist.github.com/bradtraversy/89fad226dc058a41b596d586022a9bd3) 616 | --------------------------------------------------------------------------------