【问题标题】:Docker : How to know if mongo is running using shell script file in Alpine/nodeDocker:如何知道 mongo 是否在 Alpine/node 中使用 shell 脚本文件运行
【发布时间】:2020-03-12 01:29:37
【问题描述】:

我正在开发一个使用 docker 部署的 MEAN STACK 应用程序。 docker-compose.yml 文件包含两个服务,一个用于 Angular 和 express,另一个用于 mongoDB。

在 docker-compose.yml 文件中:

version: '2' # specify docker-compose version

# Define the services/containers to be run
services:
  webapp: #name of the second service
    build: ./ # specify the directory of the Dockerfile
    volumes : 
          - ./dockerScripts:/temp/scripts
    entrypoint:
          -  /temp/scripts/wait-for-service.sh
          - 'localhost'
          - '27018'
          - 'npm run serve' 
    ports:
      - "3000:3000" #specify ports forewarding

  appdb: # name of the third service
    image: mongo # specify image to build container from
    command: mongod --port 27018
    expose:
      - 27018
    ports:
      - "27018:27018" # specify port forewarding

下面是dockerFile的内容:

FROM mhart/alpine-node:10

MAINTAINER https://hub.docker.com/u/mhart/

RUN apk update && \
    apk add git && \
    apk add --no-cache python build-base && \
    apk add busybox-extras && \
    apk --no-cache add procps

#RUN apk add --no-cache python build-base

RUN mkdir -p /usr/src/

WORKDIR /usr/src/

COPY package.json .

RUN npm install --production

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

在执行 webapp 命令之前,我需要检查 mongodb 是否在指定端口上启动。

为了实现这一点,我编写了一个如下的 shell 脚本文件:

set -e

host="$1"
port="$2"
svc="$3"

echo `Inspectingggggg ` $host $port 
until `telnet $host $port`; do
# until `ps -ax`; do
  >&2 echo "Service is unavailable - going to sleeping "
  sleep 5
done

>&2 echo "Service is now up, will execute npm run " $svc

npm run $svc

**

我面临的问题是如何检查mongodb服务是否启动 在执行 webapp 服务之前?

**

使用 telnet 命令我总是收到以下错误:

telnet: can't connect to remote host (127.0.0.1): Connection refused
templatexapp_1  | Service is unavailable - going to sleeping 

【问题讨论】:

    标签: mongodb shell docker docker-compose telnet


    【解决方案1】:

    depends_on 中定义您的相互依赖关系,以依赖顺序启动服务。这消除了一个 shell 脚本来检查 mongo 是否正在运行。所以你可以简单地拥有这个docker-compose.yml 文件:

    version: '2' # specify docker-compose version
    
    # Define the services/containers to be run
    services:
      webapp: #name of the second service
        build: ./ # specify the directory of the Dockerfile
        volumes : 
              - ./dockerScripts:/temp/scripts
        depends_on:
              - appdb
        command: npm run serve
        ports:
          - "3000:3000" #specify ports forewarding
    
      appdb: # name of the third service
        image: mongo # specify image to build container from
        command: mongod --port 27018
        expose:
          - 27018
        ports:
          - "27018:27018" # specify port forewarding
    

    注意webapp服务中的这个部分:

        depends_on:
              - appdb
    

    这确保appdb 将在webapp 之前启动。

    【讨论】:

    • 会不会也保证webapp启动前端口是开放的?即,不只是跑步,而是准备好了吗?
    猜你喜欢
    • 1970-01-01
    • 2020-04-01
    • 2017-12-01
    • 2010-11-29
    • 1970-01-01
    • 2013-01-01
    • 2015-10-13
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多