{"id":2175,"date":"2021-08-23T05:45:23","date_gmt":"2021-08-23T05:45:23","guid":{"rendered":"https:\/\/www.nicktailor.com\/?p=2175"},"modified":"2025-11-23T05:46:30","modified_gmt":"2025-11-23T05:46:30","slug":"docker-cheat-sheet","status":"publish","type":"post","link":"https:\/\/nicktailor.com\/tech-blog\/docker-cheat-sheet\/","title":{"rendered":"Docker Cheat Sheet"},"content":{"rendered":"<h1><\/h1>\n<p>This Docker cheat sheet provides a complete reference for working with containers, images, volumes, networks, Dockerfiles, Docker Compose, and orchestration.<\/p>\n<hr \/>\n<h2>1. Running Containers<\/h2>\n<pre><code># Run a container with limited CPU\ndocker run --cpus=.5 ubuntu\n\n# Run a container with limited memory\ndocker run --memory=100m ubuntu\n\n# Run interactively with a shell\ndocker run -it ubuntu \/bin\/bash\n\n# Detached mode (background)\ndocker run -d ubuntu\n\n# Run a web container and publish host port 8080 to container port 80\ndocker run -d -p 8080:80 httpd\n\n# View running processes inside the container\ndocker exec &lt;container-id&gt; ps -eaf\n\n# Open an interactive shell\ndocker exec -it &lt;container-id&gt; \/bin\/bash\n<\/code><\/pre>\n<hr \/>\n<h2>2. Docker Storage Location<\/h2>\n<p>Docker stores local container data in:<\/p>\n<pre><code>\/var\/lib\/docker\n<\/code><\/pre>\n<ul>\n<li><strong>aufs \/ overlay2<\/strong> \u2013 storage layers<\/li>\n<li><strong>containers<\/strong> \u2013 container metadata<\/li>\n<li><strong>images<\/strong> \u2013 image layers<\/li>\n<li><strong>volumes<\/strong> \u2013 named volume data<\/li>\n<\/ul>\n<hr \/>\n<h2>3. Docker Volumes<\/h2>\n<h3>Create a volume<\/h3>\n<pre><code>docker volume create volume_name\ndocker volume ls\ndocker volume inspect volume_name\n<\/code><\/pre>\n<h3>Mount a named volume<\/h3>\n<pre><code>docker run -v volume_name:\/var\/lib\/mysql mysql\n<\/code><\/pre>\n<p><strong>Note:<\/strong> If the volume does not exist, Docker will create it automatically.<\/p>\n<h3>Bind mount (preferred)<\/h3>\n<pre><code>docker run --mount type=bind,source=\/data\/mysql,target=\/var\/lib\/mysql mysql\n<\/code><\/pre>\n<hr \/>\n<h2>4. Networking Basics<\/h2>\n<pre><code># Map host port 8080 to container port 80\ndocker run -d -p 8080:80 httpd\n\n# Get a container IP on the bridge network\ndocker inspect -f \"{{ .NetworkSettings.Networks.bridge.IPAddress }}\" &lt;container-id&gt;\n<\/code><\/pre>\n<hr \/>\n<h2>5. Storage Drivers<\/h2>\n<table>\n<thead>\n<tr>\n<th>Storage Driver<\/th>\n<th>Supported Filesystems<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>overlay2 \/ overlay<\/td>\n<td>ext4, xfs (ftype=1)<\/td>\n<\/tr>\n<tr>\n<td>aufs<\/td>\n<td>ext4, xfs<\/td>\n<\/tr>\n<tr>\n<td>devicemapper<\/td>\n<td>direct-lvm<\/td>\n<\/tr>\n<tr>\n<td>btrfs<\/td>\n<td>btrfs<\/td>\n<\/tr>\n<tr>\n<td>zfs<\/td>\n<td>zfs<\/td>\n<\/tr>\n<tr>\n<td>vfs<\/td>\n<td>any<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code>docker info\n<\/code><\/pre>\n<hr \/>\n<h2>6. Inspect Containers, Images &amp; Networks<\/h2>\n<pre><code># Full metadata\ndocker inspect &lt;id&gt;\n\n# Image layer history\ndocker history &lt;image&gt;\n<\/code><\/pre>\n<hr \/>\n<h2>7. Image Management &amp; Disk Usage<\/h2>\n<pre><code># List images\ndocker images\n\n# Remove image\ndocker rmi &lt;id&gt;\n\n# Remove unused images\ndocker image prune -a\n\n# Disk usage summary\ndocker system df\n<\/code><\/pre>\n<hr \/>\n<h2>8. Building Docker Images<\/h2>\n<h3>Example Dockerfile<\/h3>\n<pre><code>FROM ubuntu\nRUN apt-get update &amp;&amp; apt-get -y install python3 python3-pip\nRUN pip3 install flask flask-mysql\nCOPY app.py \/opt\/source-code\nENTRYPOINT flask_app=\/opt\/source-code\/app.py flask run --host=0.0.0.0\n<\/code><\/pre>\n<h3>Build commands<\/h3>\n<pre><code># Build using Dockerfile in current dir\ndocker build . -t simple-webapp\n\n# Using a custom Dockerfile\ndocker build -f Dockerfile2 -t myorg\/app2 .\n<\/code><\/pre>\n<hr \/>\n<h2>9. Docker Compose<\/h2>\n<h3>docker-compose.yml example<\/h3>\n<pre><code>version: '3'\nservices:\n  web:\n    image: \"simple-webapp\"\n    ports:\n      - \"5000:5000\"\n\n  redis:\n    image: \"redis:alpine\"\n<\/code><\/pre>\n<p>Start everything:<\/p>\n<pre><code>docker-compose up\n<\/code><\/pre>\n<h3>Python app Dockerfile for Compose<\/h3>\n<pre><code>FROM python:3.4-alpine\nADD . \/code\nWORKDIR \/code\nRUN pip install -r requirements.txt\nCMD [\"python\", \"app.py\"]\n<\/code><\/pre>\n<hr \/>\n<h2>10. Multi-Container Stack Example<\/h2>\n<pre><code># Redis\ndocker run -d --name=redis redis\n\n# PostgreSQL\ndocker run -d --name=db postgres:9.4\n\n# Voting app\ndocker run -d --name=vote -p 5000:80 voting-app\n\n# Result app\ndocker run -d --name=result -p 5001:80 result-app\n\n# Worker\ndocker run -d --name=worker worker\n<\/code><\/pre>\n<h3>Legacy container linking (not used anymore)<\/h3>\n<pre><code>docker run -d --name=vote --link redis:redis -p 5000:80 voting-app\ndocker run -d --name=result --link db:db -p 5001:80 result-app\ndocker run -d --name=worker --link db:db --link redis:redis worker\n<\/code><\/pre>\n<hr \/>\n<h2>11. Cleanup Commands<\/h2>\n<pre><code># Remove all containers\ndocker rm -f $(docker ps -a -q)\n\n# Remove selected containers\ndocker container rm &lt;id1&gt; &lt;id2&gt;\n\n# Remove unused images\ndocker image prune -a\n<\/code><\/pre>\n<hr \/>\n<h2>12. Docker Swarm (Legacy \u2014 Rarely Used Today)<\/h2>\n<p>Docker Swarm was Docker\u2019s native orchestrator. While simple and fast to set up, it is now considered a legacy technology. Today, nearly all container orchestration is performed using <strong>Kubernetes<\/strong>, including lightweight versions like K3s, MicroK8s, and K3d.<\/p>\n<h3>Why Docker Swarm Is No Longer Common<\/h3>\n<ul>\n<li><strong>Minimal development<\/strong> in recent years with few updates.<\/li>\n<li><strong>Lacks modern orchestration features<\/strong> such as CRDs, Operators, service mesh, and advanced autoscaling.<\/li>\n<li><strong>No ecosystem<\/strong> compared to Kubernetes (no Helm, no Ingress controllers, no admission controllers).<\/li>\n<li><strong>Poor scalability<\/strong> vs Kubernetes; struggles beyond moderate cluster sizes.<\/li>\n<li><strong>Nearly zero enterprise adoption<\/strong> as all major clouds support Kubernetes instead.<\/li>\n<\/ul>\n<p>Swarm still works for very small clusters, but it is not recommended for new deployments.<\/p>\n<h3>Legacy Swarm Commands (For Reference Only)<\/h3>\n<pre><code># Initialize a swarm\ndocker swarm init\n\n# Rebuild a failed manager\ndocker swarm init --force-new-cluster\n\n# Promote worker to manager\ndocker node promote &lt;node&gt;\n\n# Drain node\ndocker node update --availability drain &lt;node&gt;\n\n# Create replicated service\ndocker service create --replicas=3 -p 8080:80 my-web-server\n\n# Global service (one per node)\ndocker service create --mode global my-agent\n\n# Update service replicas\ndocker service update --replicas=4 web-server\n<\/code><\/pre>\n<hr \/>\n<h2>13. Overlay Networking (Swarm or Legacy Use)<\/h2>\n<pre><code># No network\ndocker run ubuntu --network=none\n\n# Host networking\ndocker run ubuntu --network=host\n\n# Create overlay network\ndocker network create --driver overlay --subnet 10.0.9.0\/24 my-overlay-network\n\n# Attach service to overlay\ndocker service create --replicas 2 --network my-overlay-network nginx\n<\/code><\/pre>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>This Docker cheat sheet provides a complete reference for working with containers, images, volumes, networks, Dockerfiles, Docker Compose, and orchestration. 1. Running Containers # Run a container with limited CPU docker run &#8211;cpus=.5 ubuntu # Run a container with limited memory docker run &#8211;memory=100m ubuntu # Run interactively with a shell docker run -it ubuntu \/bin\/bash # Detached mode (background)<a href=\"https:\/\/nicktailor.com\/tech-blog\/docker-cheat-sheet\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[],"class_list":["post-2175","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/2175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/comments?post=2175"}],"version-history":[{"count":2,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/2175\/revisions"}],"predecessor-version":[{"id":2178,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/2175\/revisions\/2178"}],"wp:attachment":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/media?parent=2175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/categories?post=2175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/tags?post=2175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}