等待脚本会覆盖默认的CMD并退出Docker容器

等待脚本会覆盖默认的CMD并退出Docker容器

本文介绍了等待脚本会覆盖默认的CMD并退出Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Docker-compose.yaml:

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:

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

我正在使用以下等待脚本: https://github.com/vishnubob/wait-for-it

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

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

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

正如您在docker-compose文件中看到的那样,我正在使用这种方法启动npm start:
命令:["./wait-for-it.sh","mysql:3306",-","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"]

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

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

只有它不起作用.我从Web容器中收到此错误: web_1 |./wait-for-it.sh:第174行:exec:npm开始:未找到

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

这是怎么回事?

推荐答案

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

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.

接下来,您对CMD的处理方式有点错误

Next your approach is a bit wrong with the CMD

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

翻译为您执行

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

由于没有命令 npm start 的原因,它应该失败,而将作为参数的 npm .因此,将命令更改为

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

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

您喜欢的哪种格式

这篇关于等待脚本会覆盖默认的CMD并退出Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:34