Docker-compose命令詳解


語法:
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f=<arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name (default: directory name)
  --verbose                   Show more output
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the name specified
                              in the client certificate (for example if your docker host
                              is an IP address)

Commands:
  build              Build or rebuild services
  config             Validate and view the compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  help               Get help on a command
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pulls service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

-f   指定docker-compose.xml文件,默認是 docker-compose.xml  ,  當一條命令有多個-f參數時,會做替換操作
-p  指定docker-compose的項目目錄,也就是docker-compose.xml文件的存儲目錄




CLI Environment Variables


Usage: build [options] [SERVICE...]

Options:
--force-rm  Always remove intermediate containers.
--no-cache  Do not use cache when building the image.
--pull      Always attempt to pull a newer version of the image.
當修改dockerfile或者docker-compose時,運行docker-compose build 重建鏡像。  生成鏡像后,可使用docker-compose up啟動

config

Usage: config [options]

--quiet     只驗證配置,不輸出。 當配置正確時,不輸出任何內容,當文件配置錯誤,輸出錯誤信息。
--services      打印服務名,一行一個
驗證和查看compose文件配置。

create

為服務創建容器.只是單純的create,還需要使用start啟動compose

Usage: create [options] [SERVICE...]

Options:
    --force-recreate       重新創建容器,即使他的配置和鏡像沒有改變,不兼容--no-recreate參數
. 不兼容--force-recreate參數
    --no-build             不創建鏡像,即使缺失.
    --build                創建容器前,生成鏡像.
down
Usage: down [options]

Options:
    --rmi type          刪除鏡像,類型必須是:
                        'all': 刪除compose文件中定義的所以鏡像.
                        'local': 刪除鏡像名為空的鏡像
--volumes       刪除卷
                        attached to containers.
    --remove-orphans    Remove containers for services not defined in the
file
停止和刪除容器、網絡、卷、鏡像,這些內容是通過docker-compose up命令創建的.  默認值刪除 容器 網絡,可以通過指定 rmi volumes參數刪除鏡像和卷

events

Usage: events [options] [SERVICE...]

Options:
    --json      輸出事件日志,json格式
輸出docker-compose 事件的日志,當執行docker-compose命令操作時,docker-compose even命令就會監控日志:
{
    "service": "web",
    "event": "create",
    "container": "213cf75fc39a",
    "image": "alpine:edge",
    "time": "2015-11-20T18:01:03.615550",
}

exec

Usage: exec [options] SERVICE COMMAND [ARGS...]

Options:
-d                分離模式,后台運行命令.
--privileged      獲取特權.
--user USER       指定運行的用戶.
-T                禁用分配TTY. By default `docker-compose exec`
                  分配 a TTY.
--index=index     當一個服務擁有多個容器時,可通過該參數登陸到該服務下的任何服務,例如:docker-compose exec --index=1 web /bin/bash ,web服務中包含多個容器
of a service [default: 1]
和docker exec命令功能相同,可以通過service name登陸到容器中
e.g.  docker-compose exec web sh 

kill

Usage: kill [options] [SERVICE...]

Options:
-s SIGNAL         向容器發送信號. 默認是SIGKILL.
通過發送 SIGKILL 信號來強制停止服務容器。支持通過參數來指定發送的信號:
$ docker-compose kill -s SIGINT

logs

Usage: logs [options] [SERVICE...]

Options:
--no-color          單色輸出,不顯示其他顏.
--follow        跟蹤日志輸出,就是可以實時查看日志
--timestamps    顯示時間戳
--tail              從日志的結尾顯示,--tail=200
顯示日志輸出.

pause

Usage: pause [SERVICE...]
暫停容器服務. docker-compose pause  暫停所有服務. docker-compose pause web,之后暫停web服務的容器。

unpause

Usage: unpause [SERVICE...]
恢復容器服務. docker-compose unpause  恢復所有服務. docker-compose unpause web,之后恢復web服務的容器。

port

Usage: 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]
輸出服務的共有端口.
# docker-compose port web 8080   -- 8080為容器內部端口
0.0.0.0:8884

ps

Usage: ps [options] [SERVICE...]

Options:
-q    只顯示ID
顯示容器. 默認顯示name、command、state、ports

pull

Usage: pull [options] [SERVICE...]

Options:
--ignore-pull-failures  忽略pull失敗的鏡像,繼續pull其他鏡像.
pull compose文件中所指明的鏡像.

push

Usage: push [options] [SERVICE...]

Options:
push-failures  忽略錯誤.
push compose文件中所指明的鏡像

restart

Usage: restart [options] [SERVICE...] Options: -t, --timeout TIMEOUT Specify a shutdown timeout in seconds. (default: 10) 
Restarts services.

rm

Usage: rm [options] [SERVICE...]

Options:
--force   Don't ask to confirm removal
    -v            期初加載到容器的任何匿名卷
    -a, --all     Also remove one-off containers created by
                  docker-compose run
Removes stopped service containers. 如果服務在運行,需要先docker-compose stop 停止容器

By default, anonymous volumes attached to containers will not be removed. You can override this with Docker volume ls.

Any data which is not in a volume will be lost.

run

Usage: run [options] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]

.
-e KEY=VAL            設置環境變量參數,可以使用多次
--user=""         指定運行的用戶
--no-deps             不啟動link服務,只啟動run的服務.
--rm                  運行后刪除容器,后台運行模式除外(-d).
p, --publish=[]      開放端口
--service-ports       compose文件中配置什么端口,就映射什么端口.
T                    禁用TTY.
--workdir=""      設置工作目錄

啟動web服務器,並執行bash命令.

$ docker-compose run web bash
根據compose配置文件制定的端口,映射到主機:
$ docker-compose run --service-ports web python manage.py shell
指定端口映射到主機:
$ docker-compose run --publish 8080:80 -p 2022:22 -p 127.0.0.1:2021:21 web python manage.py shell
link db容器:
$ docker-compose run db psql -h db -U docker 

不linke容器,單獨啟動指定容器:

$ docker-compose run --no-deps web python manage.py shell

scale

Usage: scale [SERVICE=NUM...]
設置服務的個數.
$ docker-compose scale web=2 worker=3

start

Usage: start [SERVICE...]
啟動服務.

stop

Usage: stop [options] [SERVICE...]

Options:
default: 10).
停止容器.

up

Usage: up [options] [SERVICE...]

Options:
    -d                         后台運行,輸出容器的名字.
                               Incompatible with --abort-on-container-exit.

    --no-color                  單色輸出.
    --no-deps                  不啟動link服務.
    --force-recreate           強制重新創建compose服務,即使沒有任何改變。重新創建后啟動容器
                               Incompatible with --no-recreate.
    --no-recreate               如果容器已經存在,不重新創建.

                               Incompatible with --force-recreate.

    --no-build                 不創建重啟,即使鏡像不存在.
    --build                    重新創建鏡像,然后生成容器.

    --abort-on-container-exit  任何容器停止,自動停止所有容器.

                               Incompatible with -d.
--timeout TIMEOUT      超時時間. (default: 10)
    --remove-orphans           移除compose文件中未定義服務的容器


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM