I’m in trouble with Docker build cache.
When creating the Docker image in production environment, the conf file for the development environment that remained in the Docker build cache is unintentionally used.
I created Docker image in production environment as following steps, and please let me know if there are any known bugs.
The environment for building Docker is divided into the following directories,
and images are created (docker compose build) in the directory where docker-compose.yaml is stored.
←[production environment] [development environment]→
■How to create an image for development environment
I moved to the development environment directory(-aws-infra-tf/environments/dev/network/extsvci/prxd/docker/nginx) and ran docker compose build to create a development image.
During build, the nginx.conf file(/-aws-infra-tf/environments/dev/network/extsvci/prxd/docker/nginx/nginx/nginx.conf) for the development environment was stored in the image by COPY in the Docerfile.
In this step, no modifications were made to nginx.conf for the development environment.
■How to create an image for production environment
I moved to the production environment directory(-aws-infra-tf/environments/prd/network/extsvci/prxd/docker/nginx) and ran docker compose build to create a production image.
During build, it was assumed that the nginx.conf file(-aws-infra-tf/environments/prd/network/extsvci/prxd/docker/nginx/nginx/nginx.conf) for the production environment was stored in the image by COPY in the Docerfile.
However, the nginx.conf file for the development environment(/*******-aws-infra-tf/environments/dev/network/extsvci/prxd/docker/nginx/nginx/nginx.conf) that remained in the Docker build cache was stored instead.
In this step, no modifications were made to nginx.conf for the production environment.
The contents of the Dockerfile is as follows, and there is no difference between development and production.
FROM nginx:1.23.4-****** ## Update all packages RUN apk update && apk upgrade --no-cache ## docker-entrypoint.d COPY --chown=root:root docker-entrypoint.d/99-debug-conf.sh /docker-entrypoint.d/ ## Default Configuration COPY --chown=nginx:nginx nginx/nginx.conf /etc/nginx/nginx.conf COPY --chown=nginx:nginx nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf COPY --chown=nginx:nginx nginx/conf.d/debug /etc/nginx/conf.d/debug ## VirtualHost Configuration COPY --chown=nginx:nginx nginx/templates/*.template /etc/nginx/templates/ ## Writable volumes ## - For Nginx VOLUME ["/var/cache/nginx","/var/run","/etc/nginx/conf.d"] ## - For exec command RUN mkdir -p /var/lib/amazon /var/log/amazon RUN chmod 777 /var/lib/amazon /var/log/amazon VOLUME ["/var/lib/amazon","/var/log/amazon"] ## - curl uninstall RUN apk del curl ## Sticky Bits Set RUN chmod o+t /usr RUN chmod o+t /usr/local RUN chmod o+t /usr/share RUN chmod o+t /usr/share/doc
I tried not to use cache for build(docker-compose build --no-cache), but this trouble was not resolved.
After I cleared Docker build cache(docker builder prune) and tried to create image, the symptom didn’t reproduce.