我正在开发一个使用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
在按依赖关系顺序启动服务的depends_on
中定义您的相互依赖关系。这消除了用于检查mongo
是否正在运行的shell脚本。所以你可以简单地拥有这个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
之前启动。