格式:
ENV <key> <value> #<key>之后的所有内容均会被视为其<value>的组成部分,因此,一次只能设置一个变量
ENV <key>=<value> ... #可以设置多个变量,每个变量为一个"<key>=<value>"的键值对,如果<key>中包含空格,可以使用\来进行转义,也可以通过""来进行标示;另外,反斜线也可以用于续行
示例:
ENV myName John Doe
ENV myDog Rex The Dog
ENV myCat=fluffy
格式:
WORKDIR /path/to/workdir
示例:
WORKDIR /a (这时工作目录为/a)
WORKDIR b (这时工作目录为/a/b)
WORKDIR c (这时工作目录为/a/b/c)
注:
通过WORKDIR设置工作目录后,Dockerfile中其后的命令RUN、CMD、ENTRYPOINT、ADD、COPY等命令都会在该目录下执行。在使用docker run运行容器时,可以通过-w参数覆盖构建时所设置的工作目录。
13、USER
指定运行容器时的用户名或 UID,后续的 RUN 也会使用指定用户。使用USER指定用户时,可以使用用户名、UID或GID,或是两者的组合。当服务不需要管理员权限时,可以通过该命令指定运行用户。并且可以在之前创建所需要的用户
格式:
USER user
USER user:group
USER uid
USER uid:gid
USER user:gid
USER uid:group
示例:
USER www
注:
使用USER指定用户后,Dockerfile中其后的命令RUN、CMD、ENTRYPOINT都将使用该用户。镜像构建完成后,通过docker run运行容器时,可以通过-u参数来覆盖所指定的用户。
14、ARG
用于指定传递给构建运行时的变量
格式:
ARG <name>[=<default value>]
示例:
ARG site
ARG build_user=www
Usage: pull [options] [SERVICE...]
Options:
--ignore-pull-failures Pull what it can and ignores images with pull failures.
--parallel Pull multiple images in parallel.
--quiet Pull without printing progress information
#用于拉取服务依赖的镜像
[root@localhost ~]
# docker-compose pull db
run一次性命令
Usage: run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]
Options:
-d, --detach Run container in background and print container ID 后台运行容器
-e, --env list Set environment variables 设置环境变量
--expose list Expose a port or a range of ports 暴露端口
-i, --interactive Keep STDIN open even if not attached 交互模式
--name string Assign a name to the container 给当前运行的容器指定名字
-p, --publish list Publish a container's port(s) to the host 指定端口映射
-P, --publish-all Publish all exposed ports to random ports 随机指定暴露的端口映射
-t, --tty Allocate a pseudo-TTY 分配一个终端
-v, --volume list Bind mount a volume 挂载数据卷
--volumes-from list Mount volumes from the specified container(s) 从一个数据卷容器挂载目录
-w, --workdir string Working directory inside the container 指定容器中的工作目录
使用的命令run从具有由服务定义的配置的新容器中启动,包括卷,链接和其他详细信息。
run启动的区别:
web服务配置以bash开头,则将其docker-compose run web python app.py覆盖python app.py
docker-compose run --no-deps web python manage.py shell
start启动服务
Usage: start [SERVICE...]
启动指定服务的容器
pause暂停服务
Usage: pause [SERVICE...]
暂停服务,不可以与unpause一起使用。
stop停止服务
Usage: stop [options] [SERVICE...]
Options:
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds (default: 10).
停止容器
restart重启服务
Usage: stop [options] [SERVICE...]
Options:
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds (default: 10).
重启服务
up启动所有容器服务
Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]
Options: -d Detached mode: Run containers in the background,
print new container names.
Incompatible with --abort-on-container-exit.
--no-color Produce monochrome output.
--no-deps Don't start linked services.
--force-recreate Recreate containers even if their configuration
and image haven't changed.
Incompatible with --no-recreate.
--no-recreate If containers already exist, don't recreate them.
Incompatible with --force-recreate.
--no-build Don't build an image, even if it's missing.
--build Build images before starting containers.
--abort-on-container-exit Stops all containers if any container was stopped.
Incompatible with -d.
-t, --timeout TIMEOUT Use this timeout in seconds for container shutdown
when attached or when containers are already
running. (default: 10)
--remove-orphans Remove containers for services not defined in
the Compose file
--exit-code-from SERVICE Return the exit code of the selected service container.
Implies --abort-on-container-exit.
--scale SERVICE=NUM Scale SERVICE to NUM instances. Overrides the `scale`
setting in the Compose file if present.
构建,(重新)创建,启动并附加到服务的容器。
docker-compose up
退出命令时,所有容器也停止。
docker-compose up -d
该命令将以后台启动所有容器服务。
容器服务后服务的配置或映像已更改的处理
docker-compose up -d --no-recreat
防止撰写选取更改
强制停止重启容器服务
docker-compose up -d --force-recreate
如果进程遇到错误,则此命令的退出代码为1。
如果使用SIGINT(ctrl+ C)中断进程或者SIGTERM容器停止,并且退出代码为0。
如果SIGINT或SIGTERM在此关闭阶段再次发送,正在运行的容器将被终止,并且退出代码为2。
logs服务日志查看
Usage: logs [options] [SERVICE...]
Options:
--no-color Produce monochrome output.
-f, --follow Follow log output
-t, --timestamps Show timestamps
--tail="all" Number of lines to show from the end of the logs
for each container.
服务日志查看
down停止并删除容器
Usage: down [options]
Options:
--rmi type Remove images. Type must be one of:
'all': Remove all images used by any service.
'local': Remove only images that don't have a custom tag
set by the `image` field.
-v, --volumes Remove named volumes declared in the `volumes` section
of the Compose file and anonymous volumes
attached to containers.
--remove-orphans Remove containers for services not defined in the
Compose file
停止容器并移除通过创建的容器,网络,卷和图像up。
默认情况下,唯一删除的内容是:
在撰写文件中定义的服务容器
网络在networks撰写文件的部分中定义
默认网络(如果使用的话)
定义为external的网络和卷从不删除。
rm删除停止的容器
Usage: rm [options] [SERVICE...]
Options:
-f, --force Don't ask to confirm removal
-s, --stop Stop the containers, if required, before removing
-v Remove any anonymous volumes attached to containers
docker-compose port [options] SERVICE PRIVATE_PORT
Options:
--protocol=proto tcp or udp [default: tcp]
--index=index index of the container if there are multiple
instances of a service [default: 1]
[root@iZbp1e9mxelwe7pwimpw8sZ ~]# docker update --help
Usage: docker update [OPTIONS] CONTAINER [CONTAINER...]
Update configuration of one or more containers
Options:
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit the CPU real-time period in microseconds
--cpu-rt-runtime int Limit the CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--kernel-memory bytes Kernel memory limit
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--pids-limit int Tune container pids limit (set -1 for unlimited)
--restart string Restart policy to apply when a container exits
[root@iZbp1e9mxelwe7pwimpw8sZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iZbp1e9mxelwe7pwimpw8sZ ~]
#
重启开机后的结果:容器已经自启动
[root@iZbp1e9mxelwe7pwimpw8sZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b397fad059a2 tomcat:8 "/usr/local/tomcat8/…" 5 weeks ago Up 8 seconds 0.0.0.0:80->8080/tcp tomcat8