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
 before_script:
  - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
 script:
  - docker build -t $CI_REGISTRY_IMAGE:latest .
  - docker push $CI_REGISTRY_IMAGE:latest
 tags:
  - build-image

deploy docker image:
 stage: deploy
 before_script:
  - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
 script:
  - docker service update --with-registry-auth --image $CI_REGISTRY_IMAGE:latest 
  $SWARM_SERVICE_NAME -d 
 tags:
  - deploy-stack

Source: StackOverflow