DevOps,  Docker

Docker cheatsheet

Table of Contents


docker exec -ti CONTAINER_ID command_to_executeRuns, interactive mode, a command over a container already running (e.g. starts the shell, /bin/sh)
docker rmi IMAGE_NAMEDeletes the image (all existing containers based on this image must have been stopped and deleted upfront)
docker rm CONTAINER_IDDeletes the container
docker stop CONTAINER_IDStops the container
docker inspect CONTAINER_IDLists all attributes of container with id CONTAINER_ID
Generic docker commands


docker network listLists all configured networks
docker inspect NETWORK_IDDisplays all attributes of network with id NETWORK_ID
docker run 
–name alpine-2 
–network=none alpine
Runs a new container with name = alpine-2 and attachs it to network “none”, using image name = alpine
docker network create 
–driver bridge 
Creates a new network with type = bridge, subnet and gateway Network name will be wp-mysql-network
docker run 
-e MYSQL_ROOT_PASSWORD=db_pass123 
–name mysql-db 
–network wp-mysql-network mysql:5.6
Runs a new container (detached mode) assigning an environment variable MYSQL_ROOT_PASSWORD=db_pass123 and container name = mysql-db. The container will be attached to the network with name wp-mysql-network. Image used to run the container: mysql:5.6
docker run –network=wp-mysql-network 
-e DB_Host=mysql-db -e DB_Password=db_pass123 
-p 38080:8080 
–name webapp 
–link mysql-db:mysql-db 
Runs a new container from image kodekloud/simple-webapp-mysql and with name webapp.Container gets attached to network with name wp-mysql-network 2 environment variables are defined Internal (container) network port 8080 is exposed to port 38080 (host) The container gets linked to container with name mysql-db Runs in detached mode
Docker networking most common commands

Storage Management

docker run -v /opt/data:/var/lib/mysql 
-d –name mysql-db
-e MYSQL_ROOT_PASSWORD=db_pass123 mysql
Runs a new container in detached mode and with name mysql-dbMaps the container directory /var/lib/mysql over the host directory /opt/data (bind-mounting) Sets an environment variable Uses the image with name mysql alternative using –mount option (rather than -v)
docker run –mount type=bind,source=/opt/data,target=/var/lib/mysql mysql
docker volume create data_volumeCreates a new persisted volume named as data_volume (will be a new folder at /var/lib/docker/volumes into host file system) and runs a new container mapping the persisted volume to /var/lib/mysql
docker run -v data_volume:/var/lib/mysql mysql
Docker storage management useful commands

Docker image build

Docker file sample:

FROM Ubuntu	Each statement is a layer with its space usage
RUN apt-get update && apt-get -y install python
RUN pip install flask flask-mysql
COPY . /opt/source/code
ENTRYPOINT FLASK_APP=/opt/source-code/ flask run

Command to be issues in order to create an image:

docker build Dockerfile -t username/app-name

Docker registry management

docker run -d -p 5000:5000 –name registry registry:2Creates a new local registry
docker image tag my-image localhost:5000/my-imageTagging an image so that it gets stored on the local registry
docker push localhost:5000/my-imagePushing an image to the local registry
docker pull localhost:5000/my-imagePulling an image from the local registry
Docker registry management useful commands

Log management

docker log -f CONTAINER_IDPrints out logs from CONTAINER_ID (-f = follow)
Docker logs management

Info & Tips

Docker engine

Docker is made up of 3 components:

  • Docker daemon
  • Docker CLI

When you install docker on a linux host, all 3 components are deployed.

Docker CLI can also be installed as single component and then used to run docker commands on a remote host with REST API and Docker daemon.

To run docker commands on a remote host:

docker -H=ip_of_remote_docker_engine:2375 run nginx

Docker cgroups

To limit the % of CPU that can be assigned to a container (.5 = cannot use more than 50% of host CPU):

docker run --cpu=.5 ubuntu

To limit the amount of memory that can be allocated from a container:

docker run --memory=100m ubuntu