...
... ... ...
docker 之獨孤九劍
... ... ...
1. install Docker Desktop:
refer to: https://docs.docker.com/desktop/
$ docker info
$ docker run hello-world
.
2. Docker Images to Containers
// List images
// Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
$ docker images
// docker run: Run a command in a new container
// Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
// docker run -ti: terminal interactive (可互動模式)
$ docker run -ti ubuntu:latest bash
// List containers
// Usage: docker ps [OPTIONS]
$ docker ps
// List containers -all
$ docker ps -a
// List containers -latest
// Show the latest created container (includes all states): 列出最後 created 的 container, 通常用於看不正常關閉的 container
$ docker ps -l
.
3. Docker Containers to Images (docker commit)
$ docker run -ti ubuntu:latest bash # touch AaA_IMAGE_V2
$ docker ps -l
$ docker images
// docker commit: Create a new image from a container's changes
// Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
$ docker commit 3597f6ac186f ubuntu-test:2.0
等同於
// docker tag: Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
// Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
$ docker tag 0eb2c4ef97cd05e22d316195efdeb908289b0bf1aa8adf157da4955c00e8e505 ubuntu-test:2.0
// Verify $ docker run -ti ubuntu-test:2.0 bash
.
4. Docker run
// docker run --rm : Automatically remove the container when it exits (若 exit; 自動移除 container)
$ docker run --rm -ti ubuntu-test:2.0 sleep 5
$ docker run -ti ubuntu bash -c "sleep 3; echo all done"
// docker run -d: --detach Run container in background and print container ID
// [AaA]: 就是類似 nohup 的概念, 在背景執行.
$ docker run -d -ti ubuntu bash
// 從 detach 恢復到 attach mode
$ docker attach lucid_rosalind
// [AaA]:密技 亦可以在 terminal 按 Ctrl+p Ctrl+q 達到 detach 的境界.
5. Docker exec
// docker exec: Run a command in a running container; 通常用於 debug & DBA administration
// Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
$ docker exec -ti lucid_rosalind bash// or
$ docker exec -ti lucid_rosalind cat test.txt
. 6. Docker logs
// docker logs: Fetch the logs of a container
// Usage: docker logs [OPTIONS] CONTAINER
// 範例把 cat 打錯成 catt
$ docker run --name example -d ubuntu bash -c "catt /etc/passwd"
$ docker logs example
bash: line 1: catt: command not found
7. Stopping and Removing Containers
$ docker run -ti ubuntu bash
// docker kill: Kill one or more running containers
// Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]$ docker ps
$ docker kill cranky_lamarr
// docker rm: Remove one or more containers (對正在 running 的 container 無效, 必須先 kill 再 rm)
// Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
$ docker rm cranky_lamarr
// 移除 images 的指令:
// docker rmi: Remove one or more images
// Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
.
8. Exposing ports: docker port
// docker run -p outside-port:inside-port/protocol (tcp/udp)
// docker port: List port mappings or a specific mapping for the container
// Usage: docker port CONTAINER [PRIVATE_PORT[/PROTO]]
$ docker run --rm -ti -p 12345:12345 -p 12346:12346 --name echo-server ubuntu:14.04 bash
$ docker port echo-server
12345/tcp -> 0.0.0.0:12345
12346/tcp -> 0.0.0.0:12346
在第一視窗 listen port 12345 & 12346
# nc -lp 12345 | nc -lp 12346
在第二視窗
$ docker run --rm -ti ubuntu:14.04 bash
# nc host.docker.internal 12345
在第三視窗
$ docker run --rm -ti ubuntu:14.04 bash
# nc host.docker.internal 12346
.
// [AaA]: 若前面不指定 outside-port, 則會隨機作 port mapping dynamically
// UDP example
$ docker run --rm -ti -p 23456:23456/udp --name echo-server ubuntu:14.04 bash
在第一視窗
# nc -ulp 23456
在第二視窗
$ docker ps
$ docker port echo-server
$ docker run --rm -ti ubuntu:14.04 bash
# nc -u host.docker.internal 23456
9. 無招勝有招 docker --help
. .
.
// End.