Docker Cheatsheet

Cheatsheet for Docker

Docker Cheatsheet
Page content

Introduction

This is a quick cheat sheet from Sematext’s Docker Cheatsheet

Docker Cheatsheet

Tutorial series

Get started with Docker: https://docs.docker.com/engine/getstarted/

Installation

Linux

Install script provided by Docker:

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

Or see Installation instructions for your Linux distribution.

Mac OS X

Download and install Docker For Mac

Create Docker VM with Docker Machine

You can use Docker Machine to:

  • Install and run Docker on Mac or Windows
  • Provision and manage multiple remote Docker hosts
  • Provision Swarm clusters

A simple example to create a local Docker VM with VirtualBox:

docker-machine create --driver=virtualbox default
docker-machine ls
eval "$(docker-machine env default)"

Then start up a container:

docker run alpine echo "hello-world"

That’s it, you have a running Docker container.

Container lifecycle

  • Create a container: docker create imageName.
  • Create and start a container in one operation: docker run imageName
    • Remove the container after it stops --rm: docker run --rm alpine ls /usr/lib
    • Attach the container stdin/stdout to the current terminal use -it: docker run -it ubuntu bash
    • To mount a directory on the host to a container add -v option, e.g. docker run -v $HOSTDIR:$DOCKERDIR imageName
    • To map a container port use -p $HOSTPORT:$CONTAINERPORT: docker run -p 8080:80 nginx
    • To run the container in background use -d switch: docker run -d -p 8080:80 nginx
    • Set container name: docker run --name myContainerName imageName
    • Example to run nginx web server to serve files from html directory on port 8080:
    docker run -d -v $(pwd)/html:/usr/share/nginx/html -p 8080:80 --name myNginx nginx
    # access the webserver
    curl http://localhost:8080

Starting and stopping containers

Executing commands in containers and apply changes

Logging and monitoring

Logging on Docker could be challenging - check Top 10 Docker Logging Gotchas.

docker run -–log-driver syslog –-log-opt syslog-address=udp://syslog-server:514 \
alpine echo hello world
  • Use Logagent for log collection and Sematext Agent for metrics and event monitoring. Create Docker Monitoring App & Logs App in Sematext Cloud to get required tokens and use them instead the placeholders YourContainerToken, YourInfraToken and YourLogsToken in commands below. Start collecting all container metrics, host metrics and Docker events:
docker run -d  --restart always --privileged -P --name st-agent \
-v /sys/kernel/debug:/sys/kernel/debug \
-v /var/run/:/var/run/ \
-v /proc:/host/proc:ro \
-v /etc:/host/etc:ro \
-v /sys:/host/sys:ro \
-v /usr/lib:/host/usr/lib:ro \
-e CONTAINER_TOKEN=YourContainerToken \
-e INFRA_TOKEN=YourInfraToken \
-e JOURNAL_DIR=/var/run/st-agent \
-e LOGGING_WRITE_EVENTS=false \
-e LOGGING_REQUEST_TRACKING=false \
-e LOGGING_LEVEL=info \
-e NODE_NAME=`hostname` \
-e CONTAINER_SKIP_BY_IMAGE=sematext \
sematext/agent:latest

Collect all container logs:

docker run -d --name st-logagent \
-e LOGS_TOKEN=YourLogsToken \
-v /var/run/docker.sock:/var/run/docker.sock \
sematext/logagent

The commands above will collect all container metrics, host metrics, Docker events, and container logs.

Exploring Docker information

Manage Docker images

Docker networks

Data cleanup

Data management commands: