Docker Container
Show help
docker help <command>
Show processes in container
docker top <container>
Execute command in container
docker exec -it<container_name> <command>
Example
docker exec -it my_xenial /bin/bash
Example
docker help run
Download of Docker container
docker pull <image_name>:<version>
Start Docker Container
docker run [<options>] <image_name>[:<tag>] '[<command>] [<args>...]
Example
docker run hello-world
Start interactive
docker run -i -t ubuntu /bin/bash
Starts ubuntu container in interactive mode, prompt in terminal
Detached start (Background)
docker run --name hello_docker -d skimpytoo/hello-docker
Set environmentvariables
Docker run -e VARIABLE1=VAL1 -e VARIABLE2=VAL2
Start explicit version
docker run <image_name>:<version-tag>
Example
docker run ubuntu:xenial
docker pull ubuntu:xenial
Port forwarding
docker run –name <name> -d -p 8080:80 <image_name>
ContainerPort 80-> Port on outside 8080
Example
docker run --name test-nginx -d -p 8080:80 nginx
Remove Container after exit
docker run --name hello_docker -d -rm skimpytoo/hello-docker
Mount Volume
--v | --volume [<VOLUME_NAME >]:<mount_path>[:<options>]
or
--mount <key>=<value> [,<key>=<value> [,…]]
Example
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
docker container run -d --name nginx --network test_net nginx
List containers
docker container ls [<optionen>]
Examples
docker container ls List all running containers
docker container ls -all List running and stopped containers
Connect terminal with container
docker container attach [<OPTIONEN>] <CONTAINER>
Stop container
docker stop <container_name>
Example
docker stop hello-docker
Restart container
docker restart <container_name>
Kill container
docker kill [<optionen>] <container> [<container> …]
Example
docker kill hello-docker
Remove container
docker container rm <container_name>
Example
docker container rm hello-docker
List running container
docker container ls
Images
Download images
docker image pull <image_name>:<version>
List downloaded images
docker image ls
Search image
docker search <image name>
Example
docker search hello-world
Create own images
docker build -t skimpytoo/hello-docker .
DockerFile:
FROM ubuntu:latest
CMD [“/bin/bash”]
Publish in dockerhub
Login
docker login
Push to dockerhub
docker push [OPTIONS] NAME[:TAG]
Examples
docker push <DOCKER_ID>/hello-docker
Volumes
Create volume
docker volume create <volume_name>
List volumes
docker volume ls
Get detailinformationen
docker volume inspect <volume_name>
Volume löschen
docker volume rm <volume_name>
Logs
Show logs
docker logs <container_name>
Options
-f, --follow Add new incoming loglines
--tail <number> Number of loglines
--details Additional information
--timestamp, -t With timestamp
Change log driver
docker run --name example_app --log-driver none -d -p 8808:80 test/example-app
Persist log entries
docker run --name example_app -d -p 8808:80 -v volumename:/var/log test/example-app
Network
List networks
docker network ls
Standard: none, host, bridge
‚overlay‘ Network that can connect multiple networks
```cmd
## Network details
```cmd
Docker network inspect <network>
Show networks
docker network create [<OPTIONEN>] <NAME>
Examples
docker network create –d bridge test_net
Neues Netzwerk, basierend auf bridge
Connect container with network
docker network connect [<OPTIONEN>] <NETZWERK> <CONTAINER>
Example
docker network connect demo_net nginx
RemoveContainer von Netzwerk trennen
docker network disconnect [<OPTIONS>] <NETZWERK> <CONTAINER>
Example
docker network disconnect test_net nginx
Docker Compose
Searches per default for
- ./docker-compose.yml
- ./docker-compose.yaml
Start
docker-compose up -d
Stop
docker-compose down
Logs
docker-compose logs
Prints the logs of all containers
Scale
docker-compose up --scale redis-master=3 –d
or inside docker-compose.yml
scale: 3 Unter Service
Environmentvariables
Searches by default in
.env
You can use the variables in docker-compose.yaml like
${VARIABLEN_NAME}
Example
.env:
NGINX_VERSION=1.17.7
docker-compose.yaml:
services:
my_web:
image: "nginx:${NGINX_VERSION}"
Defaultvalues
: after variablename defines the default
Example
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
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
environment:
- DATABASE
- BRANCH
Dependent Container
In docker-compose.yml
services:
database:
…
wordpress:
depends_on:
- database
Info
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
docker swarm init
Creates a manager node on the current node
Token to join manager
docker swarm join-token worker | manager
Add manager node
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
docker node ls
Informationen about node
docker node inspect –pretty a1b3 //a1b3= Teil der Node-Id
Show running tasks in node
Docker node ps [node]
Remove node out of swarm
docker swarm [--force] leave
- Managers should be removed last.
- Managers must be specified with –force, otherwise it will be ignored.
Remove node
docker node rm [node]
Geht erst, wenn der Node aus dem Swarm entfernt ist
Promote to manager node
docker node promote [node]
Degrade/Demote
docker node demote [node]
Services
Create service
docker service create <image>
- -p Portmapping (like docker run)
Show all services
docker service ls
Show tasks in service
docker service ps <node>
Remove service
docker service rm <service>
Update docker service
docker service update <service>
Example
docker service update --replicas=6 my_service
docker service update --replicas=3 --publish-add published=8080,target=80 my_service
Change scaling
docker service scale my_service=4
Undo changes
Docker service rollback my_service
Log services/tasks
docker service logs service|task
Details about service
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
docker config create <Config_name> <Config-File>
or
docker config create <Config_name> - Liest von StdIn ein
List configs
docker config ls
Show details of config
docker config inspect --pretty <config_name>
Pass config to service
docker service create --name my_redis --config my_config --replicas=3 redis:alpine
Remove config
First remove from all services
docker service update --config-rm my_config my_redis
after that
docker config rm my_config
Docker Secrets
Ähnlich wie config nur verschlüsselt und nur für
Erzeugen eines Secret
docker secret create <secret_name> <Config-File>
or
docker secret create <secret_name> - Liest von StdIn ein
List all secrets
docker secret ls
Show details of secret
docker secret inspect --pretty <secret_name>
Pass secret to service
docker service create --name my_redis --secret my_secret --replicas=3 redis:alpine
Remove secret
First remove from all services
docker service update --secret-rm my_secret my_redis
after that
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