Skip to content

官当

gitbub

1. compse创建服务

1.1 create

描述:只是创建一个服务,并不会启动容器

语法:

docker compose --help
docker compose --help
DescriptionCreates containers for a service
Usagedocker compose create [OPTIONS] [SERVICE...]
bash
docker compose create --help
docker compose create --help
OptionDefaultDescription
--buildBuild images before starting containers
--force-recreateRecreate containers even if their configuration and image haven't changed
--no-buildDon't build an image, even if it's policy
--no-recreateIf containers already exist, don't recreate them. Incompatible with --force-recreate.
--pullpolicyPull image before running ("always"|"missing"|"never"|"build")
--quiet-pullPull without printing progress information
--remove-orphansRemove containers for services not defined in the Compose file
--scaleScale SERVICE to NUM instances. Overrides the scale setting in the Compose file if present.

1.2 run

描述:运行一次性命令

DescriptionRun a one-off command on a service
Usagedocker compose run [OPTIONS] SERVICE [COMMAND] [ARGS...]
OptionDefaultDescription
--buildBuild image before starting container
--cap-addAdd Linux capabilities
--cap-dropDrop Linux capabilities
-d, --detachRun container in background and print container ID
--entrypointOverride the entrypoint of the image
-e, --envSet environment variables
-i, --interactivetrueKeep STDIN open even if not attached
-l, --labelAdd or override a label
--nameAssign a name to the container
-T, --no-TTYtrueDisable pseudo-TTY allocation (default: auto-detected)
--no-depsDon't start linked services
-p, --publishPublish a container's port(s) to the host
--quiet-pullPull without printing progress information
--remove-orphansRemove containers for services not defined in the Compose file
--rmAutomatically remove the container when it exits
-P, --service-portsRun command with all service's ports enabled and mapped to the host
--use-aliasesUse the service's network useAliases in the network(s) the container connects to
-u, --userRun as specified username or uid
-v, --volumeBind mount a volume
-w, --workdirWorking directory inside the container

比如:

bash
docker compose run web bash

bash -->执行你所运行的命令
docker compose run web bash

bash -->执行你所运行的命令

1.3 up

描述:这个会创建并启动服务。这个对已经存在的服务会重新停止并创建。

DescriptionCreate and start containers
Usagedocker compose up [OPTIONS] [SERVICE...]
OptionDefaultDescription
--abort-on-container-exitStops all containers if any container was stopped. Incompatible with -d
--abort-on-container-failureStops all containers if any container exited with failure. Incompatible with -d
--always-recreate-depsRecreate dependent containers. Incompatible with --no-recreate.
--attachRestrict attaching to the specified services. Incompatible with --attach-dependencies.
--attach-dependenciesAutomatically attach to log output of dependent services
--buildBuild images before starting containers
-d, --detachDetached mode: Run containers in the background
--exit-code-fromReturn the exit code of the selected service container. Implies --abort-on-container-exit
--force-recreateRecreate containers even if their configuration and image haven't changed
--menuEnable interactive shortcuts when running attached. Incompatible with --detach. Can also be enable/disable by setting COMPOSE_MENU environment var.
--no-attachDo not attach (stream logs) to the specified services
--no-buildDon't build an image, even if it's policy
--no-colorProduce monochrome output
--no-depsDon't start linked services
--no-log-prefixDon't print prefix in logs
--no-recreateIf containers already exist, don't recreate them. Incompatible with --force-recreate.
--no-startDon't start the services after creating them
--pullpolicyPull image before running ("always"|"missing"|"never")
--quiet-pullPull without printing progress information
--remove-orphansRemove containers for services not defined in the Compose file
-V, --renew-anon-volumesRecreate anonymous volumes instead of retrieving data from the previous containers
--scaleScale SERVICE to NUM instances. Overrides the scale setting in the Compose file if present.
-t, --timeoutUse this timeout in seconds for container shutdown when attached or when containers are already running
--timestampsShow timestamps
--waitWait for services to be running|healthy. Implies detached mode.
--wait-timeoutMaximum duration to wait for the project to be running|healthy
-w, --watchWatch source code and rebuild/refresh containers when files are updated.

2. compose启动服务

2.1 start

描述:这个是启动已经存在的服务

DescriptionStart services
Usagedocker compose start [SERVICE...]

3. compose关闭服务

3.1 stop

描述:停止运行容器并不是删除。

DescriptionStop services
Usagedocker compose stop [OPTIONS] [SERVICE...]
OptionDefaultDescription
-t, --timeoutSpecify a shutdown timeout in seconds

3.2 kill

描述:强制停止服务

DescriptionForce stop service containers
Usagedocker compose kill [OPTIONS] [SERVICE...]
OptionDefaultDescription
--remove-orphansRemove containers for services not defined in the Compose file
-s, --signalSIGKILLSIGNAL to send to the container
bash
docker compose kill -s SIGINT
docker compose kill -s SIGINT

4. compose删除服务

4.1 down

DescriptionStop and remove containers, networks
Usagedocker compose down [OPTIONS] [SERVICES]
OptionDefaultDescription
--remove-orphansRemove containers for services not defined in the Compose file
--rmiRemove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")
-t, --timeoutSpecify a shutdown timeout in seconds
-v, --volumesRemove named volumes declared in the "volumes" section of the Compose file and anonymous volumes attached to containers

4.2 rm

描述:删除已经停止的服务

DescriptionRemoves stopped service containers
Usagedocker compose rm [OPTIONS] [SERVICE...]
OptionDefaultDescription
-f, --forceDon't ask to confirm removal
-s, --stopStop the containers, if required, before removing
-v, --volumesRemove any anonymous volumes attached to containers

5. compose查看

5.1 ps

描述:列出容器

DescriptionList containers
Usagedocker compose ps [OPTIONS] [SERVICE...]
OptionDefaultDescription
-a, --allShow all stopped containers (including those created by the run command)
--filterFilter services by a property (supported filters: status)
--formattableFormat 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-truncDon't truncate output
--orphanstrueInclude orphaned services (not declared by project)
-q, --quietOnly display IDs
--servicesDisplay services
--statusFilter 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

描述:列出正在运行的项目

DescriptionList running compose projects
Usagedocker compose ls [OPTIONS]
OptionDefaultDescription
-a, --allShow all stopped Compose projects
--filterFilter output based on conditions provided
--formattableFormat the output. Values: [table | json]
-q, --quietOnly display IDs

5.3 logs

描述:查看服务日志

DescriptionView output from containers
Usagedocker compose logs [OPTIONS] [SERVICE...]
OptionDefaultDescription
-f, --followFollow log output
--indexindex of the container if service has multiple replicas
--no-colorProduce monochrome output
--no-log-prefixDon't print prefix in logs
--sinceShow logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
-n, --tailallNumber of lines to show from the end of the logs for each container
-t, --timestampsShow timestamps
--untilShow logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)

5.4 images

描述:查看镜像

DescriptionList images used by the created containers
Usagedocker compose images [OPTIONS] [SERVICE...]
OptionDefaultDescription
--formattableFormat the output. Values: [table | json]
-q, --quietOnly display IDs

5.5 version

描述:查看版本

DescriptionShow the Docker Compose version information
Usagedocker compose version [OPTIONS]
OptionDefaultDescription
-f, --formatFormat the output. Values: [pretty | json]. (Default: pretty)
--shortShows only Compose's version number
bash
docker compose version
docker compose version