├── .github ├── FUNDING.yml └── workflows │ └── main.yaml ├── .gitignore ├── README.md └── images └── dockervsvm.jpg /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [sudheerj] 2 | custom: https://buymeacoffee.com/sudheerj 3 | -------------------------------------------------------------------------------- /.github/workflows/main.yaml: -------------------------------------------------------------------------------- 1 | name: Advanced Usage 2 | 3 | on: [push, pull_request] 4 | 5 | env: 6 | FILE_NAME: docker-cheat-sheet 7 | 8 | jobs: 9 | convert_via_pandoc: 10 | runs-on: ubuntu-18.04 11 | steps: 12 | - uses: actions/checkout@v2 13 | 14 | - name: Create output directory 15 | run: | 16 | mkdir output # create output dir 17 | 18 | - name: Create PDF 19 | uses: docker://pandoc/latex:2.9 20 | with: 21 | args: --pdf-engine=xelatex --output=output/${{env.FILE_NAME}}.pdf README.md 22 | 23 | - name: Create epub 24 | uses: docker://pandoc/latex:2.9 25 | with: 26 | args: --output=output/${{env.FILE_NAME}}.epub README.md 27 | 28 | - name: Upload 29 | uses: actions/upload-artifact@master 30 | with: 31 | name: output 32 | path: output -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-cheat-sheet 2 | Quick reference guide for Docker commands 3 | > Click :star:if you like the project. Pull Requests are highly appreciated. Follow me [@SudheerJonna](https://twitter.com/SudheerJonna) for technical updates. 4 | 5 | ## Downloading PDF/Epub formats 6 | 7 | You can download the PDF and Epub version of this repository from the latest run on the [actions tab](https://github.com/sudheerj/docker-cheat-sheet/actions). 8 | 9 | ### Table of Contents 10 | 11 | | No. | Questions | 12 | |---- | --------- 13 | |1 | [**What is docker?**](#what-is-docker) | 14 | |2 | [**Why docker?**](#why-docker)| 15 | |3 | [**Installation**](#installation) | 16 | |4 | [**Registries and Repositories**](#registries-and-repositories)| 17 | |5 | [**Create,Run,Update and Delete containers**](#)| 18 | |6 | [**Start and stop containers**](#start-and-stop-containers) | 19 | |7 | [**Networks**](#networks)| 20 | |8 | [**Cleanup commands**](#cleanup-commands)| 21 | |9 | [**Utility commands**](#utility-commands)| 22 | |10 | [**Docker Hub**](#docker-hub)| 23 | |11 | [**Dockerfile**](#dockerfile)| 24 | |12 | [**Docker Compose**](#docker-compose)| 25 | |13 | [**Docker Swarm**](#docker-swarm)| 26 | 27 | ### What is docker? 28 | Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. 29 | 30 | **[⬆ Back to Top](#table-of-contents)** 31 | 32 | ### Why docker? 33 | Docker is useful to automate the deployment of applications inside a software containers, which makes the applications easy to ship and run virtually anywhere (i.e, platform independent). The Docker container processes run on the host kernel, unlike VM which runs processes in guest kernel. 34 | 35 | ![dockervsvm](images/dockervsvm.jpg) 36 | 37 | **[⬆ Back to Top](#table-of-contents)** 38 | 39 | ### Installation 40 | The docker desktop downloads are available for windows, mac and linux distributions. 41 | 42 | #### Windows 43 | It supports for Windows 10 64-bit: Home, Pro, Enterprise, or Education, version 1903 (Build 18362 or higher). You need to follow the below steps for installation. 44 | 45 | 1. Download docker desktop for windows from https://docs.docker.com/docker-for-windows/install/ 46 | 2. Double-click `Docker Desktop Installer.exe` to run the installer. 47 | 3. Make sure `Enable Hyper-V Windows Features` option is selected 48 | 49 | #### Mac 50 | 51 | 1. Download docker desktop for mac from https://docs.docker.com/docker-for-mac/install/ 52 | 2. Double-click `Docker.dmg` to open the installer and drag it to the Applications folder. 53 | 3. Double-click `Docker.app` in the Applications folder to start Docker. 54 | 55 | #### Linux 56 | 57 | You can install from a package easily 58 | 1. Go to https://download.docker.com/linux/ubuntu/dists/, choose your Ubuntu version and then go to pool/stable/ to get .deb file 59 | 2. Install Docker Engine by referring the downloaded location of the Docker package. 60 | ```cmd 61 | $ sudo dpkg -i /path/to/package.deb 62 | ``` 63 | 3. Verify the Docker Engine by running the `hello-world` image to check correct installation. 64 | ```cmd 65 | $ sudo docker run hello-world 66 | ``` 67 | 68 | **[⬆ Back to Top](#table-of-contents)** 69 | 70 | ### Registries and Repositories 71 | #### Registry: 72 | Docker Registry is a service that stores your docker images. It could be hosted by a third party, as public or private registry. Some of the examples are, 73 | 74 | - Docker Hub, 75 | - Quay, 76 | - Google Container Registry, 77 | - AWS Container Registry 78 | 79 | #### Repository: 80 | A Docker Repository is a collection of related images with same name which have different tags. These tags are an alphanumeric identifiers(like 1.0 or latest) attached to images within a repository. 81 | 82 | For example, if you want to pull golang image using `docker pull golang:latest` command, it will download the image tagged latest within the `golang` repository from the Docker Hub registry. The tags appeared on dockerhub as below, 83 | 84 | #### Login 85 | Login to a registry 86 | ```js 87 | > docker login [OPTIONS] [SERVER] 88 | 89 | [OPTIONS]: 90 | -u/--username username 91 | -p/--password password 92 | 93 | Example: 94 | 95 | 1. docker login localhost:8080 // Login to a registry on your localhost 96 | 2. docker login 97 | ``` 98 | 99 | #### Logout 100 | Logout from a registry 101 | ```js 102 | > docker logout [SERVER] 103 | 104 | Example: 105 | 106 | docker logout localhost:8080 // Logout from a registry on your localhost 107 | ``` 108 | 109 | #### Search image 110 | Search for an image in registry 111 | ```js 112 | docker search [OPTIONS] TERM 113 | 114 | Example: 115 | docker search golang 116 | docker search --filter stars=3 --no-trunc golang 117 | ``` 118 | #### Pull image 119 | 120 | This command pulls an image or a repository from a registry to local machine 121 | 122 | ```js 123 | docker image pull [OPTIONS] NAME[:TAG|@DIGEST] 124 | 125 | Example: 126 | docker image pull golang:latest 127 | ``` 128 | #### Push image 129 | 130 | This command pushes an image to the registry from local machine. 131 | 132 | ```js 133 | docker image push [OPTIONS] NAME[:TAG] 134 | docker image push golang:latest 135 | ``` 136 | 137 | **[⬆ Back to Top](#table-of-contents)** 138 | 139 | ### Create,Run,Update and Delete containers 140 | 141 | #### Create 142 | Create a new container 143 | ```cmd 144 | docker container create [OPTIONS] IMAGE [COMMAND] [ARG...] 145 | 146 | Example: 147 | docker container create -t -i sudheerj/golang --name golang 148 | ``` 149 | 150 | #### Rename 151 | 152 | Rename a container 153 | 154 | ```cmd 155 | docker container rename CONTAINER NEW_NAME 156 | 157 | Example: 158 | docker container rename golang golanguage 159 | docker container rename golanguage golang 160 | ``` 161 | 162 | #### Run 163 | 164 | ```cmd 165 | docker container run [OPTIONS] IMAGE [COMMAND] [ARG...] 166 | 167 | Example: 168 | docker container run -it --name golang -d sudheerj/golang 169 | ``` 170 | 171 | You can also run a command inside container 172 | ```cmd 173 | docker exec [OPTIONS] CONTAINER COMMAND [ARG...] 174 | 175 | Example: 176 | docker exec -it golang sh // Or use bash command if sh is failed 177 | ``` 178 | 179 | #### Update 180 | Update configuration of one or more containers 181 | 182 | ```cmd 183 | docker container update [OPTIONS] CONTAINER [CONTAINER...] 184 | 185 | Example: 186 | docker container update --memory "1g" --cpuset-cpu "1" golang // update the golang to use 1g of memory and only use cpu core 1 187 | ``` 188 | 189 | #### Remove 190 | Remove one or more containers 191 | 192 | ```cmd 193 | docker container rm [OPTIONS] CONTAINER [CONTAINER...] 194 | 195 | Example: 196 | docker container rm golang 197 | docker rm $(docker ps -q -f status=exited) // Remove all the stopped containers 198 | ``` 199 | **[⬆ Back to Top](#table-of-contents)** 200 | ### Start and stop containers 201 | 202 | #### Start 203 | Start one or more stopped containers 204 | 205 | ```cmd 206 | docker container start [OPTIONS] CONTAINER [CONTAINER...] 207 | 208 | Example: 209 | docker container start golang 210 | ``` 211 | 212 | ### Stop 213 | Stop one or more running containers 214 | 215 | 216 | ```cmd 217 | docker container stop [OPTIONS] CONTAINER [CONTAINER...] 218 | 219 | Example: 220 | docker container stop golang 221 | docker stop $(docker ps -a -q) // To stop all the containers 222 | ``` 223 | 224 | #### Restart 225 | Restart one or more containers and processes running inside the container/containers. 226 | 227 | ```cmd 228 | docker container restart [OPTIONS] CONTAINER [CONTAINER...] 229 | 230 | Example: 231 | docker container restart golang 232 | ``` 233 | 234 | #### Pause 235 | Pause all processes within one or more containers 236 | 237 | ```cmd 238 | docker container pause CONTAINER [CONTAINER...] 239 | 240 | Example: 241 | docker container pause golang 242 | ``` 243 | 244 | ### Unpause/Resume 245 | Unpause all processes within one or more containers 246 | 247 | ```cmd 248 | docker container unpause CONTAINER [CONTAINER...] 249 | 250 | Example: 251 | docker container unpause golang 252 | ``` 253 | 254 | #### Kill 255 | Kill one or more running containers 256 | 257 | ```cmd 258 | docker container kill [OPTIONS] CONTAINER [CONTAINER...] 259 | 260 | Example: 261 | docker container kill golang 262 | ``` 263 | 264 | #### Wait 265 | Block until one or more containers stop and print their exit codes after that 266 | 267 | 268 | ```cmd 269 | docker container wait CONTAINER [CONTAINER...] 270 | 271 | Example: 272 | docker container wait golang 273 | ``` 274 | **[⬆ Back to Top](#table-of-contents)** 275 | 276 | ### Networks 277 | Docker provides network commands connect containers to each other and to other non-Docker workloads. The usage of network commands would be `docker network COMMAND` 278 | 279 | #### List networks 280 | List down available networks 281 | 282 | ```cmd 283 | docker network ls 284 | ``` 285 | 286 | #### Connect a container to network 287 | You can connect a container by name or by ID to any network. Once it connected, the container can communicate with other containers in the same network. 288 | 289 | ```cmd 290 | docker network connect [OPTIONS] NETWORK CONTAINER 291 | 292 | Example: 293 | docker network connect multi-host-network container1 294 | ``` 295 | 296 | #### Disconnect a container from a network 297 | You can disconnect a container by name or by ID from any network. 298 | 299 | ```cmd 300 | docker network disconnect [OPTIONS] NETWORK CONTAINER 301 | 302 | Example: 303 | docker network disconnect multi-host-network container1 304 | ``` 305 | 306 | #### Remove one or more networks 307 | Removes one or more networks by name or identifier. Remember, you must first disconnect any containers connected to it before removing it. 308 | 309 | ```cmd 310 | docker network rm NETWORK [NETWORK...] 311 | 312 | Example: 313 | docker network rm my-network 314 | ``` 315 | 316 | #### Create network 317 | It is possible to create a network in Docker before launching containers 318 | 319 | ```cmd 320 | docker network create [OPTIONS] NETWORK 321 | 322 | Example: 323 | sudo docker network create –-driver bridge some_network 324 | ``` 325 | 326 | The above command will output the long ID for the new network. 327 | 328 | #### Inspect network 329 | You can see more details on the network associated with Docker using network inspect command. 330 | 331 | ```cmd 332 | docker network inspect networkname 333 | 334 | Example: 335 | docker network inspect bridge 336 | ``` 337 | ### Cleanup commands 338 | 339 | You may need to cleanup resources (containers, volumes, images, networks) regularly. 340 | 341 | #### Remove all unused resources 342 | 343 | ```cmd 344 | docker system prune 345 | ``` 346 | 347 | #### Images 348 | 349 | ```cmd 350 | $ docker images 351 | $ docker rmi $(docker images --filter "dangling=true" -q --no-trunc) 352 | 353 | $ docker images | grep "none" 354 | $ docker rmi $(docker images | grep "none" | awk '/ / { print $3 }') 355 | ``` 356 | 357 | #### Containers 358 | 359 | ```cmd 360 | $ docker ps 361 | $ docker ps -a 362 | $ docker rm $(docker ps -qa --no-trunc --filter "status=exited") 363 | ``` 364 | 365 | #### Volumes 366 | 367 | ```cmd 368 | $ docker volume rm $(docker volume ls -qf dangling=true) 369 | $ docker volume ls -qf dangling=true | xargs -r docker volume rm 370 | ``` 371 | 372 | #### Networks 373 | 374 | ```cmd 375 | $ docker network ls 376 | $ docker network ls | grep "bridge" 377 | $ docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }') 378 | ``` 379 | 380 | **[⬆ Back to Top](#table-of-contents)** 381 | 382 | ### Utility commands 383 | 384 | **[⬆ Back to Top](#table-of-contents)** 385 | 386 | ### Docker Hub 387 | Docker Hub is a cloud-based repository provided by Docker to test, store and distribute container images which can be accessed either privately or publicly. 388 | 389 | #### From 390 | It initializes a new image and sets the Base Image for subsequent instructions. It must be a first non-comment instruction in the Dockerfile. 391 | ```cmd 392 | FROM 393 | FROM : 394 | FROM @ 395 | ``` 396 | **Note:** Both `tag` and `digest` are optional. If you omit either of them, the builder assumes a latest by default. 397 | 398 | ### Dockerfile 399 | Dockerfile is a text document that contains set of commands and instructions which will be executed in a sequence in the docker environment for building a new docker image. 400 | 401 | #### FROM 402 | This command Sets the Base Image for subsequent instructions 403 | 404 | ```cmd 405 | FROM 406 | FROM : 407 | FROM @ 408 | 409 | Example: 410 | FROM ubuntu:18.04 411 | ``` 412 | 413 | #### RUN 414 | 415 | RUN instruction allows you to install your application and packages required for it. It executes any commands on top of the current image and creates a new layer by committing the results. It is quite common to have multiple RUN instructions in a Dockerfile. 416 | 417 | It has two forms 418 | 1. Shell Form: RUN 419 | ```cmd 420 | RUN npm start 421 | ``` 422 | 2. Exec form RUN ["", "", ""] 423 | ```cmd 424 | RUN [ "npm", "start" ] 425 | ``` 426 | 427 | #### ENTRYPOINT 428 | An ENTRYPOINT allows you to configure a container that will run as an executable. It is used to run when container starts. 429 | 430 | ```cmd 431 | Exec Form: 432 | ENTRYPOINT ["executable", "param1", "param2"] 433 | Shell Form: 434 | ENTRYPOINT command param1 param2 435 | 436 | Example: 437 | FROM alpine:3.5 438 | ENTRYPOINT ["/bin/echo", "Print ENTRYPOINT instruction of Exec Form"] 439 | 440 | ``` 441 | 442 | If an image has an ENTRYPOINT and pass an argument to it while running the container, it wont override the existing entrypoint but it just appends what you passed with the entrypoint. To override the existing ENTRYPOINT. you should user `–entrypoint` flag for the running container. 443 | 444 | Let's see the behavior with the above dockerfile, 445 | 446 | ```cmd 447 | Build image: 448 | docker build -t entrypointImage . 449 | 450 | Run the image: 451 | docker container run entrypointImage // Print ENTRYPOINT instruction of Exec Form 452 | 453 | Override entrypoint: 454 | docker run --entrypoint "/bin/echo" entrypointImage "Override ENTRYPOINT instruction" // Override ENTRYPOINT instruction 455 | ``` 456 | 457 | #### CMD 458 | CMD instruction is used to set a default command, which will be executed only when you run a container without specifying a command. But if the docker container runs with a command, the default command will be ignored. 459 | 460 | The CMD instruction has three forms, 461 | ```cmd 462 | 1. Exec form: 463 | CMD ["executable","param1","param2"] 464 | 2. Default params to ENTRYPOINT: 465 | CMD ["param1","param2"] 466 | 3. Shell form: 467 | CMD command param1 param2 468 | ``` 469 | 470 | The main purpose of the CMD command is to launch the required software in a container. For example, running an executable .exe file or a Bash terminal as soon as the container starts. 471 | 472 | Remember, if docker runs with executable and parameters then CMD instruction will be overridden(Unlike ENTRYPOINT). 473 | 474 | ```cmd 475 | docker run executable parameters 476 | ``` 477 | 478 | **Note:** There should only be one CMD command in your Dockerfile. Otherwise only the last instance of CMD will be executed. 479 | 480 | #### COPY 481 | The COPY instruction copies new files or directories from source and adds them to the destination filesystem of the container. 482 | 483 | ```cmd 484 | COPY [--chown=:] ... 485 | COPY [--chown=:] ["",... ""] 486 | 487 | Example: 488 | COPY test.txt /absoluteDir/ 489 | COPY tes? /absoluteDir/ // Copies all files or directories starting with test to destination container 490 | ``` 491 | 492 | The path must be relative to the source directory that is being built. Whereas is an absolute path, or a path relative to `WORKDIR`. 493 | #### ADD 494 | The ADD instruction copies new files, directories or remote file URLs from source and adds them to the filesystem of the image at the destination path. The functionality is similar to COPY command and supports two forms of usage, 495 | 496 | ```cmd 497 | ADD [--chown=:] ... 498 | ADD [--chown=:] ["",... ""] 499 | 500 | Example: 501 | ADD test.txt /absoluteDir/ 502 | ADD tes? /absoluteDir/ // Copies all files or directories starting with test to destination container 503 | ``` 504 | 505 | ADD commands provides additional features such as downloading remote resources, extracting TAR files etc. 506 | 507 | ```cmd 508 | 1. Download an external file and copy to the destination 509 | ADD http://source.file/url /destination/path 510 | 511 | 2. Copies compressed files and extract the content in the destination 512 | ADD source.file.tar.gz /temp 513 | ``` 514 | 515 | #### ENV 516 | The ENV instruction sets the environment variable to the value . It has two forms, 517 | 518 | 1. The first form, `ENV `, will set a single variable to a value. 519 | 2. The second form, `ENV = ...`, allows for multiple variables to be set at one time. 520 | 521 | ```cmd 522 | ENV 523 | ENV = [= ...] 524 | 525 | Example: 526 | ENV name="John Doe" age=40 527 | ENV name John Doe 528 | ENV age 40 529 | ``` 530 | 531 | #### EXPOSE 532 | The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. i.e, It helps in inter-container communication. You can specify whether the port listens on TCP or UDP, and the default is TCP. 533 | 534 | ```cmd 535 | EXPOSE [/...] 536 | 537 | Example: 538 | EXPOSE 80/udp 539 | EXPOSE 80/tcp 540 | ``` 541 | 542 | But if you want to bind the port of the container with the host machine on which the container is running, use -p option of `docker run` command. 543 | 544 | ```cmd 545 | docker run -p : IMAGE_NAME 546 | 547 | Example: 548 | docker run -p 80:80/udp myDocker 549 | ``` 550 | 551 | #### WORKDIR 552 | The WORKDIR command is used to define the working directory of a Docker container at any given time for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. 553 | 554 | ```cmd 555 | WORKDIR /path/to/workdir 556 | 557 | Example: 558 | WORKDIR /c 559 | WORKDIR d 560 | WORKDIR e 561 | RUN pwd // /c/d/e 562 | ``` 563 | 564 | #### LABEL 565 | The LABEL instruction adds metadata as key-value pairs to an image. Labels included in base or parent images (images in the FROM line) are inherited by your image. 566 | 567 | ```cmd 568 | LABEL = = = ... 569 | 570 | Example: 571 | LABEL version="1.0" 572 | LABEL multi.label1="value1" \ 573 | multi.label2="value2" \ 574 | other="value3" 575 | ``` 576 | 577 | You can view an image’s labels using the `docker image inspect --format='' myimage` command. The output would be as below, 578 | 579 | ```js 580 | { 581 | "version": "1.0", 582 | "multi.label1": "value1", 583 | "multi.label2": "value2", 584 | "other": "value3" 585 | } 586 | ``` 587 | 588 | #### MAINTAINER 589 | The MAINTAINER instruction sets the Author field of the generated images. 590 | ```cmd 591 | MAINTAINER 592 | 593 | Example: 594 | MAINTAINER John 595 | ``` 596 | 597 | This command is deprecated status now and the recommended usage is with LABEL command 598 | ```cmd 599 | LABEL maintainer="John" 600 | ``` 601 | 602 | #### VOLUME 603 | The VOLUME instruction creates a mount point with the specified name and mounted volumes from native host or other containers. 604 | 605 | ```cmd 606 | VOLUME ["/data"] 607 | 608 | Example: 609 | FROM ubuntu 610 | RUN mkdir /test 611 | VOLUME /test 612 | ``` 613 | 614 | ### Docker Compose 615 | Docker compose(or compose) is a tool for defining and running multi-container Docker applications. 616 | ### Docker Swarm 617 | Docker Swarm(or swarm) is an open-source tool used to cluster and orchestrate Docker containers. 618 | 619 | -------------------------------------------------------------------------------- /images/dockervsvm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudheerj/docker-cheat-sheet/60a76f92d65076ac99d0e2627273c11e59731215/images/dockervsvm.jpg --------------------------------------------------------------------------------