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

标签 docker docker-compose wait

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

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

等待脚本可以正常工作,但是它会覆盖Web容器的现有启动命令:CMD [ "npm", "start" ]
如您在docker-compose文件中看到的,我正在使用这种方法启动npm start: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_1 | ./wait-for-it.sh: line 174: exec: npm start: not found
这里发生了什么?

最佳答案

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

接下来,您的方法对于CMD有点错误

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

转化为您执行
./wait-for-it.sh mysql:3306 -- "npm start"

由于没有命令npm start,应该会失败,这是npm,它以start作为参数。因此将命令更改为
command: ["./wait-for-it.sh", "mysql:3306", "--", "npm", "start"]

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

您喜欢哪种格式

关于docker - 等待脚本会覆盖默认的CMD并退出Docker容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45455037/

相关文章:

node.js - Docker-compose up : no such file or directory, open '/usr/src/app/package.json'

java - 错误:The method elementToBeClickable(By) in the type ExpectedConditions is not applicable for the arguments (WebElement)

node.js - Docker Postgres 服务不允许我连接到它

javascript - 从 Docker 内部调试 Node.js 应用程序

docker - 如何从 docker 容器访问主机的 localhost 127.0.0.1

Java异步servlet : Wait for a specific event

java - 如何在Java中的不同对象之间进行等待和通知?

docker - 从注册表获取最新的 Docker 镜像创建日期

linux - 使用 yum 在 centos 上安装特定包版本

docker - 使用 docker-compose 来回退环境变量