docker學習3:Docker容器使用
Docker 容器使用
Docker 客戶端
docker 客戶端非常簡單 ,我們可以直接輸入 docker 命令來檢視到 Docker 客戶端的所有命令選項。
[root@huixuan ~]# docker
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to (default [])
-l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
volume Manage volumes
Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
[root@huixuan ~]#
可以透過命令 docker command --help 更深入的瞭解指定的 Docker 命令使用方法。
例如我們要檢視 docker stats 指令的具體使用方法:
[root@huixuan ~]# docker stats --help
Usage: docker stats [OPTIONS] [CONTAINER...]
Display a live stream of container(s) resource usage statistics
Options:
-a, --all Show all containers (default shows just running)
--format string Pretty-print images using a Go template
--help Print usage
--no-stream Disable streaming stats and only pull the first result
[root@huixuan ~]#
執行一個web應用
前面我們執行的容器並沒有一些什麼特別的用處。
接下來讓我們嘗試使用 docker 構建一個 web 應用程式。
我們將在docker容器中執行一個 Python Flask 應用來執行一個web應用。
[root@huixuan ~]# docker pull training/webapp # 載入映象
Using default tag: latest
Trying to pull repository docker.io/training/webapp ...
latest: Pulling from docker.io/training/webapp
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
10bbbc0fc0ff: Pull complete
fca59b508e9f: Pull complete
e7ae2541b15b: Pull complete
9dd97ef58ce9: Pull complete
a4c1b0cb7af7: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for docker.io/training/webapp:latest
[root@huixuan ~]# docker run -d -P training/webapp python app.py
e4fbb06d2dda11414252575dd77b2561b23e09c0237fac764076ba61fffa3461
[root@huixuan ~]#
引數說明:
-d:讓容器在後臺執行。
-P:將容器內部使用的網路埠對映到我們使用的主機上。
檢視 WEB 應用容器
使用 docker ps 來檢視我們正在執行的容器
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4fbb06d2dda training/webapp "python app.py" 37 seconds ago Up 35 seconds 0.0.0.0:1024->5000/tcp kickass_wright
[root@huixuan ~]#
這裡多了埠資訊。
PORTS
0.0.0.0:1024->5000/tcp
Docker 開放了 5000 埠(預設 Python Flask 埠)對映到主機埠 32769 上。
這時我們可以透過瀏覽器訪問WEB應用
我們也可以指定 -p 標識來繫結指定埠。
[root@huixuan ~]# docker run -d -p 5000:5000 training/webapp python app.py
0e044f323370c157539fa9e235a37c7b2907e5920e846429562f1f18f3f6ff55
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e044f323370 training/webapp "python app.py" 4 seconds ago Up 3 seconds 0.0.0.0:5000->5000/tcp admiring_goldwasser
e4fbb06d2dda training/webapp "python app.py" 13 minutes ago Up 13 minutes 0.0.0.0:1024->5000/tcp kickass_wright
[root@huixuan ~]#
容器內部的 5000 埠對映到我們本地主機的 5000 埠上。
網路埠的快捷方式
透過docker ps 命令可以檢視到容器的埠對映,docker還提供了另一個快捷方式:docker port,使用 docker port 可以檢視指定 (ID或者名字)容器的某個確定埠對映到宿主機的埠號。
上面我們建立的web應用容器ID為:e4fbb06d2dda kickass_wright
我可以使用docker port e4fbb06d2dda 或docker port kickass_wright來檢視容器埠的對映情況
[root@huixuan ~]# docker port e4fbb06d2dda
5000/tcp -> 0.0.0.0:1024
[root@huixuan ~]# docker port kickass_wright
5000/tcp -> 0.0.0.0:1024
[root@huixuan ~]#
檢視WEB應用程式日誌
docker logs [ID或者名字] 可以檢視容器內部的標準輸出。
[root@huixuan ~]# docker logs -f e4fbb06d2dda
* Running on (Press CTRL+C to quit)
172.17.8.178 - - [01/May/2018 01:35:36] "GET / HTTP/1.1" 200 -
172.17.8.178 - - [01/May/2018 01:35:36] "GET /favicon.ico HTTP/1.1" 404 -
-f:讓 dokcer logs 像使用 tail -f 一樣來輸出容器內部的標準輸出。
從上面,我們可以看到應用程式使用的是 5000 埠並且能夠檢視到應用程式的訪問日誌。
檢視WEB應用程式容器的程式
我們還可以使用 docker top 來檢視容器內部執行的程式
[root@huixuan ~]# docker top e4fbb06d2dda
UID PID PPID C STIME TTY TIME CMD
root 3034 3017 0 09:32 ? 00:00:00 python app.py
[root@huixuan ~]#
檢查WEB應用程式
使用 docker inspect 來檢視Docker的底層資訊。它會返回一個 JSON 檔案記錄著 Docker 容器的配置和狀態資訊。
[root@huixuan ~]# docker inspect e4fbb06d2dda
[
{
"Id": "e4fbb06d2dda11414252575dd77b2561b23e09c0237fac764076ba61fffa3461",
"Created": "2018-05-01T01:32:33.151210372Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3034,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-05-01T01:32:35.091034063Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
....
停止WEB應用容器
[root@huixuan ~]# docker stop e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]#
重啟WEB應用容器
已經停止的容器,我們可以使用命令 docker start 來啟動。
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e044f323370 training/webapp "python app.py" 11 minutes ago Up 11 minutes 0.0.0.0:5000->5000/tcp admiring_goldwasser
[root@huixuan ~]# docker start e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e044f323370 training/webapp "python app.py" 11 minutes ago Up 11 minutes 0.0.0.0:5000->5000/tcp admiring_goldwasser
e4fbb06d2dda training/webapp "python app.py" 24 minutes ago Up 2 seconds 0.0.0.0:1025->5000/tcp kickass_wright
[root@huixuan ~]#
docker ps -l 查詢最後一次建立的容器:
[root@huixuan ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e044f323370 training/webapp "python app.py" 11 minutes ago Up 11 minutes 0.0.0.0:5000->5000/tcp admiring_goldwasser
[root@huixuan ~]#
正在執行的容器,我們可以使用 docker restart 命令來重啟
移除WEB應用容器
我們可以使用 docker rm 命令來刪除不需要的容器
刪除容器時,容器必須是停止狀態,否則會報如下錯誤
[root@huixuan ~]# docker rm e4fbb06d2dda
Error response from daemon: You cannot remove a running container e4fbb06d2dda11414252575dd77b2561b23e09c0237fac764076ba61fffa3461. Stop the container before attempting removal or use -f
[root@huixuan ~]#
[root@huixuan ~]# docker stop e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]# docker rm e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e044f323370 training/webapp "python app.py" 13 minutes ago Up 13 minutes 0.0.0.0:5000->5000/tcp admiring_goldwasser
[root@huixuan ~]#
Docker 客戶端
docker 客戶端非常簡單 ,我們可以直接輸入 docker 命令來檢視到 Docker 客戶端的所有命令選項。
[root@huixuan ~]# docker
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to (default [])
-l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
volume Manage volumes
Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
[root@huixuan ~]#
可以透過命令 docker command --help 更深入的瞭解指定的 Docker 命令使用方法。
例如我們要檢視 docker stats 指令的具體使用方法:
[root@huixuan ~]# docker stats --help
Usage: docker stats [OPTIONS] [CONTAINER...]
Display a live stream of container(s) resource usage statistics
Options:
-a, --all Show all containers (default shows just running)
--format string Pretty-print images using a Go template
--help Print usage
--no-stream Disable streaming stats and only pull the first result
[root@huixuan ~]#
執行一個web應用
前面我們執行的容器並沒有一些什麼特別的用處。
接下來讓我們嘗試使用 docker 構建一個 web 應用程式。
我們將在docker容器中執行一個 Python Flask 應用來執行一個web應用。
[root@huixuan ~]# docker pull training/webapp # 載入映象
Using default tag: latest
Trying to pull repository docker.io/training/webapp ...
latest: Pulling from docker.io/training/webapp
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
10bbbc0fc0ff: Pull complete
fca59b508e9f: Pull complete
e7ae2541b15b: Pull complete
9dd97ef58ce9: Pull complete
a4c1b0cb7af7: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for docker.io/training/webapp:latest
[root@huixuan ~]# docker run -d -P training/webapp python app.py
e4fbb06d2dda11414252575dd77b2561b23e09c0237fac764076ba61fffa3461
[root@huixuan ~]#
引數說明:
-d:讓容器在後臺執行。
-P:將容器內部使用的網路埠對映到我們使用的主機上。
檢視 WEB 應用容器
使用 docker ps 來檢視我們正在執行的容器
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4fbb06d2dda training/webapp "python app.py" 37 seconds ago Up 35 seconds 0.0.0.0:1024->5000/tcp kickass_wright
[root@huixuan ~]#
這裡多了埠資訊。
PORTS
0.0.0.0:1024->5000/tcp
Docker 開放了 5000 埠(預設 Python Flask 埠)對映到主機埠 32769 上。
這時我們可以透過瀏覽器訪問WEB應用
我們也可以指定 -p 標識來繫結指定埠。
[root@huixuan ~]# docker run -d -p 5000:5000 training/webapp python app.py
0e044f323370c157539fa9e235a37c7b2907e5920e846429562f1f18f3f6ff55
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e044f323370 training/webapp "python app.py" 4 seconds ago Up 3 seconds 0.0.0.0:5000->5000/tcp admiring_goldwasser
e4fbb06d2dda training/webapp "python app.py" 13 minutes ago Up 13 minutes 0.0.0.0:1024->5000/tcp kickass_wright
[root@huixuan ~]#
容器內部的 5000 埠對映到我們本地主機的 5000 埠上。
網路埠的快捷方式
透過docker ps 命令可以檢視到容器的埠對映,docker還提供了另一個快捷方式:docker port,使用 docker port 可以檢視指定 (ID或者名字)容器的某個確定埠對映到宿主機的埠號。
上面我們建立的web應用容器ID為:e4fbb06d2dda kickass_wright
我可以使用docker port e4fbb06d2dda 或docker port kickass_wright來檢視容器埠的對映情況
[root@huixuan ~]# docker port e4fbb06d2dda
5000/tcp -> 0.0.0.0:1024
[root@huixuan ~]# docker port kickass_wright
5000/tcp -> 0.0.0.0:1024
[root@huixuan ~]#
檢視WEB應用程式日誌
docker logs [ID或者名字] 可以檢視容器內部的標準輸出。
[root@huixuan ~]# docker logs -f e4fbb06d2dda
* Running on (Press CTRL+C to quit)
172.17.8.178 - - [01/May/2018 01:35:36] "GET / HTTP/1.1" 200 -
172.17.8.178 - - [01/May/2018 01:35:36] "GET /favicon.ico HTTP/1.1" 404 -
-f:讓 dokcer logs 像使用 tail -f 一樣來輸出容器內部的標準輸出。
從上面,我們可以看到應用程式使用的是 5000 埠並且能夠檢視到應用程式的訪問日誌。
檢視WEB應用程式容器的程式
我們還可以使用 docker top 來檢視容器內部執行的程式
[root@huixuan ~]# docker top e4fbb06d2dda
UID PID PPID C STIME TTY TIME CMD
root 3034 3017 0 09:32 ? 00:00:00 python app.py
[root@huixuan ~]#
檢查WEB應用程式
使用 docker inspect 來檢視Docker的底層資訊。它會返回一個 JSON 檔案記錄著 Docker 容器的配置和狀態資訊。
[root@huixuan ~]# docker inspect e4fbb06d2dda
[
{
"Id": "e4fbb06d2dda11414252575dd77b2561b23e09c0237fac764076ba61fffa3461",
"Created": "2018-05-01T01:32:33.151210372Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3034,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-05-01T01:32:35.091034063Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
....
停止WEB應用容器
[root@huixuan ~]# docker stop e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]#
重啟WEB應用容器
已經停止的容器,我們可以使用命令 docker start 來啟動。
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e044f323370 training/webapp "python app.py" 11 minutes ago Up 11 minutes 0.0.0.0:5000->5000/tcp admiring_goldwasser
[root@huixuan ~]# docker start e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e044f323370 training/webapp "python app.py" 11 minutes ago Up 11 minutes 0.0.0.0:5000->5000/tcp admiring_goldwasser
e4fbb06d2dda training/webapp "python app.py" 24 minutes ago Up 2 seconds 0.0.0.0:1025->5000/tcp kickass_wright
[root@huixuan ~]#
docker ps -l 查詢最後一次建立的容器:
[root@huixuan ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e044f323370 training/webapp "python app.py" 11 minutes ago Up 11 minutes 0.0.0.0:5000->5000/tcp admiring_goldwasser
[root@huixuan ~]#
正在執行的容器,我們可以使用 docker restart 命令來重啟
移除WEB應用容器
我們可以使用 docker rm 命令來刪除不需要的容器
刪除容器時,容器必須是停止狀態,否則會報如下錯誤
[root@huixuan ~]# docker rm e4fbb06d2dda
Error response from daemon: You cannot remove a running container e4fbb06d2dda11414252575dd77b2561b23e09c0237fac764076ba61fffa3461. Stop the container before attempting removal or use -f
[root@huixuan ~]#
[root@huixuan ~]# docker stop e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]# docker rm e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e044f323370 training/webapp "python app.py" 13 minutes ago Up 13 minutes 0.0.0.0:5000->5000/tcp admiring_goldwasser
[root@huixuan ~]#
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/312079/viewspace-2153586/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Docker 容器學習Docker
- docker學習5:Docker 容器連線Docker
- Docker學習(十)Docker容器編排 Docker-composDocker
- docker學習3:Docker Hello WorldDocker
- [Docker 系列]docker 學習六,資料卷容器Docker
- [Docker 系列]docker 學習 五,容器資料卷Docker
- docker學習4:Docker 映象使用Docker
- Docker容器學習與分享12Docker
- 3、docker容器操作Docker
- Docker學習之搭建MySql容器服務DockerMySql
- 容器Docker學習系列五~命令學習history,save, importDockerImport
- 容器Docker學習系列五~命令學習history,save,importDockerImport
- Docker-容器使用Docker
- docker學習筆記(3)- 映象Docker筆記
- 學習瞭解使用dockerDocker
- Docker學習筆記:映象、容器、資料卷Docker筆記
- 【Docker 系列】docker 學習 三Docker
- [Docker 系列]docker 學習 三Docker
- [Docker 系列]docker 學習八,Docker 網路Docker
- 使用iptables管控docker容器Docker
- 使用docker執行CentOS容器DockerCentOS
- docker建立容器後如何使用Docker
- (四)Docker安裝使用容器Docker
- Docker容器Docker
- docker學習Docker
- docker 學習Docker
- Docker 學習筆記(第三集:容器)Docker筆記
- docker學習筆記-為容器配置重啟策略Docker筆記
- Docker構建自己的容器(學習筆記二)Docker筆記
- docker學習系列2儲存對容器的修改Docker
- docker學習6:在Centos7 更改Docker預設映象和容器的位置DockerCentOS
- Dockerfile與docker-compose容器編排(Docker系列第3章,共3章)Docker
- 使用docker-compose管理freeswitch容器Docker
- docker 容器的使用與檢視Docker
- 如何在Docker容器中使用ArthasDocker
- 使用Portainer部署Docker容器實踐AIDocker
- 使用容器Docker進行開發Docker
- Docker--容器和映象的使用Docker