4

I was thinking to use a docker for django.

Since this docker image will be exclusive for a particular django project. Is it ok to just pip install everything in docker rather than creating a virtualenv and then install all the required django and related packages using pip?

So what is the best way and also safer way if someone want to stick to docker for django projects?

1
  • I think it is. I am doing the same in my application. Commented Mar 7, 2019 at 13:16

2 Answers 2

2

You are right that you don't need a virtual environment inside the django container.

If you are always using pip and store the the requirements in a requirements.txt you can use this to initialize a virtual environment for development without docker as well as for setting up the docker container:

To reduce the size of the container remove the pip cache after installation:

FROM python:3.6.7-alpine3.8

...

RUN pip3.6 install -U pip setuptools \
    && pip3.6 install -r requirements.txt \
    && pip3.6 install gunicorn \. # or uwsgi or whatever
    && rm -rf /root/.cache
Sign up to request clarification or add additional context in comments.

3 Comments

I also found pipenv has a better dependency management than using requirements.txt. So without using virtualenv how to use pipenv so that i can take advantage of the the pipenv
At the point when you create docker containers for deployment, the dependencies should be clear and fixed. You should use the same packages and versions as during development. Create a requirements.txt from your pipenv environment and this should be it.
@Santhosh you can generate a requirements file by running pipenv run pip freeze > requirements.txt.
1

Docker containers provide already isolated environment which is a similar goal to that of virtualenv. So, if it's only 1 application running in a Docker container, it is fine to use it without another layer that virtualenv would bring. Personally, I don't remember seeing a Django app used with virtualenv in a container.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.