\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Boulard} \pdfinfo{ /Title (docker-and-swarm-2022.pdf) /Creator (Cheatography) /Author (Boulard) /Subject (Docker \& Swarm 2022 Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{333333} \definecolor{LightBackground}{HTML}{F8F8F8} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Docker \& Swarm 2022 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Boulard} via \textcolor{DarkBackground}{\uline{cheatography.com/125912/cs/34263/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Boulard \\ \uline{cheatography.com/boulard} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 22nd September, 2022.\\ Updated 29th September, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{General Commands}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{docker version} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} docker version -{}-format '\{\{.Server.Version\}\}' & Get the server version \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} docker version -{}-format '\{\{json .\}\}' & Dump raw JSON data \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} docker info -D & All docker commands to output debug info \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker container COMMAND}} \tn % Row 0 \SetRowColor{LightBackground} ls {[}OPTIONS{]} & list running containers \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} ls -{}-all|-a & list all containers \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} ls -{}-size|-s & list running containers sizes \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} ls -q & display container IDs of running container (q stands for quiet) \tn % Row Count 9 (+ 4) % Row 4 \SetRowColor{LightBackground} ls -{}-format '\{\{printf "\%-40s" .Names\}\} \{\{.Ports\}\}' & display name and ports of running containers \tn % Row Count 12 (+ 3) % Row 5 \SetRowColor{white} \seqsplit{start|stop|pause|unpause|restart|rm} CONTAINER & start, stop, pause, unpause, restart or remove container \tn % Row Count 15 (+ 3) % Row 6 \SetRowColor{LightBackground} logs -{}-tail|-n 10 CONTAINER & show the last 10 lines of logs \tn % Row Count 17 (+ 2) % Row 7 \SetRowColor{white} logs {[}OPTIONS{]} CONTAINER & fetch the logs of a container \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} logs -{}-timestamps|-t CONTAINER & show logs including timestamps \tn % Row Count 21 (+ 2) % Row 9 \SetRowColor{white} run {[}OPTIONS{]} IMAGE {[}COMMAND{]} {[}ARG...{]} & create a new container and run a command into it \tn % Row Count 24 (+ 3) % Row 10 \SetRowColor{LightBackground} run -{}-name CONTAINER nginx:1.22.0 & create CONTAINER using nginx image tagged 1.22.0 \tn % Row Count 27 (+ 3) % Row 11 \SetRowColor{white} run -p 8080:80 IMAGE & maps the host port 8080 to the created container port 80 \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker container COMMAND (cont)}} \tn % Row 12 \SetRowColor{LightBackground} run -{}-detach|-d IMAGE & run container in background and print container ID \tn % Row Count 3 (+ 3) % Row 13 \SetRowColor{white} run -{}-rm -it IMAGE CMD & create a container, run a cmd on it interactively, then delete the container \tn % Row Count 7 (+ 4) % Row 14 \SetRowColor{LightBackground} run -{}-net NETWORK IMAGE & -{}-net connects a container to NETWORK \tn % Row Count 9 (+ 2) % Row 15 \SetRowColor{white} run -d -{}-name mysql -e \seqsplit{MYSQL\_ALLOW\_EMPTY\_PASSWORD=True} -v \seqsplit{mysql-db:/var/lib/mysql} mysql & create a {\bf{named volume}} mysql-db pointing to the container directory /var/lib/mysql \tn % Row Count 14 (+ 5) % Row 16 \SetRowColor{LightBackground} run -d -{}-name nginx -p 80:80 -v \seqsplit{\$(pwd):/usr/share/nginx/html} nginx & create a {\bf{bind mount}} between the host current directory and \seqsplit{/usr/share/nginx/html} \tn % Row Count 19 (+ 5) % Row 17 \SetRowColor{white} run -{}-name postgres-db -e \seqsplit{POSTGRES\_PASSWORD=password} -{}-mount type=volume,source=\$HOME/docker/volumes/postgres,target=/var/lib/postgresql/data -p 2000:5432 -d postgres & create a {\bf{named volume}} between the host directory \seqsplit{\$HOME/docker/volumes/postgres} and the container directory \seqsplit{/var/lib/postgresql/data} \tn % Row Count 28 (+ 9) % Row 18 \SetRowColor{LightBackground} run -{}-name postgres-db -e \seqsplit{POSTGRES\_PASSWORD=password} -{}-v \seqsplit{\$HOME/docker/volumes/postgres:/var/lib/postgresql/data} -p 2000:5432 -d postgres & create a {\bf{named volume}} between the host directory \seqsplit{\$HOME/docker/volumes/postgres} and the container directory \seqsplit{/var/lib/postgresql/data} \tn % Row Count 35 (+ 7) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker container COMMAND (cont)}} \tn % Row 19 \SetRowColor{LightBackground} run -d -{}-name postgres-db -e \seqsplit{POSTGRES\_PASSWORD=password} -{}-mount type=bind,source="\$pwd",target=/var/lib/postgresql/data -p 2000:5432 -d postgres & create a {\bf{bind mount}} between the host current directory and the \seqsplit{/var/lib/postgresql/} directory in the container. \tn % Row Count 8 (+ 8) % Row 20 \SetRowColor{white} run -{}-health-cmd="curl -f \seqsplit{localhost:9200/\_cluster/health} || false" -{}-health-interval=5s -{}-health-retries=3 -{}-health-timeout=2s -{}-health-start-period=15s IMAGE & health check \tn % Row Count 17 (+ 9) % Row 21 \SetRowColor{LightBackground} ps -a -{}-filter volume=VOLUME & display all containers using VOLUME \tn % Row Count 19 (+ 2) % Row 22 \SetRowColor{white} ps -{}-no-trunc & prevent truncating output \tn % Row Count 21 (+ 2) % Row 23 \SetRowColor{LightBackground} & \{\{link="https://docs.docker.com/engine/reference/commandline/container\_run/\#options"\}\}more docker container run options here\{\{/link\}\} \tn % Row Count 28 (+ 7) % Row 24 \SetRowColor{white} top CONTAINER {[}ps OPTIONS{]} & display the running processes of a container \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker container COMMAND (cont)}} \tn % Row 25 \SetRowColor{LightBackground} & \{\{link="https://docs.docker.com/engine/reference/commandline/ps/"\}\}ps OPTIONS here\{\{/link\}\} \tn % Row Count 5 (+ 5) % Row 26 \SetRowColor{white} rm -{}-force|-f CONTAINER & force the removal of a running container (uses SIGKILL) \tn % Row Count 8 (+ 3) % Row 27 \SetRowColor{LightBackground} stats {[}OPTIONS{]} CONTAINER & display a live stream of running container(s) resource usage statistics \tn % Row Count 12 (+ 4) % Row 28 \SetRowColor{white} stats -{}-all|-a CONTAINER & display a live stream of ALL running container(s) resource usage statistics \tn % Row Count 16 (+ 4) % Row 29 \SetRowColor{LightBackground} inspect -{}-size|-s -{}-pretty CONTAINER & display detailed information on one or more containers with size \tn % Row Count 20 (+ 4) % Row 30 \SetRowColor{white} update -c 4 -m 8G CONTAINER & update cpu and ram of a container. see \seqsplit{https://docs.docker.com/engine/reference/commandline/container\_update/\#options} \tn % Row Count 26 (+ 6) % Row 31 \SetRowColor{LightBackground} commit {[}OPTIONS{]} CONTAINER {[}REPOSITORY{[}:TAG{]}{]} & Create a new image from a container's changes excepts on volumes. \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker container COMMAND (cont)}} \tn % Row 32 \SetRowColor{LightBackground} commit -{}-change "ENV DEBUG=true" CONTAINER {[}REPOSITORY{[}:TAG{]}{]} & Apply Dockerfile instruction to the created image \tn % Row Count 4 (+ 4) % Row 33 \SetRowColor{white} exec -it CONTAINER sh -c "test -d /some/dir \&\& echo 'It Exists'" & test if a folder exists in a container \tn % Row Count 8 (+ 4) % Row 34 \SetRowColor{LightBackground} docker container inspect CONTAINER -{}-format '\{\{json .NetworkSettings\}\}' | jq & pretty print container's network info \tn % Row Count 12 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker image COMMAND}} \tn % Row 0 \SetRowColor{LightBackground} ls & List images. same as `docker images` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} inspect -f|-{}-format='\{\{.Config.Cmd\}\}' IMAGE & Check available command (ex: sh or bash) available on the image \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} pull nginx & Pull the "latest" nginx image from dockerhub (default repo) \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} pull nginx:1.11.9 & Pull image nginx 1.11.9 from dockerhub \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} history {[}OPTIONS{]} IMAGE & Show the history of the IMAGE (layers) \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} tag SOURCE\_IMAGE{[}:TAG{]} TARGET\_IMAGE{[}:TAG{]} & Create a tag \tn % Row Count 16 (+ 3) % Row 6 \SetRowColor{LightBackground} push {[}OPTIONS{]} NAME{[}:TAG{]} & Push the USER/IMAGE:TAG image to DockerHub (default registry) \tn % Row Count 20 (+ 4) % Row 7 \SetRowColor{white} build {[}OPTIONS{]} PATH | URL | - & Build an image from a Dockerfile. \{\{link="https://docs.docker.com/engine/reference/commandline/image\_build/\#options"\}\}see options here\{\{/link\}\} \tn % Row Count 28 (+ 8) % Row 8 \SetRowColor{LightBackground} build -{}-tag|-t IMAGE . & Build an image searching for a dockerfile in the host current directory and tag it `my\_cust\_image` \tn % Row Count 33 (+ 5) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker image COMMAND (cont)}} \tn % Row 9 \SetRowColor{LightBackground} build -{}-target builder -t IMAGE:latest . & Build from Dockerfile but stops at buil stage "builder" in a multi-stage build dockerfile. \tn % Row Count 5 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker volume}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\} A {\bf{volume gives }}full control of the storage** from the container. A new directory is created within Docker's storage directory on the host machine, and Docker manages that directory's conten} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\}{\bf{Bind mounts}} gives full control of the storage from the host and containers. It's not a secure option. {\bf{A file or directory on the host machine is mounted into a container}}. The file or directory is referenced by its full or relative path on the host machine} \tn % Row Count 11 (+ 6) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\}\{\{link="https://docs.docker.com/storage/\#more-details-about-mount-types"\}\}more info on mount types here\{\{/link\}\}} \tn % Row Count 14 (+ 3) % Row 3 \SetRowColor{white} ls & list volumes \tn % Row Count 15 (+ 1) % Row 4 \SetRowColor{LightBackground} inspect VOLUME & display detailed information on a volume \tn % Row Count 17 (+ 2) % Row 5 \SetRowColor{white} create -{}-name VOLUME & create a volume \tn % Row Count 18 (+ 1) % Row 6 \SetRowColor{LightBackground} rm -f VOLUME & Remove a volume. -f to force \tn % Row Count 20 (+ 2) % Row 7 \SetRowColor{white} prune -f & Remove all unused local volumes. -f to force \tn % Row Count 23 (+ 3) % Row 8 \SetRowColor{LightBackground} docker container run -{}-rm -{}-volumes-from CONTAINER -v \$(pwd):/backup ubuntu tar cvf /backup/backup.tar /NAMED\_VOL & backup data from a container \tn % Row Count 29 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker network}} \tn % Row 0 \SetRowColor{LightBackground} ls & list all the networks \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} create \textless{}network\textgreater{} & create the \textless{}network\textgreater{} network \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} inspect -v \textless{}network\textgreater{} -{}-pretty & display detailed information on a networks. -v for verbose \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} connect \textless{}network\textgreater{} \textless{}container\textgreater{} & connect a running container to a network \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} rm \textless{}network\textgreater{} & remove a network \tn % Row Count 10 (+ 1) % Row 5 \SetRowColor{white} prune & remove all unused networks \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\}a {\bf{bridge network}} is an isolated network on a single engine install (=single host)} \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{create -d|-{}-driver bridge NET} \tn % Row Count 15 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\}an {\bf{overlay network}} is an isolated network on a {\bf{swarm}} (=across host)} \tn % Row Count 17 (+ 2) % Row 9 \SetRowColor{white} create -d|-{}-driver overlay NET & create an overlay network used to enable communication between containers \{\{link="https://docs.docker.com/network/overlay/"\}\}more info here\{\{/link\}\} \tn % Row Count 25 (+ 8) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\}All swarm service management traffic is encrypted by default, using the AES algorithm in GCM mode. Manager nodes in the swarm rotate the key used to encrypt gossip data every 12 hours.} \tn % Row Count 29 (+ 4) % Row 11 \SetRowColor{white} create -{}-opt encrypted NET & encrypt application data. enables IPSEC encryption at the level of the vxlan. \tn % Row Count 33 (+ 4) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker network (cont)}} \tn % Row 12 \SetRowColor{LightBackground} create -{}-driver overlay -{}-attachable NET & create an overlay network which can be used by swarm services {\bf{or standalone containers}} \tn % Row Count 5 (+ 5) % Row 13 \SetRowColor{white} create =d|-{}-driver bridge NET & containers across the host \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker images}} \tn % Row 0 \SetRowColor{LightBackground} docker images & List the most recently created images \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} docker images -f 'dangling=true' -q & display untagged images that are the leaves of the images tree (not intermediary layers). These images occur when a new build of an image takes the repo:tag away from the image ID, leaving it as \textless{}none\textgreater{}:\textless{}none\textgreater{} or untagged. A warning will be issued if trying to remove an image when a container is presently using it. By having this flag it allows for batch cleanup. \tn % Row Count 21 (+ 19) % Row 2 \SetRowColor{LightBackground} docker rmi \$(docker images -f "dangling=true" -q) & Clean up untagged images that are the leaves of the images tree (not intermediary layers). These images occur when a new build of an image takes the repo:tag away from the image ID, leaving it as \textless{}none\textgreater{}:\textless{}none\textgreater{} or untagged. A warning will be issued if trying to remove an image when a container is presently using it. \tn % Row Count 37 (+ 16) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker compose}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\}Compose can only create services locally} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\}A {\bf{Service}} is a {\bf{set of replicated containers}}} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\}{\bf{`docker-compose.yaml`}} is the default file used but we can use the -f option to use another filename} \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} up {[}OPTIONS{]} {[}SERVICE...{]} & Builds, (re)creates, and then starts a set of defined services. \tn % Row Count 11 (+ 4) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\}{\bf{docker compose up}} builds the image from the dockerfile in the build section of the compose file only if not found in cache} \tn % Row Count 14 (+ 3) % Row 5 \SetRowColor{white} -f docker-compose.yaml -f \seqsplit{docker-compose.test.yaml} up & Build test container(s) for services using \seqsplit{`docker-compose.yaml`} as base config overridden by \seqsplit{`docker-compose.test.yaml`} \tn % Row Count 20 (+ 6) % Row 6 \SetRowColor{LightBackground} -f docker-compose.yml -f \seqsplit{docker-compose.prod.yml} config \textgreater{} output.yml up & Build prod container(s) and creates the a merged yaml file \tn % Row Count 24 (+ 4) % Row 7 \SetRowColor{white} up -{}-build & rebuild the images even if found in cache \tn % Row Count 27 (+ 3) % Row 8 \SetRowColor{LightBackground} up -d|-{}-detach & Create container(s) for service(s) in the background. \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker compose (cont)}} \tn % Row 9 \SetRowColor{LightBackground} up -p PROJECT\_NAME & set a project name that will be the containers names prefix instead of the parent directory of the `compose file` \tn % Row Count 6 (+ 6) % Row 10 \SetRowColor{white} down {[}OPTIONS{]} & Stops and removes containers, networks, volumes, and images created by up. \tn % Row Count 10 (+ 4) % Row 11 \SetRowColor{LightBackground} down -{}-rmi local|all & removes images of the service as well as containers. Remove images used by services. "local" remove only images that don't have a custom tag \tn % Row Count 17 (+ 7) % Row 12 \SetRowColor{white} \seqsplit{start|stop|pause|unpause|restart|kill} {[}SERVICE...{]} & Start, stop, pause, unpause, restart, kill q service(s) and its container(s) \tn % Row Count 21 (+ 4) % Row 13 \SetRowColor{LightBackground} logs {[}OPTIONS{]} {[}SERVICE...{]} & Displays log output from containers of a service \tn % Row Count 24 (+ 3) % Row 14 \SetRowColor{white} ps {[}OPTIONS{]} {[}SERVICE...{]} & List containers of a service \tn % Row Count 26 (+ 2) % Row 15 \SetRowColor{LightBackground} top {[}SERVICE...{]} & Display the running processes of a service \tn % Row Count 29 (+ 3) % Row 16 \SetRowColor{white} build {[}OPTIONS{]} {[}SERVICE...{]} & Build or rebuild images but don't start the container \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker compose (cont)}} \tn % Row 17 \SetRowColor{LightBackground} -f FILE build {[}OPTIONS{]} {[}SERVICE...{]} & Build or rebuild images specifying a Dockerfile \tn % Row Count 3 (+ 3) % Row 18 \SetRowColor{white} VAR=VALUE docker-compose build & pass variables at build time to compose \tn % Row Count 5 (+ 2) % Row 19 \SetRowColor{LightBackground} VAR=VALUE docker-compose up & pass variables at run time to compose \tn % Row Count 7 (+ 2) % Row 20 \SetRowColor{white} build -{}-build-arg GIT\_PERSONAL\_ACCESS\_TOKEN="\{TOKEN\}" & Pass variables into Dockerfile through Docker Compose during build \tn % Row Count 11 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker service (Swarm)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\}{\bf{A service is a set of replicated containers}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\}A single {\bf{service}} can have {\bf{multiple tasks}} and each one will launch a {\bf{container}}} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} create -{}-name mydb -{}-replicas 3 redis:7-bullseye & Create a mydb service using 3 redis:7-bullseye nodes \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} create -{}-name mydb -{}-replicas 2 -{}-env MYVAR=foo -{}-env MYVAR2=bar redis:3.0.6 & Create a mydb service specifying 2 env variables \tn % Row Count 12 (+ 4) % Row 4 \SetRowColor{LightBackground} create -{}-name redis -{}-secret source=ssh-key,target=ssh -{}-secret source=app-key,target=app,uid=1000,gid=1001,mode=0400 redis:3.0.6 & Creates /run/secrets/ssh and /run/secrets/app in the container \tn % Row Count 19 (+ 7) % Row 5 \SetRowColor{white} create -{}-name frontend -{}-network \textless{}net\textgreater{} -p 8081:80 nginx & Creates a network attached to \textless{}net\textgreater{}. exposes port 80 reachable throught the host port 8081 \tn % Row Count 24 (+ 5) % Row 6 \SetRowColor{LightBackground} create -{}-name db -{}-network backend -{}-mount type=volume,source=db-data,target=/var/lib/postgresql/data -e \seqsplit{POSTGRES\_HOST\_AUTH\_METHOD=trust} -{}-replicas 1 postgres:9.4 & Create a postgres "db" service with a named volume, attached to the backend network \tn % Row Count 33 (+ 9) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker service (Swarm) (cont)}} \tn % Row 7 \SetRowColor{LightBackground} create -{}-name SERVICE -{}-network NET IMAGE & Creates a SERVICE and attach it to the existing NET network. The swarm extends NET to each node running the service. \tn % Row Count 6 (+ 6) % Row 8 \SetRowColor{white} create -{}-name pgserv -e \seqsplit{POSTGRES\_HOST\_AUTH\_METHOD=trust} -{}-health-cmd="pg\_isready -U postgres || exit 1" -{}-health-start-period 120s postgres:latest & Creates a `pgserv` service with health checks executed every 30sec (default) but the failure counts begins after 120s \tn % Row Count 14 (+ 8) % Row 9 \SetRowColor{LightBackground} create -{}-name SERVICE -p \seqsplit{HOST\_PORT:CONTAINER\_PORT} -{}-replicas 5 -{}-detach=false \seqsplit{REGISTRY\_HOST:REGISTRY\_HOST/IMAGE} & Create a service from a custom registry \tn % Row Count 20 (+ 6) % Row 10 \SetRowColor{white} ls & List services running in the swarm \tn % Row Count 22 (+ 2) % Row 11 \SetRowColor{LightBackground} ps SERVICE & List the tasks of one or more services \tn % Row Count 24 (+ 2) % Row 12 \SetRowColor{white} inspect SERVICE & Display detailed information on one or more services \tn % Row Count 27 (+ 3) % Row 13 \SetRowColor{LightBackground} logs SERVICE & Batch-retrieves logs present at the time of execution \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker service (Swarm) (cont)}} \tn % Row 14 \SetRowColor{LightBackground} rm SERVICE & Removes the specified services from the swarm. \tn % Row Count 3 (+ 3) % Row 15 \SetRowColor{white} update {[}OPTIONS{]} SERVICE & Update a service \tn % Row Count 5 (+ 2) % Row 16 \SetRowColor{LightBackground} update -{}-replicas 10 -{}-reserve-cpu 4 -{}-reserve-memory 16G SERVICE & Change cpu and ram and max tasks. \tn % Row Count 9 (+ 4) % Row 17 \SetRowColor{white} update -{}-mount-add type=volume,source=other-volume,target=/somewhere-else SERVICE & Add a named volume `other-volume` pointing to `/somewhere-else`. note the syntax {\bf{-{}-option-add}} \& {\emph{*-{}-option-rm}} \tn % Row Count 15 (+ 6) % Row 18 \SetRowColor{LightBackground} update -{}-mount-rm PATH myservice & Remove the PATH volume. a path always begins with a / \tn % Row Count 18 (+ 3) % Row 19 \SetRowColor{white} update -{}-secret-add source=ssh-2,target=ssh-2 -{}-secret-rm ssh-1 myservice & Add/remove secret. note the syntax {\bf{-{}-option-add}} \& {\bf{-{}-option-rm}} \tn % Row Count 22 (+ 4) % Row 20 \SetRowColor{LightBackground} update -{}-rollback SERVICE & Rollback a service to its previous state \tn % Row Count 24 (+ 2) % Row 21 \SetRowColor{white} update -{}-image IMAGE SERVICE & Change the image of a service \tn % Row Count 26 (+ 2) % Row 22 \SetRowColor{LightBackground} update -{}-publish-rm HOST\_PORT -{}-publish-add HOST\_PORT:CONTAINER\_PORT\textgreater{} SERVICE & Replace a port in containers of a service \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker service (Swarm) (cont)}} \tn % Row 23 \SetRowColor{LightBackground} scale SERVICE=REPLICAS & Scale one or multiple replicated services. a replica is a task \tn % Row Count 4 (+ 4) % Row 24 \SetRowColor{white} update -{}-force SERVICE & Force update of a service to rebalance the load across the swarm. \tn % Row Count 8 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker node (swarm)}} \tn % Row 0 \SetRowColor{LightBackground} ls & list nodes in the swarm \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} ps NODE & list the running task in a node or current node by default \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} promote|demote NODE {[}NODE...{]} & promote or demote a node to manager or worker \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} rm -f NODE {[}NODE...{]} & remove a node from a swarm. -f to force \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} inspect -{}-pretty self|NODE {[}NODE...{]} & Display detailed and pretty-printed info on one or more nodes \tn % Row Count 14 (+ 4) % Row 5 \SetRowColor{white} inspect -{}-format '\{\{ .Status.Addr \}\}' self|NODE {[}NODE...{]} & get the node IP address \tn % Row Count 17 (+ 3) % Row 6 \SetRowColor{LightBackground} ls -f "role=manager" -f \seqsplit{node.label=region=region-a} & list manager nodes having a region label set to region-a \tn % Row Count 20 (+ 3) % Row 7 \SetRowColor{white} update {[}OPTIONS{]} NODE & update a node \tn % Row Count 22 (+ 2) % Row 8 \SetRowColor{LightBackground} update -{}-label-add \seqsplit{LABEL\_KEY=LABEL\_VALUE} NODE {[}LABEL\_KEY=LABEL\_VALUE NODE...{]} & update \textless{}node\textgreater{} adding a label key/value \tn % Row Count 26 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker stack (swarm)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\{\{colspan=2\}\} {\bf{stacks}} are compose for {\bf{production swarms}} and {\bf{accepts compose files but can't build images}}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} deploy -{}-compose-file|-c docker-compose.yml -{}-compose-file|-c \seqsplit{docker-compose.prod.yml} STACK & Deploy or update the prod stack using the \seqsplit{`docker-compose.yml`} base config and the \seqsplit{`docker-compose.prod.yml`} for specific prod conf \tn % Row Count 10 (+ 7) % Row 2 \SetRowColor{LightBackground} cat \textless{}docker-compose.yml\textgreater{} | docker stack deploy -{}-compose-file - \textless{}stack\textgreater{} & Create or update a stack using the std input (`-` opt) \tn % Row Count 14 (+ 4) % Row 3 \SetRowColor{white} deploy -c \textless{}docker-compose-1.yml\textgreater{} -c \textless{}docker-compose-2.yml\textgreater{}\textless{}stack\textgreater{} & Deploy a stack using multiple compose files. It must be exec on a Manager node \tn % Row Count 18 (+ 4) % Row 4 \SetRowColor{LightBackground} ls {[}OPTIONS{]} & Lists the stacks. \tn % Row Count 19 (+ 1) % Row 5 \SetRowColor{white} ls -{}-format "table \{\{.Name\}\}: \{\{.Services\}\}" & Output stacks with the Name and Services \tn % Row Count 22 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{ps {[}OPTIONS{]} STACK} \tn % Row Count 23 (+ 1) % Row 7 \SetRowColor{white} ps -f "name=redis.1" -f "name=redis.7" STACK & List the tasks that are part of the STACK named `redis.1` and `redis.7` \tn % Row Count 27 (+ 4) % Row 8 \SetRowColor{LightBackground} ps -f "node=NODE\_01" -f "node=NODE\_02" STACK & List the tasks from NODE\_01 and NODE\_02 \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{docker stack (swarm) (cont)}} \tn % Row 9 \SetRowColor{LightBackground} ps -f \seqsplit{"desired-state=running"} -f \seqsplit{"desired-state=ready"} & List the tasks which desired-state is running or ready \tn % Row Count 3 (+ 3) % Row 10 \SetRowColor{white} ps -{}-format "table \{\{.Name\}\}: \{\{.Image\}\}, \{\{.CurrentState\}\}" STACK & Output tasks with the Name, Image and State \tn % Row Count 7 (+ 4) % Row 11 \SetRowColor{LightBackground} rm {[}OPTIONS{]} STACK {[}STACK...{]} & Removes one or more \textless{}stack\textgreater{} \tn % Row Count 9 (+ 2) % Row 12 \SetRowColor{white} services {[}OPTIONS{]} STACK & Lists the services that are running as part of the specified stack \tn % Row Count 13 (+ 4) % Row 13 \SetRowColor{LightBackground} services -{}-filter name=web -{}-filter name=db myapp & List both the web and db services \tn % Row Count 16 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Secrets (swarm)}} \tn % Row 0 \SetRowColor{LightBackground} create {[}OPTIONS{]} SECRET {[}file|-{]} & Create a secret from a file or STDIN as content \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} create psql\_user psql\_user.txt & Create the psql\_user secret containing the value in psql\_user.txt \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} echo SECRET\_PASS | docker secret create psql\_pass - & Create the secret psql\_pass from stdin \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} ls {[}OPTIONS{]} & List secrets \tn % Row Count 11 (+ 1) % Row 4 \SetRowColor{LightBackground} ls -{}-filter label=project & List all secrets with a "project" label \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} inspect SECRET & List info about SECRET \tn % Row Count 15 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{} \tn % Row Count 15 (+ 0) % Row 7 \SetRowColor{white} docker service create -{}-name SERVICE -{}-secret SECRET\_USER -{}-secret SECRET\_PASS -e \seqsplit{POSTGRES\_PASSWORD\_FILE=/run/secrets/SECRET\_PASS} -e \seqsplit{POSTGRES\_USER\_FILE=/run/secrets/SECRET\_USER} postgres & Use secrets inside the containers of a service \tn % Row Count 25 (+ 10) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{A secret can be a username, a password, a key or whatever shouldn't be seen from the outside.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Docker Registry}} \tn % Row 0 \SetRowColor{LightBackground} docker container run -d -p 5000:5000 -{}-restart=always -v \seqsplit{\$(pwd)/registry-data:/var/lib/registry} -{}-name registry registry:2 & start the registry container.A production-ready registry must be protected by TLS and should ideally use an access-control mechanism. \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} docker run -d -p 5000:5000 -{}-name \textless{}registry\textgreater{} -{}-restart unless-stopped -v \seqsplit{\$(pwd)/registry-data:/var/lib/registry} -v \seqsplit{\$(pwd)/certs:/certs} -e \seqsplit{REGISTRY\_HTTP\_TLS\_CERTIFICATE=/certs/domain}.crt -e \seqsplit{REGISTRY\_HTTP\_TLS\_KEY=/certs/domain}.key registry & start a secured registry using host `./registry-data` data folder and `./certs/` for the signed cert and the private key \tn % Row Count 19 (+ 12) % Row 2 \SetRowColor{LightBackground} docker service create -{}-name \textless{}registry\textgreater{} -p 5000:5000 registry & creates a registry in a swarm \tn % Row Count 23 (+ 4) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{} \tn % Row Count 23 (+ 0) % Row 4 \SetRowColor{LightBackground} docker login \textless{}REGISTRY\_HOST\textgreater{}:\textless{}REGISTRY\_PORT\textgreater{} & 1/2 Login to a private registry \tn % Row Count 26 (+ 3) % Row 5 \SetRowColor{white} docker service create -{}-name SERVICE -p \seqsplit{HOST\_PORT:CONTAINER\_PORT} -{}-replicas 5 -{}-detach=false \seqsplit{REGISTRY\_HOST:REGISTRY\_HOST/IMAGE} & 2/2 Create a service from an image on a private registry \tn % Row Count 33 (+ 7) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Docker Registry (cont)}} \tn % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) % Row 7 \SetRowColor{white} docker login \textless{}REGISTRY\_HOST\textgreater{}:\textless{}REGISTRY\_PORT\textgreater{} & 1/2 Login to a private registry \tn % Row Count 3 (+ 3) % Row 8 \SetRowColor{LightBackground} docker tag \textless{}IMAGE\_ID\textgreater{} \textless{}REGISTRY\_HOST\textgreater{}:\textless{}REGISTRY\_PORT\textgreater{}/\textless{}APPNAME\textgreater{}:\textless{}APPVERSION\textgreater{} & 2/2 Tag an image to a private registry \tn % Row Count 7 (+ 4) % Row 9 \SetRowColor{white} docker push \textless{}REGISTRY\_HOST\textgreater{}:\textless{}REGISTRY\_PORT\textgreater{}/\textless{}IMAGE\textgreater{}:\textless{}TAG\textgreater{} & 3/3 Push an image to a private registry \tn % Row Count 10 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \end{document}