1. compse创建服务
1.1 create
描述:只是创建一个服务,并不会启动容器
语法:
docker compose --help
docker compose --help
Description | Creates containers for a service |
---|---|
Usage | docker compose create [OPTIONS] [SERVICE...] |
bash
docker compose create --help
docker compose create --help
Option | Default | Description |
---|---|---|
--build | Build images before starting containers | |
--force-recreate | Recreate containers even if their configuration and image haven't changed | |
--no-build | Don't build an image, even if it's policy | |
--no-recreate | If containers already exist, don't recreate them. Incompatible with --force-recreate. | |
--pull | policy | Pull image before running ("always"|"missing"|"never"|"build") |
--quiet-pull | Pull without printing progress information | |
--remove-orphans | Remove containers for services not defined in the Compose file | |
--scale | Scale SERVICE to NUM instances. Overrides the scale setting in the Compose file if present. |
1.2 run
描述:运行一次性命令
Description | Run a one-off command on a service |
---|---|
Usage | docker compose run [OPTIONS] SERVICE [COMMAND] [ARGS...] |
Option | Default | Description |
---|---|---|
--build | Build image before starting container | |
--cap-add | Add Linux capabilities | |
--cap-drop | Drop Linux capabilities | |
-d, --detach | Run container in background and print container ID | |
--entrypoint | Override the entrypoint of the image | |
-e, --env | Set environment variables | |
-i, --interactive | true | Keep STDIN open even if not attached |
-l, --label | Add or override a label | |
--name | Assign a name to the container | |
-T, --no-TTY | true | Disable pseudo-TTY allocation (default: auto-detected) |
--no-deps | Don't start linked services | |
-p, --publish | Publish a container's port(s) to the host | |
--quiet-pull | Pull without printing progress information | |
--remove-orphans | Remove containers for services not defined in the Compose file | |
--rm | Automatically remove the container when it exits | |
-P, --service-ports | Run command with all service's ports enabled and mapped to the host | |
--use-aliases | Use the service's network useAliases in the network(s) the container connects to | |
-u, --user | Run as specified username or uid | |
-v, --volume | Bind mount a volume | |
-w, --workdir | Working directory inside the container |
比如:
bash
docker compose run web bash
bash -->执行你所运行的命令
docker compose run web bash
bash -->执行你所运行的命令
1.3 up
描述:这个会创建并启动服务。这个对已经存在的服务会重新停止并创建。
Description | Create and start containers |
---|---|
Usage | docker compose up [OPTIONS] [SERVICE...] |
Option | Default | Description |
---|---|---|
--abort-on-container-exit | Stops all containers if any container was stopped. Incompatible with -d | |
--abort-on-container-failure | Stops all containers if any container exited with failure. Incompatible with -d | |
--always-recreate-deps | Recreate dependent containers. Incompatible with --no-recreate. | |
--attach | Restrict attaching to the specified services. Incompatible with --attach-dependencies. | |
--attach-dependencies | Automatically attach to log output of dependent services | |
--build | Build images before starting containers | |
-d, --detach | Detached mode: Run containers in the background | |
--exit-code-from | Return the exit code of the selected service container. Implies --abort-on-container-exit | |
--force-recreate | Recreate containers even if their configuration and image haven't changed | |
--menu | Enable interactive shortcuts when running attached. Incompatible with --detach. Can also be enable/disable by setting COMPOSE_MENU environment var. | |
--no-attach | Do not attach (stream logs) to the specified services | |
--no-build | Don't build an image, even if it's policy | |
--no-color | Produce monochrome output | |
--no-deps | Don't start linked services | |
--no-log-prefix | Don't print prefix in logs | |
--no-recreate | If containers already exist, don't recreate them. Incompatible with --force-recreate. | |
--no-start | Don't start the services after creating them | |
--pull | policy | Pull image before running ("always"|"missing"|"never") |
--quiet-pull | Pull without printing progress information | |
--remove-orphans | Remove containers for services not defined in the Compose file | |
-V, --renew-anon-volumes | Recreate anonymous volumes instead of retrieving data from the previous containers | |
--scale | Scale SERVICE to NUM instances. Overrides the scale setting in the Compose file if present. | |
-t, --timeout | Use this timeout in seconds for container shutdown when attached or when containers are already running | |
--timestamps | Show timestamps | |
--wait | Wait for services to be running|healthy. Implies detached mode. | |
--wait-timeout | Maximum duration to wait for the project to be running|healthy | |
-w, --watch | Watch source code and rebuild/refresh containers when files are updated. |
2. compose启动服务
2.1 start
描述:这个是启动已经存在的服务
Description | Start services |
---|---|
Usage | docker compose start [SERVICE...] |
3. compose关闭服务
3.1 stop
描述:停止运行容器并不是删除。
Description | Stop services |
---|---|
Usage | docker compose stop [OPTIONS] [SERVICE...] |
Option | Default | Description |
---|---|---|
-t, --timeout | Specify a shutdown timeout in seconds |
3.2 kill
描述:强制停止服务
Description | Force stop service containers |
---|---|
Usage | docker compose kill [OPTIONS] [SERVICE...] 、 |
Option | Default | Description |
---|---|---|
--remove-orphans | Remove containers for services not defined in the Compose file | |
-s, --signal | SIGKILL | SIGNAL to send to the container |
bash
docker compose kill -s SIGINT
docker compose kill -s SIGINT
4. compose删除服务
4.1 down
Description | Stop and remove containers, networks |
---|---|
Usage | docker compose down [OPTIONS] [SERVICES] |
Option | Default | Description |
---|---|---|
--remove-orphans | Remove containers for services not defined in the Compose file | |
--rmi | Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all") | |
-t, --timeout | Specify a shutdown timeout in seconds | |
-v, --volumes | Remove named volumes declared in the "volumes" section of the Compose file and anonymous volumes attached to containers |
4.2 rm
描述:删除已经停止的服务
Description | Removes stopped service containers |
---|---|
Usage | docker compose rm [OPTIONS] [SERVICE...] |
Option | Default | Description |
---|---|---|
-f, --force | Don't ask to confirm removal | |
-s, --stop | Stop the containers, if required, before removing | |
-v, --volumes | Remove any anonymous volumes attached to containers |
5. compose查看
5.1 ps
描述:列出容器
Description | List containers |
---|---|
Usage | docker compose ps [OPTIONS] [SERVICE...] |
Option | Default | Description |
---|---|---|
-a, --all | Show all stopped containers (including those created by the run command) | |
--filter | Filter services by a property (supported filters: status) | |
--format | table | Format output using a custom template: 'table': Print output in table format with column headers (default) 'table TEMPLATE': Print output in table format using the given Go template 'json': Print in JSON format 'TEMPLATE': Print output using the given Go template. Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates |
--no-trunc | Don't truncate output | |
--orphans | true | Include orphaned services (not declared by project) |
-q, --quiet | Only display IDs | |
--services | Display services | |
--status | Filter services by status. Values: [paused | restarting | removing | running | dead | created | exited] |
bash
docker compose ps --format json | jq .
[
{
"ID": "1553b0236cf4d2715845f053a4ee97042c4f9a2ef655731ee34f1f7940eaa41a",
"Name": "example-bar-1",
"Command": "/docker-entrypoint.sh nginx -g 'daemon off;'",
"Project": "example",
"Service": "bar",
"State": "exited",
"Health": "",
"ExitCode": 0,
"Publishers": null
},
{
"ID": "f02a4efaabb67416e1ff127d51c4b5578634a0ad5743bd65225ff7d1909a3fa0",
"Name": "example-foo-1",
"Command": "/docker-entrypoint.sh nginx -g 'daemon off;'",
"Project": "example",
"Service": "foo",
"State": "running",
"Health": "",
"ExitCode": 0,
"Publishers": [
{
"URL": "0.0.0.0",
"TargetPort": 80,
"PublishedPort": 8080,
"Protocol": "tcp"
}
]
}
]
docker compose ps --format json | jq .
[
{
"ID": "1553b0236cf4d2715845f053a4ee97042c4f9a2ef655731ee34f1f7940eaa41a",
"Name": "example-bar-1",
"Command": "/docker-entrypoint.sh nginx -g 'daemon off;'",
"Project": "example",
"Service": "bar",
"State": "exited",
"Health": "",
"ExitCode": 0,
"Publishers": null
},
{
"ID": "f02a4efaabb67416e1ff127d51c4b5578634a0ad5743bd65225ff7d1909a3fa0",
"Name": "example-foo-1",
"Command": "/docker-entrypoint.sh nginx -g 'daemon off;'",
"Project": "example",
"Service": "foo",
"State": "running",
"Health": "",
"ExitCode": 0,
"Publishers": [
{
"URL": "0.0.0.0",
"TargetPort": 80,
"PublishedPort": 8080,
"Protocol": "tcp"
}
]
}
]
5.2 ls
描述:列出正在运行的项目
Description | List running compose projects |
---|---|
Usage | docker compose ls [OPTIONS] |
Option | Default | Description |
---|---|---|
-a, --all | Show all stopped Compose projects | |
--filter | Filter output based on conditions provided | |
--format | table | Format the output. Values: [table | json] |
-q, --quiet | Only display IDs |
5.3 logs
描述:查看服务日志
Description | View output from containers |
---|---|
Usage | docker compose logs [OPTIONS] [SERVICE...] |
Option | Default | Description |
---|---|---|
-f, --follow | Follow log output | |
--index | index of the container if service has multiple replicas | |
--no-color | Produce monochrome output | |
--no-log-prefix | Don't print prefix in logs | |
--since | Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) | |
-n, --tail | all | Number of lines to show from the end of the logs for each container |
-t, --timestamps | Show timestamps | |
--until | Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) |
5.4 images
描述:查看镜像
Description | List images used by the created containers |
---|---|
Usage | docker compose images [OPTIONS] [SERVICE...] |
Option | Default | Description |
---|---|---|
--format | table | Format the output. Values: [table | json] |
-q, --quiet | Only display IDs |
5.5 version
描述:查看版本
Description | Show the Docker Compose version information |
---|---|
Usage | docker compose version [OPTIONS] |
Option | Default | Description |
---|---|---|
-f, --format | Format the output. Values: [pretty | json]. (Default: pretty) | |
--short | Shows only Compose's version number |
bash
docker compose version
docker compose version