Docker容器的使用
docker客户端:
直接输入docker命令来查看Docker客户端的所有命令选项
runoob@runoob:~# docker
通过命令docker command --help 查看指定的docker命令使用方法
root@ubuntu:/# docker
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-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:
config Manage Docker configs
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
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams 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 to files or directories 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@ubuntu:/# 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
--no-stream Disable streaming stats and only pull the first result
--no-trunc Do not truncate output
接下来使用docker构建一个web应用程序
我们将在docker容器中运行一个Python Flask应用来运行一个Web应用。
root@ubuntu:/# docker pull training/webapp 载入镜像
Using default tag: latest
latest: Pulling from training/webapp
e190868d63f8: Already exists
909cd34c6fd7: Already exists
0b9bfabab7c1: Already exists
a3ed95caeb02: Already exists
10bbbc0fc0ff: Already exists
fca59b508e9f: Already exists
e7ae2541b15b: Already exists
9dd97ef58ce9: Already exists
a4c1b0cb7af7: Already exists
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Image is up to date for training/webapp:latest
-d:让容器在后台运行 -P: 将容器内部使用的网络端口映射到我们使用的主机上
root@ubuntu:/# docker run -d -P training/webapp python app.py
d3bc4d05735159e427cc2091df3ad977bb3e2108304d557188e5f7dafcc0c611
查看WEB应用容器
root@ubuntu:/# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3bc4d057351 training/webapp "python app.py" 2 minutes ago Up 2 minutes 0.0.0.0:32769->5000/tcp friendly_shaw
875078b4858c training/webapp "python app.py" 12 minutes ago Up 12 minutes 0.0.0.0:32768->5000/tcp distracted_neumann
可以看到Docker开放了5000端口(默认Python flask端口)映射到主机端口32769上。
我们可以通过浏览器访问WEB应用,为了安全起见,我就不显示我的公网IP了
http://服务器公网IP:32768/
我们也可以通过-p参数来设置不一样的端口
docker run -d -p 5000:5000 training/webapp python app.py
网络端口的快捷方式
docker ps命令可以查看到容器的端口映射,docker还提供了另一种快捷方式,使用docker
port ID或名字 可以查看指定容器的某个确定端口映射到宿主机的端口号。
root@ubuntu:/# docker port d3bc4d057351
5000/tcp -> 0.0.0.0:32769
查看WEB应用程序日志
docker logs[ID或者名字]可以查看容器内部的标准输出
查看WEB应用程序容器的进程
docker top 容器名
检查WEB应用程序
docker inspect 容器名,查看Docker的底层信息,他会返回一个JSON文件记录着Docker
容器的配置和状态信息
停止WEB应用容器
docker stop 容器名
正在运行的容器,我们可以使用docker restart命令来重启
移除WEB应用容器
docker rm命令来删除不需要的容器 删除容器,容器必须处于停止状态