Docker CheatSheet

Welcome to the ultimate Docker cheat sheet, your go-to guide for mastering containerization with ease and sophistication. With this comprehensive resource at your fingertips, you'll have all the knowledge you need to confidently navigate Docker's powerful features and functionalities. From basic commands to advanced techniques, this cheat sheet is designed to help developers of all skill levels make the most of Docker's vast capabilities.

With clear and concise explanations, practical examples, and expert tips from seasoned professionals, this Docker cheat sheet is the perfect tool for anyone seeking to streamline their workflow and accelerate application development. Whether you're looking to optimize your Docker images, boost performance, or simply manage your containers more efficiently, this cheat sheet has everything you need to succeed.

So don't wait – take your Docker skills to the next level today with our sophisticated and comprehensive cheat sheet. Download it now and discover the power of containerization like never before!


Table of Content




# Getting started Docker


Getting started

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Dockers methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.



Create and run a container in background


$ docker run -d -p 80:80 docker/getting-started

-d - Run the container in detached mode

-p 80:80 - Map port 80 to port 80 in the container

docker/getting-started - The image to use



Create and run a container in foreground.


$ docker run -it -p 8001:8080 --name my-nginx nginx

-it - Interactive bash mode

-p 8001:8080 - Map port 8001 to port 8080 in the container

--name my-nginx - Specify a name

nginx - The image to use

Why Docker ?

  • Consistent & Isolated Environment
  • Rapid Application Deployment
  • Ensures Scalability & Flexibility
  • Better Portability
  • Cost-Effective
  • In-Built Version Control System
  • Security

General commands

docker ps List running containers
docker ps -a List all containers
docker ps -s List running containers
(with CPU / memory)
docker images List all images
docker exec -it <container> bash Connecting to container
docker logs <container> Shows container's console log
docker stop <container> Stop a container
docker restart <container> Restart a container
docker rm <container> Remove a container
docker port <container> Shows container's port mapping
docker top <container> List processes
docker kill <container> Kill a container

# Containers in Docker


Starting & Stopping

docker start nginx-server Starting
docker stop nginx-server Stopping
docker restart nginx-server Restarting
docker pause nginx-server Pausing
docker unpause nginx-server Unpausing
docker wait nginx-server Blocking a Container
docker kill nginx-server Sending a SIGKILL
docker attach nginx-server Connecting to an Existing Container

Information

docker ps List running containers
docker ps -a List all containers
docker logs nginx-server Container Logs
docker inspect nginx-server Inspecting Containers
docker events nginx-server Containers Events
docker port nginx-server Public Ports
docker top nginx-server Running Processes
docker stats nginx-server Container Resource Usage
docker diff nginx-server Lists the changes made to a container.

Creating

    
docker create [options] IMAGE
    -a, --attach               # attach stdout/err
    -i, --interactive          # attach stdin (interactive)
    -t, --tty                  # pseudo-tty
        --name NAME            # name your image
    -p, --publish 5000:5000    # port map (host:container)
        --expose 5432          # expose a port to containers
    -P, --publish-all          # publish all ports
        --link container:alias # linking
    -v, --volume `pwd`:/app    # mount (absolute paths needed)
    -e, --env NAME=hello       # env vars
    

Example


$ docker create --name my_redis --expose 6379 redis:3.0.2

Manipulating

Renaming a Container


docker rename my-nginx nginx-server

Removing a Container


docker rm nginx-server

Updating a Container


docker update --cpu-shares 512 -m 300M nginx-server

# Images in Docker


Manipulating

docker images Listing images
docker rmi nginx Removing an image
docker load < ubuntu.tar.gz Loading a tarred repository
docker load --input ubuntu.tar Loading a tarred repository
docker save busybox > ubuntu.tar Save an image to a tar archive
docker history Showing the history of an image
docker commit nginx Save a container as an image.
docker tag nginx eon01/nginx Tagging an image
docker push eon01/nginx Pushing an image

Building Images

$ docker build .

$ docker build github.com/creack/docker-firefox

$ docker build - < Dockerfile

$ docker build - < context.tar.gz

$ docker build -t eon/nginx-server .

$ docker build -f myOtherDockerfile .

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

# Networking in Docker


Manipulating

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 run -it -d --network=MyOverlayNetwork nginx

Disconnecting a container from a network

docker network disconnect MyOverlayNetwork nginx

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

# Miscellaneous in Docker


Docker Hub

docker search search_word Search docker hub for images.
docker pull user/image Downloads an image from docker hub.
docker login Authenticate to docker hub
docker push user/image Uploads an image to docker hub.

Registry commands

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=3 --no-trunc busybox


Pulling an Image

$ docker pull nginx

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


Pushing an Image

$ docker push eon01/nginx

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

Batch clean

docker stop -f $(docker ps -a -q) Stopping all containers
docker rm -f $(docker ps -a -q) Removing all containers
docker rmi -f $(docker images -q) Removing all images

Volumes

Check volumes

$ docker volume ls


Cleanup unused volumes

$ docker volume prune

# Dockerfile


Definition

A Dockerfile is a script that automatically creates containers on the Docker platform. A Dockerfile is basically a text document that contains all the commands a user could call on the command line to assemble an image. The Docker platform runs natively on Linux and allows developers to build and run containers, self-contained applications, or systems with no dependencies on the underlying infrastructure. Built upon the resource-isolation features of the Linux kernel, Docker helps developers and system administrators port applications across different systems and machines by running them inside containers.

Docker containers, created by Dockerfiles, can run on any Linux server. Container environments for applications are created using Docker images, which can be built either by executing commands manually or automatically through Dockerfiles. Linux and Windows programs can run inside Docker containers. Using Dockerfiles, developers can create an automated container build that executes several command-line instructions in succession, step by step. Docker containerization is essentially operating-system-level virtualization. Multiple independent containers may run within a single Linux instance without the startup overhead of virtual machines.

Dockerfiles enable greater flexibility and portability of business applications. IT organizations use Dockerfiles to package applications and their dependencies in a virtual container that can run on-premises, in public or private clouds, or on bare metal. Such containers allow multiple applications, worker tasks, and other processes to run autonomously on a single physical machine or across multiple virtual machines. Kubernetes is an open-source system for automatically managing and orchestrating containerized applications created by Dockerfiles.

FROM

# Usage
FROM [image]
FROM [image]:[tag]
FROM [image]@[digest]

Information:

  • FROM must be the first non-comment instruction in the Dockerfile.
  • FROM can appear multiple times within a single Dockerfile in order to create multiple images. Simply make a note of the last image ID output by the commit before each new FROM command.
  • The tag or digest values are optional. If you omit either of them, the builder assumes the latest by default. The builder returns an error if it cannot match the tag value.

MAINTAINER

# Usage
MAINTAINER [name]

Information:

The MAINTAINER instruction allows you to set the Author field of the generated images.

RUN

# Usage

RUN [command]
# (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)

RUN ["[executable]", "[param1]", "[param2]"] # (exec form)

  • The exec form makes it possible to avoid shell string munging, and to RUN commands using a base image that does not contain the specified shell executable.
  • The default shell for the shell form can be changed using the SHELL command.
  • Normal shell processing does not occur when using the exec form. For example, RUN ["echo", "$HOME"] will not do variable substitution on $HOME.


Best Suggest