Dockerfile 和 Jenkinsfile - COPY 存储库
Dockerfile and Jenkinsfile - COPY repository
我正在尝试在 Jenkinsfile 中克隆我所有的 git 存储库:
stage('Clone git') {
steps {sh 'mkdir repo1'
sh 'cd repo1'
dir ('repo1') {
git ([url : 'https://github.../repo1.git', branch : 'develop', credentialsId : '***'])}
sh 'mkdir repo2'
sh 'cd repo2'
dir ('repo2') {
git ([url : 'https://github.../repo2.git', branch : 'develop', credentialsId : '***'])}
}
}
stage('Build Docker') {
steps {sh 'cd ..'
sh 'docker build -t myimage .'
sh 'docker run myimage'
}
然后在我的 Dockerfile 中我有这个:
FROM node:12.2.0-alpine
COPY . /myapp
WORKDIR /myapp
RUN apk update
FROM gradle:6.3-jdk11 AS build
COPY --chown=gradle:gradle . /repo1
WORKDIR /repo1
RUN gradle build --no-daemon
WORKDIR /repo2
RUN npm install
RUN ng serve
请帮助我理解,如果我这样做是正确的,特别是 COPY 命令。
我想,它是这样工作的:首先它会克隆 git repos 到 repo1 和 repo2 文件夹。
然后在 Dockerfile 中它会在 myapp 文件夹中创建一个布局,它会复制 myapp 文件夹下的两个 repo 文件夹。
问题是,我需要 运行 这两个服务 - 在 repo1 中它是后端,gradle 需要构建,这就是为什么我将 gradle 复制到 repo1 文件夹,不确定我是否应该在 COPY 中使用 myapp/repo1。
repo2 是前端,我认为没有必要复制任何东西,只需复制到 select WORKDIR repo2 和 运行 服务器。
所以问题是,COPY 就够了吗。 /myapp 在第一步中,它会复制那里的每个 repo 文件夹,还是只是将 docker 图像复制到 myapp 文件夹,我需要单独复制所有创建的文件夹?
当使用单个 Docker 文件时,只能创建一个 docker 图像,因此如果您希望这两种服务都 运行 您需要构建并 运行 它们单独(可能带有分离模式的 -d
标志)。
在同一容器中使用多个应用程序 - docker docs:
A container’s main running process is the ENTRYPOINT and/or CMD at the
end of the Dockerfile. It is generally recommended that you separate
areas of concern by using one service per container. That service may
fork into multiple processes (for example, Apache web server starts
multiple worker processes). It’s ok to have multiple processes, but to
get the most benefit out of Docker, avoid one container being
responsible for multiple aspects of your overall application. You can
connect multiple containers using user-defined networks and shared
volumes.
您可以在容器启动时为 运行 创建一个脚本,例如:
#!/bin/bash
# Start the first process
./my_first_process -D
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start my_first_process: $status"
exit $status
fi
# Start the second process
./my_second_process -D
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start my_second_process: $status"
exit $status
fi
# Naive check runs checks once a minute to see if either of the processes exited.
# This illustrates part of the heavy lifting you need to do if you want to run
# more than one service in a container. The container exits with an error
# if it detects that either of the processes has exited.
# Otherwise it loops forever, waking up every 60 seconds
while sleep 60; do
ps aux |grep my_first_process |grep -q -v grep
PROCESS_1_STATUS=$?
ps aux |grep my_second_process |grep -q -v grep
PROCESS_2_STATUS=$?
# If the greps above find anything, they exit with 0 status
# If they are not both 0, then something is wrong
if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then
echo "One of the processes has already exited."
exit 1
fi
done
然后让你的 CMD 命令 运行 脚本如下:
CMD ./my_wrapper_script.sh
其文档中列出的另一个选项 Docker 包括使用 supervisord
,它应该通过适当的配置处理您打算在容器中 运行 的所有主要进程,here's 如何配置。请记住,它是要安装在您的容器或基础映像上的另一个包。
此外,由于节点不只是从默认目录静态地提供网站文件,您需要使用 node
命令并指向父 JS 文件,该文件应定义节点网络服务器来提供文件,例如express 或 http,参见 this example. Though if you want to use a production webserver you better build your project and use a commercial webserver like nginx or apache http, for them you build the project (possibly inside the Dockerfile) put them in their respected directories and start the web server process, here's an example
如果您只是 运行 这两个应用程序,您可以将 Dockerfile 拆分为 2 个 Dockerfile,一个用于后端,一个用于前端,并在 docker-compose.yml
中引用它们
docker-compose.yml
示例:
version: '3.9'
services:
backend:
image: backend:${TAG:-latest}
build:
context: ./backend
dockerfile: Dockerfile
frontend:
image: frontend:${TAG:-latest}
build:
context: ./frontend
dockerfile: Dockerfile
到 运行 两个服务 docker-compose, exec docker-compose up
我正在尝试在 Jenkinsfile 中克隆我所有的 git 存储库:
stage('Clone git') {
steps {sh 'mkdir repo1'
sh 'cd repo1'
dir ('repo1') {
git ([url : 'https://github.../repo1.git', branch : 'develop', credentialsId : '***'])}
sh 'mkdir repo2'
sh 'cd repo2'
dir ('repo2') {
git ([url : 'https://github.../repo2.git', branch : 'develop', credentialsId : '***'])}
}
}
stage('Build Docker') {
steps {sh 'cd ..'
sh 'docker build -t myimage .'
sh 'docker run myimage'
}
然后在我的 Dockerfile 中我有这个:
FROM node:12.2.0-alpine
COPY . /myapp
WORKDIR /myapp
RUN apk update
FROM gradle:6.3-jdk11 AS build
COPY --chown=gradle:gradle . /repo1
WORKDIR /repo1
RUN gradle build --no-daemon
WORKDIR /repo2
RUN npm install
RUN ng serve
请帮助我理解,如果我这样做是正确的,特别是 COPY 命令。
我想,它是这样工作的:首先它会克隆 git repos 到 repo1 和 repo2 文件夹。
然后在 Dockerfile 中它会在 myapp 文件夹中创建一个布局,它会复制 myapp 文件夹下的两个 repo 文件夹。
问题是,我需要 运行 这两个服务 - 在 repo1 中它是后端,gradle 需要构建,这就是为什么我将 gradle 复制到 repo1 文件夹,不确定我是否应该在 COPY 中使用 myapp/repo1。
repo2 是前端,我认为没有必要复制任何东西,只需复制到 select WORKDIR repo2 和 运行 服务器。
所以问题是,COPY 就够了吗。 /myapp 在第一步中,它会复制那里的每个 repo 文件夹,还是只是将 docker 图像复制到 myapp 文件夹,我需要单独复制所有创建的文件夹?
当使用单个 Docker 文件时,只能创建一个 docker 图像,因此如果您希望这两种服务都 运行 您需要构建并 运行 它们单独(可能带有分离模式的 -d
标志)。
在同一容器中使用多个应用程序 - docker docs:
A container’s main running process is the ENTRYPOINT and/or CMD at the end of the Dockerfile. It is generally recommended that you separate areas of concern by using one service per container. That service may fork into multiple processes (for example, Apache web server starts multiple worker processes). It’s ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.
您可以在容器启动时为 运行 创建一个脚本,例如:
#!/bin/bash
# Start the first process
./my_first_process -D
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start my_first_process: $status"
exit $status
fi
# Start the second process
./my_second_process -D
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start my_second_process: $status"
exit $status
fi
# Naive check runs checks once a minute to see if either of the processes exited.
# This illustrates part of the heavy lifting you need to do if you want to run
# more than one service in a container. The container exits with an error
# if it detects that either of the processes has exited.
# Otherwise it loops forever, waking up every 60 seconds
while sleep 60; do
ps aux |grep my_first_process |grep -q -v grep
PROCESS_1_STATUS=$?
ps aux |grep my_second_process |grep -q -v grep
PROCESS_2_STATUS=$?
# If the greps above find anything, they exit with 0 status
# If they are not both 0, then something is wrong
if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then
echo "One of the processes has already exited."
exit 1
fi
done
然后让你的 CMD 命令 运行 脚本如下:
CMD ./my_wrapper_script.sh
其文档中列出的另一个选项 Docker 包括使用 supervisord
,它应该通过适当的配置处理您打算在容器中 运行 的所有主要进程,here's 如何配置。请记住,它是要安装在您的容器或基础映像上的另一个包。
此外,由于节点不只是从默认目录静态地提供网站文件,您需要使用 node
命令并指向父 JS 文件,该文件应定义节点网络服务器来提供文件,例如express 或 http,参见 this example. Though if you want to use a production webserver you better build your project and use a commercial webserver like nginx or apache http, for them you build the project (possibly inside the Dockerfile) put them in their respected directories and start the web server process, here's an example
如果您只是 运行 这两个应用程序,您可以将 Dockerfile 拆分为 2 个 Dockerfile,一个用于后端,一个用于前端,并在 docker-compose.yml
docker-compose.yml
示例:
version: '3.9'
services:
backend:
image: backend:${TAG:-latest}
build:
context: ./backend
dockerfile: Dockerfile
frontend:
image: frontend:${TAG:-latest}
build:
context: ./frontend
dockerfile: Dockerfile
到 运行 两个服务 docker-compose, exec docker-compose up