Search code examples
dockerkubernetesazure-pipelinesazure-aksazure-repos

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. 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

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. 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.


Solution

  • Instead of adding the Git into the Docker, it will add extra layers to the docker image.

    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.

    command you can use the

    docker create --name container_name 
    

    Docker create will create the new container without running it.

    The docker container create (or shorthand: docker create) command creates a new container from the specified image, without starting it.

    When creating a container, the docker daemon creates a writeable container layer over the specified image and prepares it for running the specified command. The container ID is then printed to STDOUT. This is similar to docker run -d except the container is never started.

    So once container filesystem there run command to copy a file from docker to CI machine simple as that.

    Docker copy command

    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.