Wait script overrides default CMD and exits Docker container

心不动则不痛 提交于 2021-02-11 13:02:05

问题


Docker-compose.yaml:

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"]

Web 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

I'm using this wait script: https://github.com/vishnubob/wait-for-it

The wait script works fine, however it overrides the existing start command for the web container: CMD [ "npm", "start" ]

As you can see in the docker-compose file I'm using this approach to kick off npm start:
command: ["./wait-for-it.sh", "mysql:3306", "--", "npm start"]

I have tried a few alternative e.g:
command: ["./wait-for-it.sh", "mysql:3306", "--", "CMD ['npm', 'start'"]
command: ["./wait-for-it.sh", "mysql:3306", "--", "docker-entrypoint.sh"]

Only it is not working. I get this error from the web container: web_1 | ./wait-for-it.sh: line 174: exec: npm start: not found

What's going on here?


回答1:


So first of all if you use command in docker-compose then it will override the the CMD and that is an expected behavior. How can docker know you want to execute both of them.

Next your approach is a bit wrong with the CMD

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

translates to you executing

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

Which is supposed to fail as there is no command npm start it is npm which takes start as the argument. So change the command to

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

or

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

Whichever format you like



来源:https://stackoverflow.com/questions/45455037/wait-script-overrides-default-cmd-and-exits-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!