Docker Cheat Sheet

0
1813
If you don’t already know, Docker is an open-source platform for building distributed software using “containerization,” which packages applications together with their environments to make them more portable and easier to deploy.

Docker Commands and Best Practices

Before we get into the best practices for using Docker, here’s a quick overview of the vocabulary you should know:
  • Layer: a set of read-only files or commands that describe how to set up the underlying system beneath the container. Layers are built on top of each other, and each one represents a change to the filesystem.
  • Image: an immutable layer that forms the base of the container.
  • Container: an instance of the image that can be executed as an independent application. The container has a mutable layer that lies on top of the image and that is separate from the underlying layers.
  • Registry: a storage and content delivery system used for distributing Docker images.
  • Repository: a collection of related Docker images, often different versions of the same application.

Below, you’ll find all of the basic Docker commands that you need to start working with containers:

Installation

Linux

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

Mac

Use this link to download the dmg.
https://download.docker.com/mac/stable/Docker.dmg

Windows

Use the msi installer:
https://download.docker.com/win/stable/InstallDocker.msi

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 nginx --stars=4 --no-trunc busybox

Pulling an Image

docker pull nginx
docker pull sysaix/nginx localhost:5000/sysaix/nginx

Pushing an Image

docker push sysaix/nginx
docker push sysaix/nginx localhost:5000/sysaix/nginx

Running Containers

Creating a Container

docker create -t -i sysaix/website --name website

Running a Container

docker run -it --name website -d sysaix/website

Renaming a Container

docker rename website websitenew

Removing a Container

docker rm website

Updating a Container

docker update --cpu-shares 256 -m 100M website

Starting & Stopping Containers

Starting

docker start nginx

Stopping

docker stop nginx

Restarting

docker restart nginx

Pausing

docker pause nginx

Unpausing

docker unpause nginx

Blocking a Container

docker wait nginx

Sending a SIGKILL

docker kill nginx

Connecting to an Existing Container

docker attach nginx

Getting Information about Containers

Running Containers

docker ps
docker ps -a

Container Logs

docker logs website

Inspecting Containers

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

Containers Events

docker events website

Public Ports

docker port website

Running Processes

docker top website

Container Resource Usage

docker stats website

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

docker diff website

Manipulating Images

Listing Images

docker images

Building Images

docker build .
docker build github.com/creack/docker-firefox
docker build - < Dockerfile
docker build - < context.tar.gz
docker build -t sysaix/website .
docker build -f sysaixDockerfile .
curl example.com/remote/Dockerfile | docker build -f - .

Removing an Image

docker rmi nginx

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

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

Save an Image to a Tar Archive

docker save busybox > sysaix.tar

Showing the History of an Image

docker history

Creating an Image From a Container

docker commit nginx

Tagging an Image

docker tag nginx sysaix/nginx

Pushing an Image

docker push sysaix/nginx

Networking

Creating Networks

docker network create -d overlay MyOverlayNetwork
docker network create -d bridge MyBridgeNetwork
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 MyNetwork

Listing Networks

docker network ls

Getting Information About a Network

docker network inspect MyNetwork

Connecting a Running Container to a Network

docker network connect MyNetwork nginx

Connecting a Container to a Network When it Starts

docker run -it -d --network=MyNetwork nginx

Disconnecting a Container from a Network

docker network disconnect MyNetwork nginx

Cleaning Docker

Removing a Running Container

docker rm nginx

Removing a Container and its Volume

docker rm -v nginx

Removing all Exited Containers

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

Removing All Stopped Containers

docker rm `docker ps -a -q`

Removing a Docker Image

docker rmi nginx

Removing Dangling Images

docker rmi $(docker images -f dangling=true -q)

Removing all Images

docker rmi $(docker images -a -q)

Removing all untagged images

docker rmi -f $(docker images | grep "^<none>" | awk "{print $3}")

Stopping & Removing all Containers

docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)

Removing Dangling Volumes

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

Docker Swarm

Installing Docker Swarm

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

Initializing the Swarm

docker swarm init --advertise-addr 192.168.100.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 example -p 8080:80 sysaix/example

Listing Swarm Tasks

docker service ps

Scaling a Service

docker service scale vote=2

Updating a Service

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.