Docker
Docker Container & Images
[Docker https://www.docker.com/]
- Automatisiertes Deployment - Durch automatisierte Builds - Isolierung von Systemen für mehr Sicherheit - Einfaches Deployment von Third-Party Software - Skalierung
systemctl start docker systemctl stop docker
systemctl enable docker
systemctl disable docker
Tutorial docker cli, docker-compose, docker swarm
Docker cli
Version
$ docker -v $ docker-compose -v
System
$ docker system info
system-wide information
$ docker system info
docker disk usage
$ docker system df
real time events from the server
$ docker system events
Status
running containers
$ 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
real time status
$ docker stats $ docker top [CONTAINER]
Images
Suche im Docker HUB nach Images
$ docker search [NAME]
download image
$ docker pull [NAME]
image history
$ docker image history [NAME] or [IMAGE ID]
$ docker images -aq $ docker images rm $(docker ps -aq) $ docker system prune
View all Images
$ docker images
$ docker image list
$ docker image ls
View Image by name
$ docker images alpine
$ docker images history [IMAGENAME]
$ docker images --digests
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 -f $(docker images -a -q)
Image commands
$ docker image build # history # import # inspect # load # ls # prune # pull # push # rm # save # tag #
Export
export container, these commands has the same result
$ docker export nginx > nginx_latest.tar $ docker export --output="nginx_latest" nginx
Volume
$ docker volume ls
Network
Networking overview $ docker network ls $ docker network create my-network 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"] }
docker bash 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
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
run alpine cli sh
$ docker run -it alpine /bin/sh
$ docker run busybox
$ docker run busybox echo "BUSYBOX"
$ docker run -it busybox sh
$ docker run -p 5000:80 nginx
$ docker run -p 5000:80 -d nginx
$ docker run -itd --name alpine1 -v $(pwd):/home alpine
run alpine cli detached
$ docker run -it -d --name MYALPINE -p 8080:80 alpine
run alpine cli with entrypoint
$ docker run -it --name MYALPINE -p 8080:80 --entrypoint /bin/sh alpine
$ docker run -it --name MYALPINE -p 8080:80 --entrypoint /bin/sh alpine $ docker run -d --TABTA $ docker run -it --rm busybox $ docker run -it --rm redis redis-cli $ docker run redis $ docker run -it --rm micro-busybox /bin/sh
EXEC
$ docker exec -it [container-name/id] /bin/sh
Volume/Directory
$ docker run -it --name MYALPINE -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
docker bash stop/kill images
$ docker stop [CONTAINER]
$ docker kill [CONTAINER]
BACKUP IMAGE, CONTAINER VOLUME
Image commit
$ docker commit [RUNNING_CONTAINER_ID] [MY_NEW_IMAGE_NAME] $ docker commit alpine1 alpine:v1 Create Image from running container
Image save
$ docker save busybox > busybox.tar Save one or more images to a tar archive
Image load
$ docker load < busybox.tar Load an image from a tar archive or STDIN
Container export
$ docker export alpine > alpinebkp.tar $ docker export --output="latest.tar" red_panda Export a container’s filesystem as a tar archive. The docker export command does not export the contents of volumes associated with the container.
Tar filesystem > Image import
$ cat alpine1.tar | docker import - alpine2:v2 Import the contents from a tarball to create a filesystem image
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. Here is a comparison of the syntax for each flag.
If you need to specify volume driver options, you must use --mount.
docker File
docker compose
docker network COMMAND
docker create
- create volume
docker volume create [OPTIONS] [VOLUME]
docker start -a xxx
docker start
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://192.168.109.152:3001 -p 3001:2368 ghost
docker attach [CONTAINER_ID]