1

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
martin
  • 135
  • 5
  • 12

1 Answers1

0

Docker images generally contain an application's source code and the dependencies required to run it. Volumes are used for persistent data that needs to be preserved across changes to the underlying application. Imagine a database: if you upgraded from somedb:1.2.3 to somedb:1.2.4, you'd need to replace the database application binary (in the image) but would need to preserve the actual database contents (in a volume).

Especially in a clustered environment, don't try storing your application code in volumes. If you delete the part of your deployment setup that attempts this, then when containers redeploy with an updated image, they'll see the updated code.

David Maze
  • 130,717
  • 29
  • 175
  • 215