在一个 docker 文件中,我使用它来运行 Node 和 Java 来执行以下操作:
- 运行测试脚本
- 运行 java 可执行文件(以捆绑屏幕截图)
- 运行我的应用程序
我从文档中了解到,这是一种不好的做法,因为这是一个“整体”部署。我应该根据各个任务将其分成单独的图像。所以,大概我会有 3 个 docker 文件。
Dockerfile 1:测试脚本
FROM node:8
RUN node --version
RUN apt-get update && apt-get install -yq libgconf-2-4
# Note: this installs the necessary libs to make the bundled version of Chromium work
RUN apt-get update && apt-get install -y wget --no-install-recommends \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install -y google-chrome-unstable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst ttf-freefont \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get purge --auto-remove -y curl \
&& rm -rf /src/*.deb
# project repo
RUN git clone ...
WORKDIR /myApp/
RUN npm install
RUN npm i puppeteer
CMD ["node", "test.ts"]
Dockerfile 2:Java 可执行文件(捆绑镜像)
FROM openjdk
RUN git clone -b ...
WORKDIR /myApp/
CMD ["java -jar", "ImageTester.jar"]
Dockerfile 3:运行应用程序
FROM node:8
RUN node --version
RUN git clone -b ...
WORKDIR /myApp/
RUN npm install
EXPOSE 9999
CMD ["npm", "start"]
问题是,究竟如何做到这一点?在我的案例中,非整体部署是如何实现的?如何在一个项目中运行 3 个 docker 镜像?
最佳答案
使用 docker-compose。在 docker-compose.yml 文件中,您将有 3 个服务指向 3 个不同的 dockerfile。
例如:
version: '3'
services:
test:
build:
context: .
dockerfile: Dockerfile.test
images:
build:
context: .
dockerfile: Dockerfile.images
app:
build:
context: .
dockerfile: Dockerfile.app
关于java - 如何从整体部署中拆分 docker 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48936809/