Docker-compose命令-up

命令

docker-compose up

描述

Create and start containers
创建并启动容器

用法

1
up [options] [--scale SERVICE=NUM...] [SERVICE...]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Options:
-d, --detach Detached mode: 后台运行容器并显示容器名称,与--abort-on-container-exit选项互斥
--no-color Produce monochrome output.
--quiet-pull Pull without printing progress information
--no-deps Don't start linked services.
--force-recreate 重新创建容器,即使配置文件和镜像没有改变
--always-recreate-deps 重新创建依赖的容器,与--no-recreate选项互斥
--no-recreate 如果容器已存在则不重新创建,与--force-recreate和--renew-anon-volumes选项互斥
--no-build Don't build an image, even if it's missing.
--no-start Don't start the services after creating them.
--build Build images before starting containers.
--abort-on-container-exit 如果有容器异常退出则停止所有容器,与--detach选项互斥
--attach-dependencies Attach to dependent containers.
-t, --timeout TIMEOUT Use this timeout in seconds for container
shutdown when attached or when containers are
already running. (default: 10)
-V, --renew-anon-volumes Recreate anonymous volumes instead of retrieving
data from the previous containers.
--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.

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ cat docker-compose.yml
version: "3.8"
services:
demo:
container_name: nginx1
image: nginx:1.20.1-alpine
restart: always
ports:
- "80:80"
volumes:
- ./data:/usr/share/nginx/html
- demo-logs:/logs
volumes:
demo-logs:
$ cat data/index.html
demo
# -d 后台运行服务
$ docker-compose up -d
Creating nginx1 ... done
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------
nginx1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:80->80/tcp
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dc1b7521e2e nginx:1.20.1-alpine "/docker-entrypoint.…" 50 seconds ago Up 50 seconds 0.0.0.0:80->80/tcp nginx1
# 验证
$ curl localhost
demo

# 如果不指定container_name,则默认显示名称格式为 目录名_服务名_编号,如下demo_demo_1,目录名也是项目名
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f625b92a447c nginx:1.20.1-alpine "/docker-entrypoint.…" 5 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp demo_demo_1