Docker

Aus robopagex.com
Zur Navigation springen Zur Suche springen

Tutorial docker, docker container, docker cli, docker-compose, docker swarm

 - Automatisiertes Deployment
 - Durch automatisierte Builds
 - Isolierung von Systemen für mehr Sicherheit
 - Einfaches Deployment von Third-Party Software
 - Skalierung

Docker keywords
- dokku  
- docker compose 
- docker machine 
- docker daemon 
- docker swarm  
- docker node 
- docker service 
- docker stack 
- docker secret 
Docker OLD | alias bashrc
alias d='docker'
alias dv='docker --version'
alias dp='docker ps'
alias dpa='docker ps -a'
alias di='docker images'
alias ds='docker search'
alias dsi='docker system information'
alias dsd='docker system df' 

Docker links

Docker
Docker hub
Install Docker Engine on Debian
Docker Registry HTTP API V2

Docker system start|stop|enable|disable

 $ systemctl start docker
 $ systemctl stop docker
 $ systemctl enable docker
 $ systemctl disable docker

Docker child commands description

https://docs.docker.com/engine/reference/commandline/docker/

Docker system information

Version
$ docker -v
$ docker-compose -v
System
$ docker system info 
docker disk usage 
$ docker system df 
real time events from the server 
$ docker system events

Docker location | change docker location

$ systemctl stop docker
$ nano daemon.json
 {
  "data-root": "/MYPATH"
 }
$ rsync -aP /var/lib/docker/ /MYPATH
$ cp -var /var/lib/docker/ /MYPATH
$ mv /var/lib/docker /var/lib/docker.old 
$ systemctl stop docker
$ rm -rf /var/lib/docker.old

Status | running containers | runned containers

docker ps description
$ docker ps
$ docker ps --all
$ docker ps -a
$ docker ps -l 
$ docker ps -n=2 
$ docker ps -n=2 -s 
$ docker ps -as 
$ docker container ls -a 
$ docker ps -a -f status=running 
$ docker ps -aq 
$ docker ps *  
-a, --all[=false] Show all containers (default shows just running) 
-f, --filter= Filter output based on conditions provided 
--format="" Pretty-print containers using a Go template 
-h, --help[=false] help for ps 
-n, --last=-1 Show n last created containers (includes all states) 
-l, --latest[=false] Show the latest created container (includes all states) 
--no-trunc[=false] Don't truncate output 
-q, --quiet[=false] Only display numeric IDs 
-s, --size[=false] Display total file sizes 

Real time status

$ docker stats
$ docker top [CONTAINER]

Images SEARCH|PULL|LIST|HISTORY

Docker image controll
docker image description
Image commands
   $ docker image
   build        #
   history      #
   import       #
   inspect      #
   load         #
   ls           #
   prune        #
   pull         #
   push         #
   rm           #
   save         #
   tag          #
docker image build 
Build an image from a Dockerfile 
docker image history 
Show the history of an image 
docker image import 
Import the contents from a tarball to create a filesystem image 
docker image inspect 
Display detailed information on one or more images 
docker image load 
Load an image from a tar archive or STDIN 
docker image ls 
List images 
docker image prune 
Remove unused images 
docker image pull 
Pull an image or a repository from a registry 
docker image push 
Push an image or a repository to a registry 
docker image rm 
Remove one or more images 
docker image save 
Save one or more images to a tar archive (streamed to STDOUT by default) 
docker image tag 
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE  
Suche im Docker HUB nach Images
docker search description
$ docker search [NAME] 
Download image
docker pull description
$ docker pull [NAME] 
View all Images
$ docker images
$ docker image list
$ docker image ls
$ docker images --digests
$ docker images *
-a, --all[=false] Show all images (default hides intermediate images)
-q, --quiet[=false] Only show numeric IDs
--digests[=false] Show digests
-h, --help[=false] help for images
-f, --filter= Filter output based on conditions provided
--format="" Pretty-print images using a Go template
--no-trunc[=false] Don't truncate output
View Image  by name
$ docker images alpine
$ docker images history [IMAGENAME]
$ docker images --digests

Image history
$ docker image history [NAME] or [IMAGE ID] 
$ docker images -aq 
$ docker images rm $(docker ps -aq) 
docker clean/check system
$ docker system prune 
$ docker system prune --volumes 

Delete Container/Images

$ docker rm [CONTAINER-ID] 
$ docker rm $(docker ps -qa) 
$ docker container rm [CONTAINER ID] 
$ docker container rm $(docker ps -aq) 
$ docker container kill [CONTAINER ID] 
$ docker rm $(docker ps -a -q -f status=exited) 
$ docker container prune 
$ docker image rm [NAME] 
$ docker image prune 
 Delete all local images 
docker rmi description
$ docker rmi -f $(docker images -a -q) 
$ docker rmi XNAMEX
$ docker rmi test:latest
$ docker rmi -f test:latest

docker run images

$ docker run --help
$ docker run -d --TABTAB
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
$ docker run 
-it                   # interactively
-d                    # detached
---ip 172.17.0.2      # Set Network first, only ip not work
--hostname            # aaaa.de
-p 172.17.0.2:1010:80 # define external port and ip
-p 8080:80            # define external port
--name MYNAME         # define container name
-m 300m               # memory limit 
--memory-swap 1g      #
--cpuset-cups="1"     # 
-rm                   # auto remove container after exit
--workdir "/home"     # Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory
-w="/home"            # ^
-v source:target      # Mount or bind Volume/Directory
-v Apache:/var/www/html 
-v "$PWD":/var/www/html

docker image run container

SINGLE RUN & COMMAND
$ docker run bash echo "WELCOME" 
$ docker run bash ping localhost 
$ docker run bash ip a 
$ docker run bash hostname 
$ docker run bash echo WELCOME && echo HELLO
$ docker run bash echo WELCOME ; echo HELLO
$ docker run bash /bin/sh -c "ls -1 | wc -l"
$ docker run busybox echo "BUSYBOX" 
run command and remove container
$  docker run --rm bash hostname 
RUN interactive & allocate a pseudo-tty
 --interactive, -i keep STDIN open even if not attached
 -t: allocate a pseudo-tty
$ docker run -it bash /bin/sh 
$ docker run -it busybox /bin/sh 
$ docker run -it --name mybash bash /bin/sh 
$ docker run -it alpine /bin/sh
$ docker run -it busybox sh 
run interactive and remove container
$ docker run -it --rm busybox 
$ docker run -it --rm redis redis-cli 
$ docker run -it --rm busybox /bin/sh 
run interactive entrypoint
$ docker run -it --entrypoint /bin/sh bash
$ docker run -it --name mybash --entrypoint /bin/sh bash
$ docker run -it --name MYALPINE -p 8080:80 --entrypoint /bin/sh alpine
RUN selected user
Connect to docker container as user other than root
$  docker run -it --user nobody busybox
RUN in the detached mode -d
$ docker run -it -d bash 
$ docker run -it -d --rm busybox 
$ docker run -it -d --name MYALPINE -p 8080:80 alpine
RUN with Volume/Directory
   $ docker run -it --name MYALPINE -v ${PWD}:/home alpine
   $ docker run -itd --name alpine1 -v $(pwd):/home alpine 
   $ docker run -it --name MYALPINE -w='/home' -v ${PWD}:/home alpine
   $ docker run -it --name MYALPINE --entrypoint /bin/sh --workdir '/home' -v ${PWD}:/home alpine
BASH
Start a bash container|restart always
$ docker run -d --name bash-single --restart=always bash
$ docker update --restart=always [CONTAINER ID] 
$ docker update --restart=on-failure [CONTAINER ID]
APACHE
$ docker run -d -p 1111:80 --name apache-single -v "$PWD":/var/www/html httpd 
APACHE/PHP
$ docker run -dit -p 1000:80 --name apache-php-5 -v "$PWD":/var/www/html php:5-apache 
$ docker run -dit -p 1001:80 --name apache-php-7 -v "$PWD":/var/www/html php:7-apache 
$ docker run -dit -p 1002:80 --name apache-php-8 -v "$PWD":/var/www/html php:8-apache 
MARIADB|MYSQL
$ docker run -dit --name mysqldb --net mynet -p 3306:3306 -e MYSQL_ROOT_PASSWORD=MYPASSWORD -d mysql:latest
PHPMYADMIN
PMA_HOST - define address/host name of the MySQL server.
$ docker run -dit --name myadmindb -d -e PMA_HOST=X.X.X.X --net mynet -p 80:80 phpmyadmin
GHOST-BLOG
$ docker run -d --name some-ghost -v Ghost-Blog:/var/lib/ghost/content ghost 
$ docker run -d --name some-ghost -v Ghost-Blog:/var/lib/ghost/content -e url=http://X.X.X.X:5001 -p 5001:2368 ghost 

RUN and EXEC

The docker exec command runs a new command in a running container.
docker exec description
$ docker exec -d [CONTAINER] touch /tmp/file
$ docker exec -it [CONTAINER] /bin/sh
$ docker exec -it [CONTAINER] bash 
$ docker exec -it -e VAR=1 [CONTAINER] bash

$ docker exec -ti [CONTAINER] sh -c "echo a && echo b"
$ docker exec -ti [CONTAINER] bash -c "echo a && echo b"
$ docker exec -it [CONTAINER] pwd
$ docker exec -it -w /usr/local/ [CONTAINER] pwd

STOP|KILL CONTAINER

$ docker stop [CONTAINER]
--time , -t 	10 	Seconds to wait for stop before killing it
$ docker kill [CONTAINER]
--signal , -s 	KILL 	Signal to send to the container
$ docker kill --signal=SIGHUP my_container
$ docker kill --signal=HUP my_container
$ docker kill --signal=1 my_container

Volume

Use volumes description
Volume, bind/mount, tmpfs mount
If your container generates non-persistent state data, consider using a tmpfs mount to avoid storing the data anywhere permanently, 
and to increase the container’s performance by avoiding writing into the container’s writable layer.
Volumes use rprivate bind propagation, and bind propagation is not configurable for volumes. 
In general, --mount is more explicit and verbose. 
The biggest difference is that the -v syntax combines all the options together in one field, while the --mount syntax separates them. 
If you need to specify volume driver options, you must use --mount.
-v|--mount
$ docker volume ls 
docker volume create description
docker volume create [OPTIONS] [VOLUME] 
$ docker volume create MYVOLUME 
$ docker volume inspect MYVOLUME 
$ docker volume rm MYVOLUME 

COPY DATA IN|OUT CONTAINER

To copy a file from the local file system to a container, run the command for Docker container or Kubernetes pod, respectively:
docker cp <src-path> <container>:<dest-path>
To copy a file from the container to the local file system, use:
docker cp <container>:<src-path> <local-dest-path>

Network

Networking overview
Assign static IP to Docker container
$ docker network ls
$ docker network create my-network
$ docker network create --subnet=172.18.0.0/16 mynet123
$ docker run --net mynet123 --ip 172.18.0.22 -it ubuntu bash
default bridge
$ docker network create --driver bridge my-network
$ docker network inspect my-network
$ docker network inspect bridge

Configure the default bridge network
   To configure the default bridge network, you specify options in daemon.json. Here is an example daemon.json with several options specified. Only specify the settings you need to customize.
   {
      "bip": "192.168.1.5/24",
      "fixed-cidr": "192.168.1.5/25",
      "fixed-cidr-v6": "2001:db8::/64",
      "mtu": 1500,
      "default-gateway": "10.20.1.1",
      "default-gateway-v6": "2001:db8:abcd::89",
      "dns": ["10.20.1.2","10.20.1.3"]
     }
    

Build Image

Build image from Dockerfile
$ docker build -t [NAME]:[TAG] .

BACKUP IMAGE, CONTAINER VOLUME

Image, commit
Create Image from running container
$ docker commit [RUNNING_CONTAINER_ID] [MY_NEW_IMAGE_NAME] 
$ docker commit alpine1 alpine:v1 
Image, save one or more images to a tar archive
$ docker save busybox > busybox.tar 
Image, load an image from a tar archive or STDIN
$ docker load < busybox.tar 
Container, export a container’s filesystem as a tar archive.
The docker export command does not export the contents of volumes associated with the container.
$ docker export alpine > alpinebkp.tar 
$ docker export --output="latest.tar" red_panda 
Tar filesystem > Image, import the contents from a tarball to create a filesystem image
$ cat alpine1.tar | docker import - alpine2:v2 

EXPORT

export container, these commands has the same result
$ docker export nginx > nginx_latest.tar
$ docker export --output="nginx_latest" nginx


Docker Builder cache

Load cache layers for build
The FROM instruction is the only line that is not affected by the no-cache argument. If the base image is present in the machine, it won’t be pulled again.
$ docker build -t print-date-time --no-cache .
It will pull the latest version of any base image(s) instead of reusing whatever you already have tagged locally
$ docker build -t print-date-time --pull . 

Docker create

Creates a new container from the specified image, without starting it. 
$ docker container create -it --name mycontainer alpine 

Docker start

Start one or more stopped containers 
$ docker start my_container 

Docker attach

Attach local standard input, output, and error streams to a running container. 
This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.
$ docker attach [OPTIONS] CONTAINER

DOCKERFILE

Dockerfile Description

Docker Groups