2017-08-02 60 views
0

多克尔 - compose.yaml:等待脚本覆盖默认CMD并退出泊坞容器

version: "3" 
services: 
    mysql: 
    image: mysql:5.7 
    environment: 
     MYSQL_HOST: localhost 
     MYSQL_DATABASE: mydb 
     MYSQL_USER: mysql 
     MYSQL_PASSWORD: 1234 
     MYSQL_ROOT_PASSWORD: root 
    ports: 
     - "3307:3306" 
    expose: 
     - 3307 
    volumes: 
     - /var/lib/mysql 
     - ./mysql/migrations:/docker-entrypoint-initdb.d 
    restart: unless-stopped 
    web: 
    build: 
     context: . 
     dockerfile: web/Dockerfile 
    volumes: 
     - ./:/web 
    ports: 
     - "32768:3000" 
    environment: 
     NODE_ENV: development 
     PORT: 3000 
    links: 
     - mysql:mysql 
    depends_on: 
     - mysql 
    expose: 
     - 3000 
    command: ["./wait-for-it.sh", "mysql:3306", "--", "npm start"] 

网络Dockerfile:

FROM node:6.11.2-slim 

RUN mkdir -p /usr/src/app 
WORKDIR /usr/src/app 

COPY package.json /usr/src/app/ 
RUN npm install 

COPY . /usr/src/app 

CMD [ "npm", "start" ] # So this is overridden by the wait script and doesn't execute 

我使用这个脚本等待: https://github.com/vishnubob/wait-for-it

的等待脚本可以正常工作,但它会覆盖Web容器的现有启动命令: CMD [ "npm", "start" ]

正如你可以在泊坞窗,撰写文件中看到我使用这种方法来揭开序幕NPM启动:
command: ["./wait-for-it.sh", "mysql:3306", "--", "npm start"]

我已经尝试了一些替代例如:
command: ["./wait-for-it.sh", "mysql:3306", "--", "CMD ['npm', 'start'"]
command: ["./wait-for-it.sh", "mysql:3306", "--", "docker-entrypoint.sh"]

只有它不起作用。我从Web容器中收到此错误: web_1 | ./wait-for-it.sh: line 174: exec: npm start: not found

这是怎么回事?

回答

1

因此,首先如果您在docker-compose中使用command,那么它将覆盖CMD,这是预期的行为。码头工人如何知道你想要执行它们。

下你的做法是有点毛病CMD

command: ["./wait-for-it.sh", "mysql:3306", "--", "npm start"] 

转化为你执行

./wait-for-it.sh mysql:3306 -- "npm start" 

这应该会失败,因为没有命令npm startnpm这需要作为启动争论。所以更改命令

command: ["./wait-for-it.sh", "mysql:3306", "--", "npm", "start"] 

command: ./wait-for-it.sh mysql:3306" -- npm start 

哪种格式,你喜欢

+0

党!我很亲密。摆脱了Dockerfile中的CMD,并调整了docker-compose中的命令arg。完美的作品。谢谢! – ChrisRich