Docker Update Code in Volume with Gitlab CI / CD

i am learning docker and i just encountered a problem i cannot solve.

I want to update source code in my docker swarm nodes when i make changes and push them. I just have a index php which echos “Hello World” and shows phpinfo. I am using data volumes since its recommended for production ( bind mounts for dev ).

my problem is: how to i update source code while using volumes? whats the best practice for this scenario?

Currently when i push changes to gitlab in my index php my gitlab-runner recreates the Docker Image and updates my swarm service.

This works when i change the php version in my Dockerfile but changes in index.php wont be affected.

My example Dockerfile looks like this. i just copy the index.php to /var/www/html in the container and thats it.

When i deploy my swarm stack the first time everything works

FROM php:7.4.5-apache
# copy files
COPY src/index.php /var/www/html/
# apahe settings
RUN echo 'ServerName localhost' >> /etc/apache2/apache2.conf

My gitlab-ci.yml looks like this

build docker image:
stage: build
  - docker build -t $CI_REGISTRY_IMAGE:latest .
  - docker push $CI_REGISTRY_IMAGE:latest
  - build-image

deploy docker image:
 stage: deploy
  - docker service update --with-registry-auth --image $CI_REGISTRY_IMAGE:latest 
  - deploy-stack

Source: StackOverflow