?
This document uses PHP Chinese website manual Release
Usage: run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...] Options: -d Detached mode: Run container in the background, print new container name. --name NAME Assign a name to the container --entrypoint CMD Override the entrypoint of the image. -e KEY=VAL Set an environment variable (can be used multiple times) -u, --user="" Run as specified username or uid --no-deps Don't start linked services. --rm Remove container after run. Ignored in detached mode. -p, --publish=[] Publish a container's port(s) to the host --service-ports Run command with the service's ports enabled and mapped to the host. -v, --volume=[] Bind mount a volume (default []) -T Disable pseudo-tty allocation. By default `docker-compose run` allocates a TTY. -w, --workdir="" Working directory inside the container
針對(duì)服務(wù)運(yùn)行一次性命令。例如,以下命令啟動(dòng)該web
服務(wù)并bash
作為其命令運(yùn)行。
docker-compose run web bash
您使用的命令run
從具有由服務(wù)定義的配置的新容器中啟動(dòng),包括卷,鏈接和其他詳細(xì)信息。但是,有兩個(gè)重要的區(qū)別。
首先,通過(guò)的命令將run
覆蓋服務(wù)配置中定義的命令。例如,如果web
服務(wù)配置以bash
開(kāi)頭,則將其docker-compose run web python app.py
覆蓋python app.py
。
第二個(gè)區(qū)別是該docker-compose run
命令不會(huì)創(chuàng)建服務(wù)配置中指定的任何端口。這可以防止端口與已打開(kāi)的端口發(fā)生沖突。如果您確實(shí)想要創(chuàng)建服務(wù)的端口并將其映射到主機(jī),請(qǐng)指定--service-ports
標(biāo)志:
docker-compose run --service-ports web python manage.py shell
或者,可以使用--publish
或-p
選項(xiàng),就像使用docker run
:
docker-compose run --publish 8080:80 -p 2022:22 -p 127.0.0.1:2021:21 web python manage.py shell
如果啟動(dòng)使用鏈接配置的服務(wù),則該run
命令首先檢查鏈接服務(wù)是否正在運(yùn)行,并在服務(wù)停止時(shí)啟動(dòng)該服務(wù)。一旦所有鏈接的服務(wù)正在運(yùn)行,run
執(zhí)行您通過(guò)它的命令。例如,您可以運(yùn)行:
docker-compose run db psql -h db -U docker
這將為鏈接db
容器打開(kāi)一個(gè)交互式 PostgreSQL shell 。
如果您不希望run
命令啟動(dòng)鏈接容器,請(qǐng)使用--no-deps
國(guó)旗:
docker-compose run --no-deps web python manage.py shell