容器使用
命令列表
# 可以通过docker命令,查看客户端的所有命令
[root@iZ2ze1le28b0mxc77tp93aZ ~]# docker
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker
context use")
-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:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
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.
容器使用
获取镜像
docker pull [镜像]
启动容器
docker run -it ubuntu /bin/bash
exit或ctrl+d退出
- -i: 交互式操作
- -t: 终端
- ubuntu: ubuntu 镜像
- /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash
启动一个停止的容器
# docker ps -a 列出所有容器
# docker ps -l 查询最后一次创建的容器
[root@iZ2ze1le28b0mxc77tp93aZ ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3a712788796 ubuntu:latest "/bin/sh -c 'while t…" 29 minutes ago Exited (137) 18 minutes ago cool_lalande
bf025b34d82e ubuntu "/bin/bash" 47 minutes ago Exited (0) 42 minutes ago gallant_mendel
5171a778845c ubuntu "/bin/echo test" 2 hours ago Exited (0) 2 hours ago sleepy_ptolemy
# 启动
docker start [容器id]/[容器名称]
后台运行
docker run -itd --name ubuntu-test ubuntu /bin/bash
# 加了 -d 参数默认不会进入容器,想要进入容器需要使用指令 docker exec(下面会介绍到)
停止一个容器
docker stop [容器id]/[容器名称]
docker restart [容器id]/[容器名称]
--重启
进入容器
# 进入容器 推荐使用 docker exec 命令,因为此退出容器终端,不会导致容器的停止
docker exec -it 0546f66f7e25 /bin/bash
root@0546f66f7e25:/#
# docker attach [容器id] --如果从这个容器退出,会导致容器的停止
导出和导入容器
- 导出
docker export [容器id] > ubuntu.tar
- 导入
cat docker/ubuntu.tar | docker import - test/ubuntu:v1
删除容器
删除容器时,容器必须是停止状态,否则会报如下错误
docker rm [容器id]/[容器名称]
docker container prune
-- 清掉所有
Web应用
-P 将容器内部使用的网络端口随机映射到我们使用的主机上
docker run -d -P training/webapp python app.py CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2ef3e5baaa32 training/webapp "python app.py" 6 seconds ago Up 5 seconds 0.0.0.0:32769->5000/tcp upbeat_shirley 0546f66f7e25 ubuntu "/bin/bash" 35 minutes ago Up 35 minutes ubuntu-test
- 我们也可以通过 -p 参数来设置不一样的端口
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES baf548e78baa training/webapp "python app.py" 5 seconds ago Up 5 seconds 0.0.0.0:5000->5000/tcp practical_gates 2ef3e5baaa32 training/webapp "python app.py" 2 minutes ago Up 2 minutes 0.0.0.0:32769->5000/tcp upbeat_shirley
- 此时通过 curl 即出现测试结果
[root@iZ2ze1le28b0mxc77tp93aZ ~]# curl localhost:5000 -w '\n' Hello world! [root@iZ2ze1le28b0mxc77tp93aZ ~]# curl localhost:32769 -w '\n' Hello world!
网络端口快捷方式
通过 docker ps [容器id]命令可以查看到容器的端口映射,docker 还提供了另一个快捷方式 docker port,使用 docker port 可以查看指定 (ID 或者名字)容器的某个确定端口映射到宿主机的端口号
docker port baf548e78baa
5000/tcp -> 0.0.0.0:5000
查看 WEB 应用程序日志
docker logs [ID或者名字] 可以查看容器内部的标准输出
# docker logs -f 能像tail -f 来输出
docker logs baf548e78baa
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
172.18.0.1 - - [14/Jul/2020 10:37:23] "GET / HTTP/1.1" 200 -
172.18.0.1 - - [14/Jul/2020 10:38:04] "GET / HTTP/1.1" 200 -
172.18.0.1 - - [14/Jul/2020 10:38:20] "GET / HTTP/1.1" 200 -
查看WEB应用程序容器的进程
docker top [id或名称]
docker top baf548e78baa
UID PID PPID C STIME TTY TIME CMD
root 18315 18298 0 18:35 ? 00:00:00 python app.py
检查 WEB 应用程序
使用 docker inspect 来查看 Docker 的底层信息。它会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息
docker inspect baf548e78baa
[
{
"Id": "baf548e78baad2a89b5b4d5e9ae89171c5561e779ac2723830f2aab65a007619",
"Created": "2020-07-14T10:35:46.126806217Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 18315,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-07-14T10:35:46.665977728Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557",
"ResolvConfPath": "/var/lib/docker/containers/baf548e78baad2a89b5b4d5e9ae89171c5561e779ac2723830f2aab65a007619/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/baf548e78baad2a89b5b4d5e9ae89171c5561e779ac2723830f2aab65a007619/hostname",
"HostsPath": "/var/lib/docker/containers/baf548e78baad2a89b5b4d5e9ae89171c5561e779ac2723830f2aab65a007619/hosts",
"LogPath": "/var/lib/docker/containers/baf548e78baad2a89b5b4d5e9ae89171c5561e779ac2723830f2aab65a007619/baf548e78baad2a89b5b4d5e9ae89171c5561e779ac2723830f2aab65a007619-json.log",
"Name": "/practical_gates",
"RestartCount": 0,
"Driver": "overlay2",
...........