一、命令行
可以通过运行 docker ,或者 docker help 命令得到命令行的帮助信息(我们以 CentOS 为操作环境为例):
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker help
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.
其中:
info、version是系统环境信息相关的命令
events、history、logs 是日志信息相关的命令
login\pull\push\search是DockerHub服务相关的命令
其余都是日常维护相关的命令
二、命令语法
基本用法:命令 + 参数
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker info Client: Debug Mode: false Server: Containers: 4 Running: 0 Paused: 0 Stopped: 4 Images: 2 Server Version: 19.03.1 ...............(省略) Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Security Options: Kernel Version: 3.10.0-693.2.2.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker version Client: Docker Engine - Community Version: 19.03.1 API version: 1.40 Go version: go1.12.5 Git commit: 74b1e89e8a Built: Thu Jul 25 21:17:37 2019 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.1 API version: 1.40 (minimum version 1.12) Go version: go1.12.5 Git commit: 74b1e89e8a Built: Thu Jul 25 21:27:55 2019 OS/Arch: linux/amd64 Experimental: false containerd: Version: v1.2.6 GitCommit: 894b81a4b802e4eb2a91d1ce216b8817763c29fb runc: Version: 1.0.0-rc8 GitCommit: 425e105d5a03fabd737a126ad93d62a9eeede87f docker-init: Version: 0.18.0 GitCommit: fec3683
查看已下载的镜像
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 3556258649b2 2 weeks ago 64.2MB hello-world latest fce289e99eb9 7 months ago 1.84kB learn/tutorial latest a7876479f1aa 6 years ago 128MB
单个字符的参数可以放在一起:
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker run -t -i ubuntu bash # 使用镜像 ubuntu:latest 以交互模式启动一个容器,在容器内执行 bash 命令,标红部分表示使用分开的参数 Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu 7413c47ba209: Pull complete 0fe7e7cbb2e8: Pull complete 1d425c982345: Pull complete 344da5c95cec: Pull complete Digest: sha256:c303f19cfe9ee92badbbbd7567bc1ca47789f79303ddcef56f77687d4744cd7a Status: Downloaded newer image for ubuntu:latest root@a0629a1e86e4:/# cat /etc/issue Ubuntu 18.04.2 LTS \n \l root@a0629a1e86e4:/# exit exit [root@iz2ze2bn5x2wqxdeq65wlpz ~]#
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker run -ti ubuntu bash # 标红部分,表示把单个字符参数,合在一起使用 root@5b84ee078161:/# exit exit
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker rm imgid-1 imgid-2 # 标红部分,表示把多个参数放在一行使用 Error: No such container: imgid-1 Error: No such container: imgid-2
二、Docker 后台进程参数
| 参数 | 介绍 |
|---|---|
| --api-enable-cors=false | 远程API调用。 |
| -b, --bridge="" | 桥接一个系统上的网桥设备到 Docker 容器里,当使用 none 可以停用容器里的网络 |
| --bip="" | 使用 CIDR 地址来设定网络桥的 IP。此参数和 -b 不能一起使用。 |
| -D, --debug=false | 开启Debug模式。例如:docker -d -D |
| -d, --daemon=false | 开启Daemon模式。 |
| --dns=[] | 设置容器使用DNS服务器。例如: docker -d --dns 8.8.8.8 |
| -dns-search=[] | 设置容器使用指定的DNS搜索域名。如: docker -d --dns-search example.com |
| --exec-driver="native" | 设置容器使用指定的运行时驱动。如:docker -d -e lxc |
| -G, --group="docker" | 在后台运行模式下,赋予指定的Group到相应的unix socket上。注意,当此参数 --group 赋予空字符串时,将去除组信息 |
| -g, --graph="/var/lib/docker" | 设置Docker运行时根目录 |
| -H, --host=[] | 设置后台模式下指定socket绑定,可以绑定一个或多个 tcp://host:port, unix:///path/to/socket, fd://* 或 fd://socketfd。如:$ docker -H tcp://0.0.0.0:2375 ps 或者$ export DOCKER_HOST="tcp://0.0.0.0:2375"$ docker ps |
| -icc=true | 设置启用内联容器的通信。 |
| --ip="0.0.0.0" | 设置容器绑定IP时使用的默认IP地址 |
| --ip-forward=true | 设置启动容器的 net.ipv4.ip_forward |
| --iptables=true | 设置启动Docker容器自定义的iptable规则 |
| --mtu=0 | 设置容器网络的MTU值,如果没有这个参数,选用默认 route MTU,如果没有默认route,就设置成常量值 1500。 |
| -p, --pidfile="/var/run/docker.pid" | 设置后台进程PID文件路径。 |
| -r, --restart=true | 设置重启之前运行中的容器 |
| -s, --storage-driver="" | 设置容器运行时使用指定的存储驱动,如,指定使用devicemapper,可以这样:docker -d -s devicemapper |
| --selinux-enabled=false | 设置启用selinux支持 |
| --storage-opt=[] | 设置存储驱动的参数 |
三、Docker 命令大全
容器生命周期管理
run
start/stop/restart
kill
rm
pause/unpause
create
exec
容器操作
ps
inspect
top
attach
events
logs
wait
export
port
容器rootfs命令
commit
cp
diff
镜像仓库
login
pull
push
search
本地镜像管理
images
rmi
tag
build
history
save
load
import
Docker信息|版本
info
version
1、docker run :创建一个新的容器并运行一个命令
语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS说明: -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项; -d: 后台运行容器,并返回容器ID; -i: 以交互模式运行容器,通常与 -t 同时使用; -P: 随机端口映射,容器内部端口随机映射到主机的高端口 -p: 指定端口映射,格式为:主机(宿主)端口:容器端口 -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用; --name="nginx-lb": 为容器指定一个名称; --dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致; --dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致; -h "mars": 指定容器的hostname; -e username="ritchie": 设置环境变量; --env-file=[]: 从指定文件读入环境变量; --cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行; -m :设置容器使用内存最大值; --net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型; --link=[]: 添加链接到另一个容器; --expose=[]: 开放一个端口或一组端口; --volume , -v: 绑定一个卷
实例:
使用docker镜像nginx:latest以后台模式启动一个容器,并将容器命名为mynginx。
docker run --name mynginx -d nginx:latest
使用镜像nginx:latest以后台模式启动一个容器,并将容器的80端口映射到主机随机端口。
docker run -P -d nginx:latest
使用镜像 nginx:latest,以后台模式启动一个容器,将容器的 80 端口映射到主机的 80 端口,主机的目录 /data 映射到容器的 /data。
docker run -p 80:80 -v /data:/data -d nginx:latest
绑定容器的 8080 端口,并将其映射到本地主机 127.0.0.1 的 80 端口上。
docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
使用镜像nginx:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。
docker run -it nginx:latest /bin/bash