Skip to content

Docker Container

Show help

cmd
docker help <command>

Show processes in container

cmd
docker top <container>

Execute command in container

cmd
docker exec -it<container_name> <command>

Example

cmd
docker exec -it my_xenial /bin/bash

Example

cmd
docker help run

Download of Docker container

cmd
docker pull <image_name>:<version>

Start Docker Container

cmd
docker run [<options>] <image_name>[:<tag>] '[<command>] [<args>...]

Example

cmd
docker run hello-world

Start interactive

cmd
docker run -i -t ubuntu /bin/bash

Starts ubuntu container in interactive mode, prompt in terminal

Detached start (Background)

cmd
docker run --name hello_docker -d skimpytoo/hello-docker

Set environmentvariables

cmd
Docker run -e VARIABLE1=VAL1 -e VARIABLE2=VAL2

Start explicit version

cmd
docker run <image_name>:<version-tag>

Example

cmd
docker run ubuntu:xenial
docker pull ubuntu:xenial

Port forwarding

cmd
docker run –name <name> -d -p 8080:80 <image_name>

ContainerPort 80-> Port on outside 8080

Example

cmd
 docker run --name test-nginx -d -p 8080:80 nginx

Remove Container after exit

cmd
docker run --name hello_docker -d -rm skimpytoo/hello-docker

Mount Volume

cmd
--v | --volume [<VOLUME_NAME >]:<mount_path>[:<options>]
or
--mount <key>=<value> [,<key>=<value> [,…]]

Example

cmd
docker run -i -t --name=voltest --mount source=test-vol,target=/test_data ubuntu /bin/bash
docker run -i -t --name=voltest2 -v test-vol:/test_data ubuntu /bin/bash

Use defined network

cmd
docker container run -d --name nginx --network test_net nginx

List containers

cmd
docker container ls [<optionen>]

Examples

cmd
docker container ls 				List all running containers
docker container ls -all		List running and stopped containers

Connect terminal with container

cmd
docker container attach [<OPTIONEN>] <CONTAINER>

Stop container

cmd
docker stop <container_name>

Example

cmd
docker stop hello-docker

Restart container

cmd
docker restart <container_name>

Kill container

cmd
docker kill [<optionen>] <container> [<container> …]

Example

cmd
docker kill hello-docker

Remove container

cmd
docker container rm <container_name>

Example

cmd
docker container rm hello-docker

List running container

cmd
docker container ls

Images

Download images

cmd
docker image pull <image_name>:<version>

List downloaded images

cmd
docker image ls

Search image

cmd
docker search <image name>

Example

cmd
docker search hello-world

Create own images

cmd
docker build -t skimpytoo/hello-docker .
cmd
DockerFile:
FROM ubuntu:latest
CMD [“/bin/bash”]

Publish in dockerhub

Login

cmd
docker login

Push to dockerhub

cmd
docker push [OPTIONS] NAME[:TAG]

Examples

cmd
docker push <DOCKER_ID>/hello-docker

Volumes

Create volume

cmd
docker volume create <volume_name>

List volumes

cmd
docker volume ls

Get detailinformationen

cmd
docker volume inspect <volume_name>

Volume löschen

cmd
docker volume rm <volume_name>

Logs

Show logs

cmd
docker logs <container_name>

Options

cmd
-f, --follow      Add new incoming loglines
--tail <number>   Number of loglines
--details         Additional information
--timestamp, -t   With timestamp

Change log driver

cmd
docker run --name example_app --log-driver none -d -p 8808:80 test/example-app

Persist log entries

cmd
docker run --name example_app -d -p 8808:80 -v volumename:/var/log test/example-app

Network

List networks

cmd
docker network ls

Standard: none, host, bridge
‚overlay‘ Network that can connect multiple networks
```cmd

## Network details
```cmd
Docker network inspect <network>

Show networks

cmd
docker network create [<OPTIONEN>] <NAME>

Examples

cmd
docker network create –d bridge test_net

Neues Netzwerk, basierend auf bridge

Connect container with network

cmd
docker network connect [<OPTIONEN>] <NETZWERK> <CONTAINER>

Example

docker network connect demo_net nginx

RemoveContainer von Netzwerk trennen

cmd
docker network disconnect [<OPTIONS>] <NETZWERK> <CONTAINER>

Example

cmd
docker network disconnect test_net nginx

Docker Compose

Searches per default for

  • ./docker-compose.yml
  • ./docker-compose.yaml

Start

cmd
docker-compose up -d

Stop

cmd
docker-compose down

Logs

cmd
docker-compose logs

Prints the logs of all containers

Scale

cmd
docker-compose up --scale redis-master=3 –d

or inside docker-compose.yml
scale: 3 Unter Service

Environmentvariables

Searches by default in

cmd
.env

You can use the variables in docker-compose.yaml like

cmd
${VARIABLEN_NAME}

Example

cmd
.env:
NGINX_VERSION=1.17.7
docker-compose.yaml:
services:
my_web:
image: "nginx:${NGINX_VERSION}"

Defaultvalues

: after variablename defines the default

Example

cmd
image: "nginx:${NGINX_VERSION:-latest}" 	– latest, if variable is not set, or empty
image: "nginx:${NGINX_VERSION-latest}" - latest, if variable not set
image: "nginx:${NGINX_VERSION:latest}" - latest, if variable empty

Define environment variable in container

cmd
Environment-section in service
services:
ubuntu_ext:

environment:

- DATABASE=my_db or DATABASE: my_db
- BRANCH=develop

To use variables from .env, just use the name

cmd
environment:

- DATABASE
- BRANCH

Dependent Container

In docker-compose.yml

cmd
services:
database:

wordpress:
depends_on:

- database

Info

cmd
docker info

Docker Swarm

Nodes

Manager Nodes

They assign tasks and ensure that the swarm's status always remains correct. There is always a node leader.

Worker Nodes

Executes the tasks

Initialise

cmd
docker swarm init

Creates a manager node on the current node

Token to join manager

cmd
docker swarm join-token worker | manager

Add manager node

cmd
docker swarm join --token SWMTKN-1-3t516o9d5wo36ao2fyh8w5ma4oqmithampjm87jq163r29ihpd-1fyx2ecncnyd5be99oavt8fl4 192.168.65.3:2377

Options

  • --advertise-addr Define address to connect to
  • --replicas=5 How many replicas to create

Show nodes

cmd
docker node ls

Informationen about node

cmd
docker node inspect –pretty a1b3 //a1b3= Teil der Node-Id

Show running tasks in node

cmd
Docker node ps [node]

Remove node out of swarm

cmd
docker swarm [--force] leave
  • Managers should be removed last.
  • Managers must be specified with –force, otherwise it will be ignored.

Remove node

cmd
docker node rm [node]

Geht erst, wenn der Node aus dem Swarm entfernt ist

Promote to manager node

cmd
docker node promote [node]

Degrade/Demote

cmd
docker node demote [node]

Services

Create service

cmd
docker service create <image>
  • -p Portmapping (like docker run)

Show all services

cmd
docker service ls

Show tasks in service

cmd
docker service ps <node>

Remove service

docker service rm <service>

Update docker service

cmd
docker service update <service>

Example

cmd
docker service update --replicas=6 my_service
docker service update --replicas=3 --publish-add published=8080,target=80 my_service

Change scaling

cmd
docker service scale my_service=4

Undo changes

cmd
Docker service rollback my_service

Log services/tasks

cmd
docker service logs service|task

Details about service

cmd
Docker service inspect <service>

Docker Config

Config files can be created and added to the swarm. They are then available to all nodes (text, binary). Only works with swarm services.

Create config

cmd
docker config create <Config_name> <Config-File>
or
docker config create <Config_name> - Liest von StdIn ein

List configs

cmd
docker config ls

Show details of config

cmd
docker config inspect --pretty <config_name>

Pass config to service

cmd
docker service create --name my_redis --config my_config --replicas=3 redis:alpine

Remove config

First remove from all services

cmd
docker service update --config-rm my_config my_redis

after that

cmd
docker config rm my_config

Docker Secrets

Ähnlich wie config nur verschlüsselt und nur für

Erzeugen eines Secret

cmd
docker secret create <secret_name> <Config-File>
or
docker secret create <secret_name> - Liest von StdIn ein

List all secrets

cmd
docker secret ls

Show details of secret

cmd
docker secret inspect --pretty <secret_name>

Pass secret to service

cmd
docker service create --name my_redis --secret my_secret --replicas=3 redis:alpine

Remove secret

First remove from all services

cmd
docker service update --secret-rm my_secret my_redis

after that

cmd
docker secret rm my_secret

Docker Stack

Similar to docker-compose but for swarm services (definition of services, configs, secrets, replicas,…) in a yaml file

Contact: M_Bergmann AT gmx.at