Search code examples
springdockerdocker-composerestart

Docker is not restarting when code changes


As described here https://docs.docker.com/compose/gettingstarted/#step-7-update-the-application if I mount the folder with the source code inside the volumes of that docker, I should see my changes instantly...

However, this is my docker-compose:

version: '3'

services: 
    database:
        ...

    engine:
        build: ./name
        restart: always
        volumes: 
            - .:/name-code
            - ./name/config-examples:/config
        ports: 
            - 8080:8080
        depends_on: 
            - database
        environment: 
            - TZ=Europe/Rome
            ...

    grafana:
        ...

and as you can see I'm mounting .:/name-code... so as soon as I change code inside this directory, I should see that docker restarting with the new JAR (I'm developing a Spring boot application)... however this is not happening, infact my local jar looks like this:

-rw-r--r--    1 user     staff     43171359 Jun  1 12:35 project.jar

but the jar in the docker is the following:

-rw-r--r--    1 root     root      43171269 May 31 19:52 app.jar

(Is just been renamed), but as you can see the last update is very different, and infact the updates are not been imported

Log:

docker-compose --verbose build           
compose.config.config.find: Using configuration files: ./docker-compose.yml
compose.cli.docker_client.get_client: docker-compose version 1.29.1, build c34c88b2
docker-py version: 5.0.0
CPython version: 3.9.0
OpenSSL version: OpenSSL 1.1.1h  22 Sep 2020
compose.cli.docker_client.get_client: Docker base_url: http+docker://localhost
compose.cli.docker_client.get_client: Docker version: Platform={'Name': 'Docker Engine - Community'}, Components=[{'Name': 'Engine', 'Version': '20.10.6', 'Details': {'ApiVersion': '1.41', 'Arch': 'amd64', 'BuildTime': '2021-04-09T22:44:56.000000000+00:00', 'Experimental': 'false', 'GitCommit': '8728dd2', 'GoVersion': 'go1.13.15', 'KernelVersion': '5.10.25-linuxkit', 'MinAPIVersion': '1.12', 'Os': 'linux'}}, {'Name': 'containerd', 'Version': '1.4.4', 'Details': {'GitCommit': '05f951a3781f4f2c1911b05e61c160e9c30eaa8e'}}, {'Name': 'runc', 'Version': '1.0.0-rc93', 'Details': {'GitCommit': '12644e614e25b05da6fd08a38ffa0cfe1903fdec'}}, {'Name': 'docker-init', 'Version': '0.19.0', 'Details': {'GitCommit': 'de40ad0'}}], Version=20.10.6, ApiVersion=1.41, MinAPIVersion=1.12, GitCommit=8728dd2, GoVersion=go1.13.15, Os=linux, Arch=amd64, KernelVersion=5.10.25-linuxkit, BuildTime=2021-04-09T22:44:56.000000000+00:00
compose.cli.verbose_proxy.proxy_callable: docker inspect_network <- ('codice_default')
compose.cli.verbose_proxy.proxy_callable: docker inspect_network -> {'Attachable': False,
 'ConfigFrom': {'Network': ''},
 'ConfigOnly': False,
 'Containers': {},
 'Created': '2021-06-01T10:31:25.2768986Z',
 'Driver': 'bridge',
 'EnableIPv6': False,
 'IPAM': {'Config': [{'Gateway': '172.31.0.1', 'Subnet': '172.31.0.0/16'}],
          'Driver': 'default',
          'Options': None},
...
compose.project.build: database uses an image, skipping
compose.project.build: grafana uses an image, skipping
compose.service.build: Building engine
[+] Building 0.3s (8/8) FINISHED                                                                                                                                                               
 => [internal] load build definition from Dockerfile                                                                                                                                      0.0s
 => => transferring dockerfile: 37B                                                                                                                                                       0.0s
 => [internal] load .dockerignore                                                                                                                                                         0.0s
 => => transferring context: 2B                                                                                                                                                           0.0s
 => [internal] load metadata for docker.io/adoptopenjdk/openjdk11:jdk-11.0.11_9-alpine-slim                                                                                               0.0s
 => [1/3] FROM docker.io/adoptopenjdk/openjdk11:jdk-11.0.11_9-alpine-slim                                                                                                                 0.0s
 => [internal] load build context                                                                                                                                                         0.0s
 => => transferring context: 1.43kB                                                                                                                                                       0.0s
 => CACHED [2/3] COPY target/*.jar app.jar                                                                                                                                                0.0s
 => [3/3] COPY config-examples /app/config                                                                                                                                                0.0s
 => exporting to image                                                                                                                                                                    0.1s
 => => exporting layers                                                                                                                                                                   0.0s
 => => writing image sha256:7162be9dfe5c15feedd12d19a5985a0f619e623eb69e949283c6e2d66442f533                                                                                              0.0s
 => => naming to docker.io/library/codice_engine                                                                                                                                          0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them 

the Dockerfile of the engine service is the following:

FROM adoptopenjdk/openjdk11:jdk-11.0.11_9-alpine-slim
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
COPY config-examples /app/config

CMD ["java","-jar","/app.jar"]

Solution

  • I read again you post, and the misunderstanding is on the fact that .:/name-code is a volume mount point, it is not part of the docker image built by docker-compose, so changing its content does not force docker to rebuild the image. Else, the new content will be accessibile from the container.

    For that reason you can see that CACHED [2/3] COPY target/*.jar app.jar is retrieved from a cached layer.

    In my opinion the solution is to write a custom Dockerfile which adds explicitly the jar as depicted here https://docs.docker.com/compose/compose-file/compose-file-v3/#dockerfile . So, each time the jar changes, the image will be rebuilt as you expect.

    Regards.