Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use docker compose watch instead of volumes #4712

Closed
epou opened this issue Dec 4, 2023 · 4 comments · May be fixed by #5434
Closed

Use docker compose watch instead of volumes #4712

epou opened this issue Dec 4, 2023 · 4 comments · May be fixed by #5434
Labels
answered Automatically closed as answered after a custom delay enhancement

Comments

@epou
Copy link

epou commented Dec 4, 2023

Description

What are you proposing? How should it be implemented?

Docker just released Docker Compose Watch with v2.22. (NOTE: Instead of docker compose up one should use docker compose watch, so docs should be modified accordingly)

With this feature, the watch option can automatically synchronize the local source code with the code inside the containers without using volumes.

Proposal in file local.yml:

[...]

services:
  django:{% if cookiecutter.use_celery == 'y' %} &django{% endif %}
      build:
        context: .
        dockerfile: ./compose/local/django/Dockerfile
      image: {{ cookiecutter.project_slug }}_local_django
      container_name: {{ cookiecutter.project_slug }}_local_django
      depends_on:
        - postgres
        {%- if cookiecutter.use_celery == 'y' %}
        - redis
        {%- endif %}
        {%- if cookiecutter.use_mailpit == 'y' %}
        - mailpit
        {%- endif %}
-     volumes:
-       - .:/app:z
+     develop:
+       watch:
+         - action: sync
+           path: ./
+           target: /app
+         - action: rebuild
+           path: ./requirements/
  
      env_file:
        - ./.envs/.local/.django
        - ./.envs/.local/.postgres
      ports:
        - '8000:8000'
      command: /start

[...]

   docs:
      image: {{ cookiecutter.project_slug }}_local_docs
      container_name: {{ cookiecutter.project_slug }}_local_docs
      build:
        context: .
        dockerfile: ./compose/local/docs/Dockerfile
      env_file:
        - ./.envs/.local/.django
-     volumes:
-       - ./docs:/docs:z
-       - ./config:/app/config:z
-       - ./{{ cookiecutter.project_slug }}:/app/{{ cookiecutter.project_slug }}:z
+     develop:
+       watch:
+         - action: sync
+           path: ./docs
+           target: /docs
+         - action: sync
+           path: ./config
+           target: /app/config
+         - action: sync
+           path: ./{{ cookiecutter.project_slug }}
+           target: /app/{{ cookiecutter.project_slug }}
+         - action: rebuild
+           path: ./requirements/
      ports:
        - '9000:9000'
      command: /start-docs

[...]

{%- if cookiecutter.frontend_pipeline in ['Gulp', 'Webpack'] %}

  node:
    build:
      context: .
      dockerfile: ./compose/local/node/Dockerfile
    image: {{ cookiecutter.project_slug }}_local_node
    container_name: {{ cookiecutter.project_slug }}_local_node
    depends_on:
      - django
    volumes:
-     - .:/app:z
      # http://jdlm.info/articles/2016/03/06/lessons-building-node-app-docker.html
      - /app/node_modules
+   develop:
+      watch:
+        - action: sync  # NOTE: node_modules already ignored since they're in .gitignore
+          path: ./
+          target: /app
+        - action: rebuild
+          path: package.json

    command: npm run dev
    ports:
      - '3000:3000'
      {%- if cookiecutter.frontend_pipeline == 'Gulp' %}
      # Expose browsersync UI: https://www.browsersync.io/docs/options/#option-ui
      - '3001:3001'
      {%- endif %}

{%- endif %}
[...]

NOTE: Not sure if action sync+restart could be considered as a replacement for watchdog/watchfiles.

Rationale

Why should this feature be implemented?

Easily synchronize local source code with code inside docker containers, even automatically rebuild on requirements.txt/packages.json changes. Improve development experience.

@leetdavid
Copy link

I don't think this works with VSCode Dev containers though, since you can't write from within the container back out into the local environment.

@foarsitter foarsitter linked a pull request Oct 7, 2024 that will close this issue
@foarsitter
Copy link
Collaborator

@epou can I have your eyes for #5434

In order to get uv to work with a .venv folder at the default location I had to use the watch directive.

@foarsitter
Copy link
Collaborator

since you can't write from within the container back out into the local environment.

@leetdavid his remark is correct and I couldn't get it to work.

@foarsitter foarsitter added the answered Automatically closed as answered after a custom delay label Oct 28, 2024
Copy link
Contributor

github-actions bot commented Nov 8, 2024

Assuming the question was answered, this will be automatically closed now.

@github-actions github-actions bot closed this as completed Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered Automatically closed as answered after a custom delay enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants