我想这样做,我可以在下面的代码中运行多个命令:
db:
image: postgres
web:
build: .
command: python manage.py migrate
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
links:
- db
如何执行多个命令?
我想这样做,我可以在下面的代码中运行多个命令:
db:
image: postgres
web:
build: .
command: python manage.py migrate
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
links:
- db
如何执行多个命令?
当前回答
想好了,使用bash-c。
例子:
command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
多行中的相同示例:
command: >
bash -c "python manage.py migrate
&& python manage.py runserver 0.0.0.0:8000"
Or:
command: bash -c "
python manage.py migrate
&& python manage.py runserver 0.0.0.0:8000
"
其他回答
最干净?
---
version: "2"
services:
test:
image: alpine
entrypoint: ["/bin/sh","-c"]
command:
- |
echo a
echo b
echo c
如果您是第二版,请尝试使用“;”分隔命令例如
命令:“sleep 20;echo'a'”
这是我解决这个问题的方法:
services:
mongo1:
container_name: mongo1
image: mongo:4.4.4
restart: always
# OPTION 01:
# command: >
# bash -c "chmod +x /scripts/rs-init.sh
# && sh /scripts/rs-init.sh"
# OPTION 02:
entrypoint: [ "bash", "-c", "chmod +x /scripts/rs-init.sh && sh /scripts/rs-init.sh"]
ports:
- "9042:9042"
networks:
- mongo-cluster
volumes:
- ~/mongors/data1:/data/db
- ./rs-init.sh:/scripts/rs-init.sh
- api_vol:/data/db
environment:
*env-vars
depends_on:
- mongo2
- mongo3
基于Alpine的图像实际上似乎没有安装bash,但您可以使用sh或ash链接到/bin/busybox。
docker-compose.yml示例:
version: "3"
services:
api:
restart: unless-stopped
command: ash -c "flask models init && flask run"
想好了,使用bash-c。
例子:
command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
多行中的相同示例:
command: >
bash -c "python manage.py migrate
&& python manage.py runserver 0.0.0.0:8000"
Or:
command: bash -c "
python manage.py migrate
&& python manage.py runserver 0.0.0.0:8000
"