The*_*ken 5 docker dockerfile docker-registry docker-compose
我是否必须首先克隆 wait-for-it 存储库并编辑 wait-for-it.sh 文件的部分?
https://github.com/vishnubob/wait-for-it/blob/master/wait-for-it.sh
我试图在我的客户服务连接并运行其服务器后 5 秒后触发我的主文件。(或每当客户服务正确连接服务器时)。
我知道在 Dockerfile 中,我们需要向其中添加这些命令(将文件复制到 workdir,并将 shell 脚本作为可执行文件运行。)
...
COPY wait-for-it.sh .
RUN chmod +x /wait-for-it.sh
...
Run Code Online (Sandbox Code Playgroud)
这是我当前的 Docker-Compose 文件
version: '3'
services:
books:
build: './books'
container_name: "horus-books"
ports:
- "30043:30043"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
customers:
depends_on:
- books
build: './customers'
container_name: "horus-customers"
ports:
- "6000:6000"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
main:
depends_on:
- customers
build: './main'
container_name: "horus-main"
ports:
- "4555:4555"
command: ["./wait-for-it.sh", "customers:6000", "--", "node", "main.js"]
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
Run Code Online (Sandbox Code Playgroud)
主服务Dockerfile
FROM node:12.14.0
WORKDIR /usr/src/app
COPY package*.json ./
COPY . /usr/src/app
COPY wait-for-it.sh .
RUN chmod +x /wait-for-it.sh
RUN npm install
EXPOSE 4555
CMD ["node", "main.js"]
Run Code Online (Sandbox Code Playgroud)
通常你根本不会把它放在你的docker-compose.yml文件中。脚本需要内置到映像中,并且需要知道它的标准启动顺序才能运行它。
使用入口点脚本进行一些初始设置,然后exec "$@"将容器的命令用作主进程,这是一种相当常见的模式。例如,这让您可以使用wait-for-it.sh脚本等待后端启动,然后运行任何主命令。例如,docker-entrypoint.sh脚本可能如下所示:
#!/bin/sh
# Abort on any error (including if wait-for-it fails).
set -e
# Wait for the backend to be up, if we know where it is.
if [ -n "$CUSTOMERS_HOST" ]; then
/usr/src/app/wait-for-it.sh "$CUSTOMERS_HOST:${CUSTOMERS_PORT:-6000}"
fi
# Run the main container command.
exec "$@"
Run Code Online (Sandbox Code Playgroud)
您需要确保此脚本是可执行的,并将其ENTRYPOINT设为映像的,否则您可以保持 Dockerfile 几乎不变。
FROM node:12.14.0
WORKDIR /usr/src/app
COPY package*.json ./
# Do this _before_ copying the entire application in
# to avoid repeating it on rebuild
RUN npm install
# Includes wait-for-it.sh and docker-entrypoint.sh
COPY . ./
RUN chmod +x ./wait-for-it.sh ./docker-entrypoint.sh
EXPOSE 4555
# Add this line
# It _must_ use the JSON-array syntax
ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["node", "main.js"]
Run Code Online (Sandbox Code Playgroud)
在您docker-compose.yml需要添加配置来说明后端在哪里,但您不需要覆盖command:.
main:
depends_on:
- customers
build: './main'
ports:
- "4555:4555"
environment:
- CUSTOMERS_HOST=customers
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6930 次 |
| 最近记录: |