How to move django project directory into a sub-directory

First the why: I would like to dockerize my Django project and I would like to keep the dockerfile in the root directory. Currently that root directory is also the Django project directory - that is, the one with `manage.py` in it. This is creating problems for me because I would like to copy an entire folder from the project directory - e.g. `myapp` to the image filesystem using docker COPY. however, the docker COPY command does not preserve the top-level directory - `myapp` in the example case - when copying but rather will copy the *contents* of the directory. Wrote the author of the comment link above, “You can try to make this less painful by copying one level higher in the src tree, if possible.”

What would be the best approach to move the source code of my project into a sub-directory within the git repository?

0
1 comment

Justin Vanderziel, you can create a sub-directory in your repository's root to house your Django project files, like:

mkdir src
git mv manage.py src/manage.py
git mv myapp src/myapp
git mv requirements.txt src/requirements.txt
# ...move other relevant files as needed

After moving the files, be sure to update your Dockerfile paths to match the new structure. For example:

# before
COPY myapp /opt/project/myapp
COPY manage.py /opt/project/
# after
COPY src/myapp  /opt/project/myapp
COPY src/manage.py

In PyCharm, update the project structure under File | Settings | Project | Project Structure > mark src as “Sources root”. Also, make sure to adjust the Django settings under File | Settings | Languages & Frameworks | Django to reflect the new locations.

1

Please sign in to leave a comment.