您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關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 http://0.0.0.0:5000/ (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容器使用過程是怎么樣的有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。