KEMBAR78
Docker Cheatsheet | PDF | Utility Software | Software Engineering
0% found this document useful (0 votes)
39 views9 pages

Docker Cheatsheet

Docker is a platform for creating, deploying, and running applications in portable containers, utilizing various components such as Dockerfiles, images, and containers. Key features include application isolation, portability, scalability, and efficient resource usage. The document also covers Docker commands for managing images, containers, volumes, networks, and interactions with Docker Hub.

Uploaded by

vb_pol@yahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views9 pages

Docker Cheatsheet

Docker is a platform for creating, deploying, and running applications in portable containers, utilizing various components such as Dockerfiles, images, and containers. Key features include application isolation, portability, scalability, and efficient resource usage. The document also covers Docker commands for managing images, containers, volumes, networks, and interactions with Docker Hub.

Uploaded by

vb_pol@yahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Docker 101

Introduction to Docker
Docker is a platform that helps us create, deploy, and run applications using portable containers.

Docker Terminology

Dockerfile
A Dockerfile is a configuration file containing instructions to build a
Docker image.

Docker image
A Docker image is a read-only template for creating containers.

Base image
A base image is the Docker image used as a template for building other images.

Container
A container is a runnable instance of a Docker image, and an image can have
multiple instances. Docker containers are lightweight and can be deployed on
any physical or virtual machine or cloud.

Layer
A layer in a Docker image is a distinct component in the build process. Each
layer builds on the previous ones, forming the complete Docker image.

Docker engine
A Docker engine is a core component of Docker that runs and manages
Docker containers.

Docker host
A Docker host is a physical or virtual machine running the engine.

Docker daemon
A Docker daemon is a background service that listens to Docker client
commands and manages Docker images, containers, networks, and volumes.

Docker client
A Docker client is a command-line interface or graphical user interface that
interacts with the Docker daemon.

Volume
Volume in Docker is a persistent data storage mechanism shared between the
host and the containers.

Docker registry
A Docker registry is a server for storing and distributing Docker images.
Docker 101

Features of Docker

Application isolation
Docker allows running multiple applications in separate containers independent
of each other.

Portability
Docker allows running containers consistently across different environments.

Scalability
Docker allows scaling applications up or down by adjusting the number of
containers running the application.

Resource efficiency
Docker allows using system resources efficiently by sharing the host OS
kernel among containers.

Performance
Docker provides enhanced performance due to lightweight and fast container
execution.

Dockerfile Creation
A Dockerfile is a text document containing instructions for building a Docker image, which is then
used to run a container.

Build Run

Dockerfile Docker Image Docker Container

Dockerfile Instructions
Command Purpose Syntax Example
FROM Sets the base image. FROM FROM
<image>:<tag> ubuntu:20.04

WORKDIR Sets the working directory for sub- WORKDIR WORKDIR /app
sequent RUN, CMD, ENTRYPOINT, <path>

COPY, and ADD commands.

COPY Copies files/directories from the COPY COPY . /app/


host machine to the image. <src> <dest>

ADD Similar to COPY, but can also handle ADD <src> <dest> ADD
remote URLs and extract tar files. app.tar.gz /app/

RUN Executes commands. RUN <command> RUN apt-get


update -y &&
apt-get install
-y nginx

CMD This is the default command to CMD ["executa- CMD ["nginx",


run when the container starts. ble", "param1", "-g", "daemon
"param2"] off;"]
Docker 101

Command Purpose Syntax Example


ENTRYPOINT Configures the primary execut- ENTRYPOINT ENTRYPOINT
able for a Docker container. ["executable", ["nginx"]
"param1"]

EXPOSE It documents the ports exposed by EXPOSE <port> EXPOSE 80


a Docker container.

LABEL Adds metadata. LABEL LABEL


<key>=<value> version="1.0"
description="My
app"

VOLUME Creates a mount point with the VOLUME VOLUME


specified path. ["/path/to/vol- ["/data"]
ume"]

USER Sets the username or UID to use USER <username> USER appuser
when running the container.

ARG Defines a build-time variable. ARG ARG


<name>[=<de- APP_VERSION=1.0
fault_value>]

ONBUILD Adds an instruction that will be ONBUILD ONBUILD RUN


triggered when the image is used <instruction> echo 'Image
built!'
as a base for another image.
STOPSIGNAL Sets the system call signal that will STOPSIGNAL STOPSIGNAL
be used to stop the container. <signal> SIGTERM

HEALTHCHECK Used to test if the container is HEALTHCHECK HEALTHCHECK


healthy. [<options>] CMD CMD curl --fail
<command> http://local-
host:8080/

SHELL Sets shell for RUN commands SHELL SHELL


in Dockerfile. ["<executable>", ["/bin/bash",
"<param1>", "-c"]
"<param2>"]

Sample Dockerfile
# Use Ubuntu 20.04 as the base image
FROM ubuntu:20.04

# Set the working directory inside the container


WORKDIR /app

# Copy the application files from the host to the container


COPY package.json /app/

# Run a command to update the package list and install curl


RUN apt-get update -y && apt-get install -y curl

# Add a file from a URL to the container


ADD https://example.com/file.tar.gz /app/

# Set an environment variable


ENV NODE_ENV=production

# Expose port 8080 to the outside world


EXPOSE 8080

# Create a volume for persistent data storage


VOLUME ["/data"]

# Set the default command to run when the container starts


CMD ["echo", "Hello, Docker!"]
Docker 101

Images
A Docker image is a read-only template for creating containers. It can be built from a Dockerfile or
pulled from a Docker registry.

Command Purpose Syntax Example


docker Builds an image from a Dockerfile. docker build -t docker build
build <im- -t my_app:1.0 .
age-name>:<tag>
<Dockerfile
path>

docker Lists all images on the local system. docker images or docker images
images or docker image ls
docker
image ls

docker rmi Removes an image from the local docker rmi <im- docker rmi
system. age>:<tag> my_app:1.0

docker tag Tags an image with a new name docker tag docker tag
and tag. <image>:<tag> my_app:1.0
<new_image_name>: theapp:2.0
<new_tag>

docker Shows the history of an image. docker history docker history


history <image>:<tag> ubuntu:20.04

Container
A Docker container is a lightweight, standalone, and executable environment with everything needed
to run software. Containers are created from Docker images and can be started, stopped, and man-
aged independently.

Command Purpose Syntax Example


docker run Starts a new container from an docker run docker run -d
<options> <im- -p 80:80
image. Optionally, you can specify --name my_con-
age> [<command>
a command to run when the [<args>...]] tainer nginx
container starts.
docker ps Lists all running containers with docker ps <options> docker ps
or or
docker their IDs, names, status, and other
docker container ls
container ls details. <options>

docker stop Stops a running container by docker stop <op- docker stop
name or ID. tions> <container> my_container
[<container>...]
docker Starts a stopped container. docker start docker start
start <options> my_container
<container>
[<container>...]

docker Restarts a running or stopped docker restart docker


restart container. <options> restart
<container> my_container
[<container>...]
docker rm Removes a stopped container. docker rm docker rm
<options> my_container
<container>
[<container>...]
docker exec Executes a command inside a docker exec docker exec -it
<container> my_container ls
running container.
<command>
[<args>...]
Docker 101

Options
Some of the common options you can use with docker run are given in the following table.

Option Description

-d, --detach Runs the container in the background and prints the container ID in the terminal.

-p, --publish Maps a host port to a container port (e.g., -p hostPort:containerPort).

-e, --env Sets environment variables (e.g., -e ENV_VAR=value).

--name Assigns a specific name to the container (e.g., --name my_container). This helps in
managing and referencing containers easily.

-v, --volume Binds mount a volume (e.g., -v host_path:container_path).

--network Connects the container to a specific network.

--restart Defines a restart policy (e.g., --restart always).

--rm Automatically removes the container when it exits.

-it Runs the container in interactive mode with a terminal (e.g., -it ubuntu bash). -i
keeps stdin open for command input, while -t allocates a pseudo-terminal for
a better interactive experience.

--init Runs an init process inside the container to manage zombie processes.

--user Specifies the user to run the container as (e.g., --user user:group).

--cpus Limits the number of CPUs available to the container (e.g., --cpus="2").

--memory Limits the memory usage (e.g., --memory="256m").

--gpus Specifies GPU resources (e.g., --gpus all).

--read-only Mounts the container’s filesystem in read-only mode.

--entrypoint Overrides the default entry point of the image.

--workdir, -w Sets the working directory inside the container (e.g., -w /app).

--tmpfs Mounts a tmpfs (temporary filesystem) (e.g., --tmpfs /tmp).

Docker Registries
A Docker registry is a server-side application that allows you to host private or public repositories for
Docker images. You can set up your own Docker registry server using the official Docker Registry
image. This is useful for organizations that need to keep their images private or have specific require-
ments for image storage.

Docker Hub
Docker Hub is a cloud-based service provided by Docker Inc. It is the default public Docker registry.
Docker Hub hosts public repositories but also supports private repositories for a fee (one private
repository is free). The following commands interact with Docker Hub through Docker CLI commands
for pushing, pulling, and managing Docker images.
Docker 101

Command Purpose Syntax Example


docker Used to log in to Docker Hub. docker login docker login
login <options> -u my_user -p
[<server>] my_password

docker Used to log out from Docker Hub. docker logout docker logout
logout [<server>]

docker Searches for images on Docker docker search docker search


search Hub. <options> TERM nginx

docker pull Pulls an image from a Docker docker pull docker pull
<options> ubuntu:20.04 or
registry (from Docker Hub by
NAME[:tag|@digest] docker pull
default). myregistry.
example.com/
my_image:my_tag

General Docker Management


Docker environments can be managed with commands for help, versioning, and system resource usage.

Docker Help Commands

Command Purpose Syntax Example

docker help Used to get help on Docker commands. docker help docker help
[<command>] run

Version and Information Commands

Command Purpose Syntax Example

docker Shows Docker version information. docker version docker version


version <options>

docker info Shows detailed information about your docker info docker info
<options>
Docker setup.

System and Disk Usage Commands

Command Purpose Syntax Example

docker Shows disk usage for Docker images, docker system docker system
system df containers, volumes, and build the cache. df <options> df

docker Removes unused data (containers, docker system docker system


system prune <options> prune -a
images, volumes, networks).
prune
Docker 101

Docker Volumes
Volumes are used for handling persistent data in Docker. Using them ensures that your containers can
access and store data even as they stop, start, or are recreated.

Command Purpose Syntax Example


docker Creates a new volume. docker volume docker volume
volume create <options> create
create [<volume_name>] my_volume

docker Lists all volumes. docker volume ls docker volume


volume ls <options> ls

docker Removes one or more volumes. docker volume rm docker volume


volume rm <options> rm my_volume
<volume>
[<volume>...]

docker Removes all unused volumes. docker volume docker volume


volume prune prune
prune

docker Displays detailed information docker volume docker volume


volume about a volume. inspect <options> inspect
inspect <volume> my_volume
[<volume>...]

Mounting Docker Volumes

Command Purpose Syntax Example

docker Mounts a volume in a container. docker run -v docker run -v


run -v [volume_name]: my_volume:/
[path] [image] data my_image
or
docker run
--volume
[volume_name]:
[path] [image]

docker Allows mounting differently, such as docker run docker run


run --mount bind mounts, tmpfs, or volumes. --mount --mount
type=volume, type=volume,
source= source=my_
[volume_name], volume,
target=[path] target=/data
[image] or my_image
docker run
--mount
type=bind,
source=
[source_path],
target=[path]
[image] or
docker run
--mount type=
tmpfs,target=
[path] [image]

Note: Use -v for bind mounts to directly link host directories to containers. Use --mount for more
options, including bind, volume, and tmpfs mounts for temporary data.
Docker 101

Networking
Networking in Docker allows containers to communicate with each other and the outside world.

Command Purpose Syntax Example


docker Creates a new network. docker network docker network
network create <options> create
create <network_name> my_network

docker Lists all networks. docker network docker network


network ls ls <options> ls

docker Displays detailed information docker network docker network


network about a network. inspect <op- inspect
inspect tions> <network> my_network
[<network>...]

docker Removes one or more networks. docker network rm docker network


network rm <options> rm my_network
<network>
[<network>...]

docker Removes all unused networks. docker network docker network


network prune <options> prune
prune

Network Driver Option


You can specify the network driver using the --driver <driver> option while creating the network.

Driver Purpose

bridge It is a default network driver for standalone containers.

overlay It creates a multi-host network.

macvlan It assigns a MAC address to a container.

Attaching a Network to a Docker Container

Command Purpose Syntax Example

docker Connects a container to a network. docker network docker network


network connect connect
connect <network> my_network
<container> my_container

docker Disconnects a container from a docker network docker network


network network. disconnect disconnect
disconnect <network> my_network
<container> my_container

docker Creates and runs a new container docker run docker run
run connected to a specified network. --network --network
--network <network> my_network
<image> my_app
Docker 101

Debugging and Logs


Docker containers are monitored by examining logs and detailed container information.

Command Purpose Syntax Example


docker logs Fetches the logs of a container. docker logs docker logs
<options> my_container
<container>

docker Views detailed information about a docker inspect docker inspect


inspect container. <options> <name> my_container

docker Displays real-time resource docker stats docker stats


stats <options>
usage statistics for containers.
[<container>...]

Docker Compose
Docker Compose simplifies the management of multi-container Docker applications using a
docker-compose.yml file

Docker Compose File Example


An example of the docker-compose.yml file is:

services:
web:
image: nginx:latest
ports:
- "8080:80"
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- db_data:/var/lib/mysql

volumes:
db_data:

Docker Compose Commands


Command Purpose Syntax Example

docker Starts up all services defined in the docker-compose docker-compose


-compose up docker-compose.yml file. up <options> up

docker Stops and removes all services docker-compose docker-compose


-compose defined in the docker-compose.yml down <options> down
down
file.

docker Starts services defined in the docker-compose docker-compose


-compose docker-compose.yml file that start [<ser- start web
start vice>...]
are stopped.

docker Restarts services defined in the docker-compose docker-compose


-compose docker-compose.yml file. restart restart db
restart [<service>...]

docker Builds or rebuilds services defined docker-compose docker-compose


-compose in the docker-compose.yml file. build <options> build
build [<service>...]

docker Views output from containers. docker-compose docker-compose


-compose logs <options> logs
logs [<service>...]

You might also like