简体   繁体   English

在构建 docker 映像时,将 meta.json 中的版本号提交到 git 存储库

[英]commit version number in meta.json to git repo when building docker image

I have a application running react as front end and node as back end code.我有一个应用程序运行响应作为前端和节点作为后端代码。 In react public folder, we have a meta.json which has the version number, every time we run npm run build, it will update version number in that file.在反应公共文件夹中,我们有一个 meta.json 有版本号,每次我们运行 npm 运行构建,它会更新该文件中的版本号。 we are using this method to make sure the website always displays the new release version, in the database also we update the version number and if both doesn't match website automatically loads new version.我们使用这种方法来确保网站始终显示新的发布版本,在数据库中我们也会更新版本号,如果两者都不匹配,网站会自动加载新版本。

We are on the process of shifting to Kubernetes and the problem now I have is we have a Dockerfile for react in which we have following steps我们正在转移到 Kubernetes 的过程中,现在我遇到的问题是我们有一个 Dockerfile 用于反应,其中我们有以下步骤

FROM node:12.18.3 AS build

ENV CI=false
ENV WDS_SOCKET_PORT=0

WORKDIR /app

COPY ["package.json", "package-lock.json", "./"]

RUN npm install --production

COPY . .

RUN npm run build:development

FROM nginx:alpine

COPY --from=build /app/build /usr/share/nginx/html

COPY --from=build /app/nginx-custom.conf /etc/nginx/conf.d/default.conf

We are using this Dockerfile in azure pipelines and building a image with it and pushing that docker image to Azure container registry and using kubectl restart to pull that image and restart the deployment in AKS. We are using this Dockerfile in azure pipelines and building a image with it and pushing that docker image to Azure container registry and using kubectl restart to pull that image and restart the deployment in AKS. After npm run build from the Dockerfile, my meta.json file will have updated version, I want to commit and push that changed files to azure repo, so that next time if pipeline is run it will have updated version number. After npm run build from the Dockerfile, my meta.json file will have updated version, I want to commit and push that changed files to azure repo, so that next time if pipeline is run it will have updated version number.

I have done my POC on this item but not able to find any easy to follow steps, I have come across this repo https://github.com/ShadowApex/docker-git-push but not clear on how to execute this one properly, any help would be greatly appreciated.我已经在这个项目上完成了我的 POC,但找不到任何容易遵循的步骤,我遇到了这个 repo https://github.com/ShadowApex/docker-git-push但不清楚如何正确执行这个, 任何帮助将不胜感激。

Instead of adding the Git into the Docker , it will add extra layers to the docker image.它不会将Git添加到Docker中,而是将额外的层添加到 docker 图像中。

Once your image build is completed after that what you can do is something like copy the JSON outside of the docker image and push it from the CI machine to git or bucket where you want to manage.一旦您的映像构建完成,您可以做的就是将 JSON 复制到docker映像之外,并将其从CI机器推送到 git 或您想要管理的存储桶。

command you can use the命令你可以使用

docker create --name container_name 

Docker create will create the new container without running it. Docker create创建容器而不运行它。

The docker container create (or shorthand: docker create) command creates a new container from the specified image, without starting it. docker 容器创建(或简写:docker 创建)命令从指定的映像创建一个新容器,而不启动它。

When creating a container, the docker daemon creates a writeable container layer over the specified image and prepares it for running the specified command.在创建容器时,docker 守护进程在指定的镜像上创建一个可写容器层,并准备运行指定的命令。 The container ID is then printed to STDOUT.然后将容器 ID 打印到 STDOUT。 This is similar to docker run -d except the container is never started.这类似于 docker run -d ,除了容器从不启动。

So once container filesystem there run command to copy a file from docker to CI machine simple as that.因此,一旦容器文件系统运行命令将文件从 docker 复制到CI机器,就这么简单。

Docker copy command Docker 复制命令

docker cp container_name:/app/build/meta.json .

Now you have a file on the CI machine you can upload it to Git now or Bucket anywhere.现在您在 CI 机器上有一个文件,您可以立即将其上传到 Git 或 Bucket 任何地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM