• Stars
    star
    3,652
  • Rank 12,113 (Top 0.3 %)
  • Language
  • License
    MIT License
  • Created over 7 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🐋 Docker Cheat Sheet 🐋

This repository is trending on Github since some days now. Watch it, we will add many updates in the future. Thank you for your support.

Check the website.

Read this in other languages: English, Russian, Persian, Chinese

Table of Contents

The Ultimate Docker Cheat Sheet

Installation

Linux

For more information, see here

curl -sSL https://get.docker.com/ | sh

Mac

For more information, see here

Use this link to download the dmg.

https://download.docker.com/mac/stable/Docker.dmg

Open the downloaded file and follow the installation instructions.

Windows

For more information, see here

Use the msi installer:

https://download.docker.com/win/stable/InstallDocker.msi

Open the downloaded file and follow the installation instructions.

Docker Registries & Repositories

Login to a Registry

docker login
docker login localhost:8080

Logout from a Registry.

docker logout
docker logout localhost:8080

Searching an Image

docker search nginx
docker search --filter stars=3 --no-trunc nginx

Pulling an Image

docker image pull nginx
docker image pull eon01/nginx localhost:5000/myadmin/nginx

Pushing an Image

docker image push eon01/nginx
docker image push eon01/nginx localhost:5000/myadmin/nginx

Running Containers

Create and Run a Simple Container

-Start an ubuntu:latest image

  • Bind the port 80 from the CONTAINER to port 3000 on the HOST
  • Mount the current directory to /data on the CONTAINER
  • Note: on windows you have to change -v ${PWD}:/data to -v "C:\Data":/data
docker container run --name infinite -it -p 3000:80 -v ${PWD}:/data ubuntu:latest

Creating a Container

docker container create -t -i eon01/infinite --name infinite

Running a Container

docker container run -it --name infinite -d eon01/infinite

Renaming a Container

docker container rename infinite infinity

Removing a Container

docker container rm infinite

A container can be removed only after stopping it using docker stop command. To avoid this, add the --rm flag while running the container.

Updating a Container

docker container update --cpu-shares 512 -m 300M infinite

Running a command within a running container

docker exec -it infinite sh

In the example above, bash can replace sh as an alternative (if the above is giving an error).

Starting & Stopping Containers

Starting

docker container start nginx

Stopping

docker container stop nginx

Restarting

docker container restart nginx

Pausing

docker container pause nginx

Unpausing

docker container unpause nginx

Blocking a Container

docker container wait nginx

Sending a SIGKILL

docker container kill nginx

Sending another signal

docker container kill -s HUP nginx

Connecting to an Existing Container

docker container attach nginx

Getting Information about Containers

From Running Containers

Shortest way:

docker ps

Alternative:

docker container ls

From All containers.

docker ps -a
docker container ls -a

Container Logs

docker logs infinite

'tail -f' Containers' Logs

docker container logs infinite -f

Inspecting Containers

docker container inspect infinite
docker container inspect --format '{{ .NetworkSettings.IPAddress }}' $(docker ps -q)

Containers Events

docker system events infinite

Public Ports

docker container port infinite

Running Processes

docker container top infinite

Container Resource Usage

docker container stats infinite

Inspecting changes to files or directories on a container’s filesystem

docker container diff infinite

Managing Images

Listing Images

docker image ls

Building Images

From a Dockerfile in the Current Directory

docker build .

From a Remote GIT Repository

docker build github.com/creack/docker-firefox

Instead of Specifying a Context, You Can Pass a Single Dockerfile in the URL or Pipe the File in via STDIN

docker build - < Dockerfile
docker build - < context.tar.gz

Building and Tagging

docker build -t eon/infinite .

Building a Dockerfile while Specifying the Build Context

docker build -f myOtherDockerfile .

Building from a Remote Dockerfile URI

curl example.com/remote/Dockerfile | docker build -f - .

Removing an Image

docker image rm nginx

Loading a Tarred Repository from a File or the Standard Input Stream

docker image load < ubuntu.tar.gz
docker image load --input ubuntu.tar

Saving an Image to a Tar Archive

docker image save busybox > ubuntu.tar

Showing the History of an Image

docker image history

Creating an Image From a Container

docker container commit nginx

Tagging an Image

docker image tag nginx eon01/nginx

Pushing an Image

docker image push eon01/nginx

Networking

Creating Networks

Creating an Overlay Network

docker network create -d overlay MyOverlayNetwork

Creating a Bridge Network

docker network create -d bridge MyBridgeNetwork

Creating a Customized Overlay Network

docker network create -d overlay \
  --subnet=192.168.0.0/16 \
  --subnet=192.170.0.0/16 \
  --gateway=192.168.0.100 \
  --gateway=192.170.0.100 \
  --ip-range=192.168.1.0/24 \
  --aux-address="my-router=192.168.1.5" --aux-address="my-switch=192.168.1.6" \
  --aux-address="my-printer=192.170.1.5" --aux-address="my-nas=192.170.1.6" \
  MyOverlayNetwork

Removing a Network

docker network rm MyOverlayNetwork

Listing Networks

docker network ls

Getting Information About a Network

docker network inspect MyOverlayNetwork

Connecting a Running Container to a Network

docker network connect MyOverlayNetwork nginx

Connecting a Container to a Network When it Starts

docker container run -it -d --network=MyOverlayNetwork nginx

Disconnecting a Container from a Network

docker network disconnect MyOverlayNetwork nginx

Exposing Ports

Using Dockerfile, you can expose a port on the container using:

EXPOSE <port_number>

You can also map the container port to a host port using:

docker run -p $HOST_PORT:$CONTAINER_PORT --name <container_name> -t <image>

e.g.

docker run -p $HOST_PORT:$CONTAINER_PORT --name infinite -t infinite

Security

Guidelines for building secure Docker images

  1. Prefer minimal base images
  2. Dedicated user on the image as the least privileged user
  3. Sign and verify images to mitigate MITM attacks
  4. Find, fix and monitor for open source vulnerabilities
  5. Don’t leak sensitive information to docker images
  6. Use fixed tags for immutability
  7. Use COPY instead of ADD
  8. Use labels for metadata
  9. Use multi-stage builds for small secure images
  10. Use a linter

You can find more nformation on Snyk's 10 Docker Image Security Best Practices blog post.

Cleaning Docker

Removing a Running Container

docker container rm nginx

Removing a Container and its Volume

docker container rm -v nginx

Removing all Exited Containers

docker container rm $(docker container ls -a -f status=exited -q)

Removing All Stopped Containers

docker container rm `docker container ls -a -q`

Removing a Docker Image

docker image rm nginx

Removing Dangling Images

docker image rm $(docker image ls -f dangling=true -q)

Removing all Images

docker image rm $(docker image ls -a -q)

Removing all Untagged Images

docker image rm -f $(docker image ls | grep "^<none>" | awk "{print $3}")

Stopping & Removing all Containers

docker container stop $(docker container ls -a -q) && docker container rm $(docker container ls -a -q)

Removing Dangling Volumes

docker volume rm $(docker volume ls -f dangling=true -q)

Removing all unused (containers, images, networks and volumes)

docker system prune -f

Clean all

docker system prune -a

Docker Swarm

Installing Docker Swarm

curl -ssl https://get.docker.com | bash

Initializing the Swarm

docker swarm init --advertise-addr 192.168.10.1

Getting a Worker to Join the Swarm

docker swarm join-token worker

Getting a Manager to Join the Swarm

docker swarm join-token manager

Listing Services

docker service ls

Listing nodes

docker node ls

Creating a Service

docker service create --name vote -p 8080:80 instavote/vote

Listing Swarm Tasks

docker service ps

Scaling a Service

docker service scale vote=3

Updating a Service

docker service update --image instavote/vote:movies vote
docker service update --force --update-parallelism 1 --update-delay 30s nginx
docker service update --update-parallelism 5--update-delay 2s --image instavote/vote:indent vote
docker service update --limit-cpu 2 nginx
docker service update --replicas=5 nginx

Notes

This work was first published in Painless Docker Course

More Repositories

1

kubernetes-workshop

⚙️ A Gentle introduction to Kubernetes with more than just the basics. 🌟 Give it a star if you like it.
Python
3,231
star
2

awesome-chatgpt

🧠 A curated list of awesome ChatGPT resources, including libraries, SDKs, APIs, and more. 🌟 Please consider supporting this project by giving it a star.
2,180
star
3

AWS-CheatSheet

☁️ The AWS Cheat Sheet ☁️
197
star
4

KubectlCheatSheet

The Definitive Kubectl Sheetcheat. ⭐ Give it a star if you like it. Work (always) in progress !
187
star
5

DevOpsLinks-Is-Awesome

Curated & Must Read Content For DevOps, SysAdmins & Fullstack Developers.
140
star
6

SaltStackCheatSheet

SaltStack Cheat Sheet
86
star
7

DoMonit

A Deadly Simple Docker Monitoring Wrapper For Docker API
Python
75
star
8

django-boilerplate

An opinionated Django boilerplate running Celery & Django on the same Docker container and ready to run on Docker Compose & Kubernetes.
JavaScript
37
star
9

LearnKubernetesByBuilding10Projects

Shell
34
star
10

NodeSS

NodeSS: Node.js Security Scanner : This container will scan your Nodejs code for dependency vulnerabilities, print the result and exits
23
star
11

AnsibleCheatSheet

A cheat sheet for Ansible, the automation tool.
23
star
12

PainlessDocker

Painless Docker book git repository.
17
star
13

c2jpg

A bash script to convert FROM gif, bmp, pdf, webp, pcx, pict, pct, png, tif, tiff, xcf, crw, cr2, arw, sr2 & nef TO jpg/jpeg formats.
Shell
17
star
14

d-ealer

A Docker Healer - Auto Restarting Unhealthy Containers
Python
12
star
15

60seconds

Netflix Linux Performance Analysis in 60,000 Milliseconds
Shell
11
star
16

OpenAIGPTForPythonDevelopersFiles

This repository contains the code and other resources used in OpenAI GPT for Python Developers (2nd Edition)
9
star
17

PainlessDockerV2

HTML
8
star
18

hostFilter

hostFilter: Protect your privacy, your computer and save your bandwidth.
Shell
8
star
19

TipsToLearnAWS

This is a work on progress free e-book
7
star
20

celery-rabbitmq-client-server

This is a temlplate to start programming using Celery/RabbitMQ
Python
7
star
21

SaltStackForDevOps

SaltStack For DevOps - The Book Sample
6
star
22

PracticalAWSV2

Python
6
star
23

python-weather-api-example

A simple Python weather app for educational purpose.
Python
6
star
24

urlpy2

URL parsing, cleanup, canonicalization, equivalence and tracking remover
Python
5
star
25

awesome-stars

A curated list of my GitHub stars!
5
star
26

kubernetes-grafana

Kubernetes configurations to create a Grafana cluster
3
star
27

flask_restful_sample

This is a sample Flask restful application
Python
3
star
28

TheAnsibleWorkshopFiles

This repository contains the code snippets used in The Ansible Workshop book.
3
star
29

docker-piwigo

Using Docker Compose To Run Piwigo In Production
PHP
2
star
30

dvsc

dvsc: Dangling Volume Size Calculator
Shell
2
star
31

LLMPromptEngineeringForDevelopersFiles

This repository contains the code snippets used in "LLM Prompt Engineering For Developers"
2
star
32

CloudNativeMicroservicesWithKubernetesFiles

1
star
33

flaskrestful-template

Python
1
star
34

katacoda-scenarios

Katacoda Scenarios
1
star
35

SimpleFullBackup

A simple full backup script
Shell
1
star
36

PainlessDockerCheatSheet

1
star
37

python-ci-example

Python
1
star
38

nodejs-micro-container

A micro container (13 MB) to run Node.js apps
JavaScript
1
star
39

flasklate

A Bootstrap Template To Start Flask Projects (Python3)
Python
1
star
40

docker-symfony

A development environement for Symfony3 developers. It runs Nginx and PHP7-FPM.
Nginx
1
star
41

one-click-hugo-cms-demo

CSS
1
star
42

PainlessDockerBookCode

JavaScript
1
star
43

awesome-projects

Awesome List of my own!
1
star
44

centralGo

A Multithreaded Scheduling Tool For Running Scripts And Routine Tasks.
Python
1
star