今天就跟大家聊聊有关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容器使用过程是怎么样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/199828.html