Docker Version: 19.03.5redis
- 容器是爲進程任務而生的
- 容器的生命週期是短暫的
一個容器建議只運行一個進程(容器內PID爲1), 並且這個進程須要在容器內以Foreground方式運行, 不能以Background方式運行. 若是進程退出了, 那麼容器就發現Foreground Stdout不被佔據, 因而容器也隨之退出docker
容器運行過程:bash
- 檢查本地是否存在指定的鏡像, 若是沒有就從指定的倉庫下載
- 利用鏡像啓動一個容器
- 分配一個文件系統, 並在只讀的鏡像層上面掛載一層可讀寫層
- 從宿主機配置的網橋接口中橋接一個虛擬接口到容器中去
- 從地址池配置一個IP給容器
- 執行用戶指定的程序
- 執行完畢後中止容器
runapp
Run a command in a new containercurl
Usage:ide
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
工具
OPTION | COMMENT |
---|---|
--detach | Run container in background and print container ID |
--interactive | Keep STDIN open even if not attached |
--tty | Allocate a pseudo-TTY |
--rm | Automatically remove the container when it exits |
... | ... |
Often use:ui
在後臺運行容器及應用url
docker container run --detach IMAGE [COMMAND] [ARG...]
在前臺運行容器並交互
docker container run --interactive --tty IMAGE /bin/sh
start
Start one or more stopped containers
Usage:
docker container start [OPTIONS] CONTAINER [CONTAINER...]
Often use:
啓動容器
docker container start CONTAINER [CONTAINER...]
stop
Stop one or more running containers
Usage:
docker container stop [OPTIONS] CONTAINER [CONTAINER...]
Often use:
中止容器
docker container stop CONTAINER [CONTAINER...]
kill
Kill one or more running containers
Usage:
docker container kill [OPTIONS] CONTAINER [CONTAINER...]
Often use:
殺掉容器
docker container kill CONTAINER [CONTAINER...]
restart
Restart one or more containers
Usage:
docker container restart [OPTIONS] CONTAINER [CONTAINER...]
Often use:
重啓容器
docker container restart CONTAINER [CONTAINER...]
rm
Remove one or more containers
Usage:
docker container rm [OPTIONS] CONTAINER [CONTAINER...]
OPTION | COMMENT |
---|---|
--force | Force the removal of a running container (uses SIGKILL) |
--volumes | Remove the volumes associated with the container |
... | ... |
Often use:
刪除已退出的容器
docker container rm CONTAINER [CONTAINER...]
刪除正運行的容器
docker container rm --force CONTAINER [CONTAINER...]
刪除容器及掛載卷
docker container rm --volumes CONTAINER [CONTAINER...]
ls
List containers
Usage:
docker container ls [OPTIONS]
OPTION | COMMENT |
---|---|
--all | Show all containers (default shows just running) |
--no-trunc | Don't truncate output |
--quiet | Only display numeric IDs |
--size | Display total file sizes |
--latest | Show the latest created container (includes all states) |
... | ... |
Often use:
列出全部容器及其狀態
docker container ls [ --all ]
顯示最近一次建立容器
docker container ls --latest
列出全部容器的ID
docker container ls --quiet [ --no-trunc ]
列出全部容器的SIZE
docker container ls --size
inspect
Display detailed information on one or more containers
Usage:
docker container inspect [OPTIONS] CONTAINER [CONTAINER...]
Often use:
顯示容器詳細信息
docker container inspect CONTAINER [CONTAINER...]
logs
Fetch the logs of a container
Usage:
docker container logs [OPTIONS] CONTAINER
OPTION | COMMENT |
---|---|
--details | Show extra details provided to logs |
--follow | Follow log output |
--since string | Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes) |
--tail string | Number of lines to show from the end of the logs (default "all") |
--timestamps | Show timestamps |
--until string | Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes) |
top
Display the running processes of a container
Usage:
docker container top CONTAINER [ps OPTIONS]
Often use:
在宿主機上查看容器應用進程
docker container top CONTAINER
stats
Display a live stream of container(s) resource usage statistics
Usage:
docker container stats [OPTIONS] [CONTAINER...]
OPTION | COMMENT |
---|---|
--all | Show all containers (default shows just running) |
--no-stream | Disable streaming stats and only pull the first result |
... | ... |
Often use:
查看全部容器的資源使用狀況統計
docker container stats [ --all ] [ --no-stream ]
查看一個容器的資源使用狀況統計
docker container stats [ --no-stream ] CONTAINER
exec
Run a command in a running container
Usage:
docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]
OPTION | COMMENT |
---|---|
--env list | Set environment variables |
--interactive | Keep STDIN open even if not attached |
--privileged | Give extended privileges to the command |
--tty | Allocate a pseudo-TTY |
--user string | Username or UID (format: <name|uid>[:<group|gid>]) |
--workdir string | Working directory inside the container |
... | ... |
Often use:
執行容器中的命令
docker container exec CONTAINER COMMAND [ARG...]
進入容器的SHELL
docker container exec --interactive --tty CONTAINER /bin/sh
cp
Copy files/folders between a container and the local filesystem
Usage:
docker container cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH
docker container cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH
OPTION | COMMENT |
---|---|
--archive | Archive mode (copy all uid/gid information) |
--follow-link | Always follow symbol link in SRC_PATH |
容器按用途大體可分爲兩類:
服務類容器
如 web server、database等
E.G. 運行httpd容器
docker container run --name="web-server" --detach httpd
工具類容器
如curl容器,redis-cli容器
E.G. 運行curl容器
docker container run --interactive --tty --rm appropriate/curl curl 172.17.0.5