alex
alex

Reputation: 31

Why aren't my docker images, built by "docker-compose build", using the correct version of my code?

Docker doesn't use the latest code after running git checkout <non_master_branch>, while I can see it in the vscode.

I am using the following docker-compose file:

version: '2'

volumes: 
  pgdata:
  backend_app:

services:
  nginx:
    container_name: nginx-angular-dev
    image: nginx-angular-dev
    build:
      context: ./frontend
      dockerfile: /.docker/nginx.dockerfile
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - web

  web:
    container_name: django-app-dev
    image: django-app-dev
    build:
      context: ./backend
      dockerfile: /django.dockerfile
    command: ["./wait-for-postgres.sh", "db", "./django-entrypoint.sh"]
    volumes:
      - backend_app:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
    env_file: .env
    environment:
      FRONTEND_BASE_URL: http://192.168.99.100/
      BACKEND_BASE_URL: http://192.168.99.100/api/
      MODE_ENV: DOCKER_DEV

  db:
    container_name: django-db
    image: postgres:10
    env_file: .env
    volumes:
      - pgdata:/var/lib/postgresql/data

I have tried docker-compose build --no-cache, followed by docker-compose up --force-recreate but it didn't solve the problem.

What is the root of my problem?

Upvotes: 1

Views: 430

Answers (1)

David Maze
David Maze

Reputation: 158758

Your volumes: are causing problems. Docker volumes aren't intended to hold code, and you should delete the volume declarations that mention backend_app:.

Your docker-compose.yml file says in part:

volumes: 
  backend_app:
services:
  web:
    volumes:
      - backend_app:/code

backend_app is a named volume: it keeps data that must be persisted across container runs. If the volume doesn't exist yet the first time then data will be copied into it from the image, but after that, Docker considers it to contain critical user data that must not be updated.

If you keep code or libraries in a Docker volume, Docker will never update it, even if the underlying image changes. This is a common problem in JavaScript applications that mount an anonymous volume on their node_modules directory.

As a temporary workaround, if you docker-compose down -v, it will delete all of the volumes, including the one with your code in it, and the next time you start it will get recreated from the image.

The best solution is to simply not use a volume here at all. Delete the lines above from your docker-compose.yml file. Develop and test your application in a non-Docker environment, and when you're ready to do integration testing, run docker-compose up --build. Your code will live in the image, and an ordinary docker build will produce a new image with new code.

Upvotes: 3

Related Questions