3

Trying to dockerize a django project for the first time, I understand that for production my Dockerfile should have ADD that copies the django project to the container.

But for local development I need every change to the code to take effect immediately, for that I read it's recommended to mount a volume when I run docker ( docker run -v path:path ) but does that mean I need to have a different Dockerfile for local development? one that doesn't run the ADD command?

Dan Bolofe
  • 1,099
  • 2
  • 13
  • 21

3 Answers3

5

No you may not need two files. You can use same folder in ADD command in volume.

See this django tutorial from official docker page:

https://docs.docker.com/compose/django/

Dockerfile

FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

Compose file

version: '2'
services:
  db:
    image: postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
atv
  • 1,950
  • 4
  • 21
  • 26
0

Instead of binding a local folder to a container path, you could create a volume that can perists anywhere you want (as explained in this answer with the local persist pluging driver or even with a more advanced driver like flocker)

That way, your data persists in a data volume which can be accessed:

  • either locally (local persist plugin)
  • or still locally, through a dedicated container (svendowideit/samba/) mounting that volume
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

No need for 2 dockerfiles, if you mount with "docker run -v hostpath:containerpath" it mounts hostpath even if containerpath already exists!

Dan Bolofe
  • 1,099
  • 2
  • 13
  • 21