您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)Docker容器使用過(guò)程是怎么樣的,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
Docker 容器使用
Docker 客戶端
docker 客戶端非常簡(jiǎn)單 ,我們可以直接輸入 docker 命令來(lái)查看到 Docker 客戶端的所有命令選項(xiàng)。
[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 ~]#
可以通過(guò)命令 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 ~]#
運(yùn)行一個(gè)web應(yīng)用
前面我們運(yùn)行的容器并沒(méi)有一些什么特別的用處。
接下來(lái)讓我們嘗試使用 docker 構(gòu)建一個(gè) web 應(yīng)用程序。
我們將在docker容器中運(yùn)行一個(gè) Python Flask 應(yīng)用來(lái)運(yùn)行一個(gè)web應(yīng)用。
[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 ~]#
參數(shù)說(shuō)明:
-d:讓容器在后臺(tái)運(yùn)行。
-P:將容器內(nèi)部使用的網(wǎng)絡(luò)端口映射到我們使用的主機(jī)上。
查看 WEB 應(yīng)用容器
使用 docker ps 來(lái)查看我們正在運(yùn)行的容器
[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 開(kāi)放了 5000 端口(默認(rèn) Python Flask 端口)映射到主機(jī)端口 32769 上。
這時(shí)我們可以通過(guò)瀏覽器訪問(wèn)WEB應(yīng)用
我們也可以指定 -p 標(biāo)識(shí)來(lái)綁定指定端口。
[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 ~]#
容器內(nèi)部的 5000 端口映射到我們本地主機(jī)的 5000 端口上。
網(wǎng)絡(luò)端口的快捷方式
通過(guò)docker ps 命令可以查看到容器的端口映射,docker還提供了另一個(gè)快捷方式:docker port,使用 docker port 可以查看指定 (ID或者名字)容器的某個(gè)確定端口映射到宿主機(jī)的端口號(hào)。
上面我們創(chuàng)建的web應(yīng)用容器ID為:e4fbb06d2dda kickass_wright
我可以使用docker port e4fbb06d2dda 或docker port kickass_wright來(lái)查看容器端口的映射情況
[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應(yīng)用程序日志
docker logs [ID或者名字] 可以查看容器內(nèi)部的標(biāo)準(zhǔn)輸出。
[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 一樣來(lái)輸出容器內(nèi)部的標(biāo)準(zhǔn)輸出。
從上面,我們可以看到應(yīng)用程序使用的是 5000 端口并且能夠查看到應(yīng)用程序的訪問(wèn)日志。
查看WEB應(yīng)用程序容器的進(jìn)程
我們還可以使用 docker top 來(lái)查看容器內(nèi)部運(yùn)行的進(jìn)程
[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應(yīng)用程序
使用 docker inspect 來(lái)查看Docker的底層信息。它會(huì)返回一個(gè) JSON 文件記錄著 Docker 容器的配置和狀態(tài)信息。
[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應(yīng)用容器
[root@huixuan ~]# docker stop e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]#
重啟WEB應(yīng)用容器
已經(jīng)停止的容器,我們可以使用命令 docker start 來(lái)啟動(dòng)。
[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 查詢最后一次創(chuàng)建的容器:
[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 ~]#
正在運(yùn)行的容器,我們可以使用 docker restart 命令來(lái)重啟
移除WEB應(yīng)用容器
我們可以使用 docker rm 命令來(lái)刪除不需要的容器
刪除容器時(shí),容器必須是停止?fàn)顟B(tài),否則會(huì)報(bào)如下錯(cuò)誤
[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 ~]#
看完上述內(nèi)容,你們對(duì)Docker容器使用過(guò)程是怎么樣的有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。