Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
docker virtual-env   0   15287
Using Docker instead of Virtual Environment for Django app development

Docker have all the good featured of virtual machine. It helps developer to set up an environment on development machine which is similar to production environment.

Please go through official docker site if you want to know more about Docker.

In this article we will see how to develop a hello world Django project and will run it docker container instead of virtual environment.



Installing Docker:

Please follow this guide to install docker on your machine. We are using Docker version 17.12.1-ce for this article.

Starting docker container of application:

- Create your Django project. You may start with hello world Django project Or you can clone this github repo.

- Add requirements.txt file and add django==1.10.0 in it.

- Create a Dockerfile and add below text in it.

Each step is explained using comment above it.

# use python version 3 docker image as base image
FROM python:3

# cd to directory /usr/src/app. this will be our working directory
WORKDIR /usr/src/app

# copy the content of current directory in to the pwd of docker image.
# pwd in image is /usr/src/app
COPY . .

# now run the command to install dependencies from requirements file
RUN pip install --no-cache-dir -r requirements.txt

# expose the port 8800
EXPOSE 8800

# everytime container is started, run this command
CMD python manage.py runserver 0.0.0.0:8800


- Now lets create a docker image of our project. Run below command.

docker build -t image_name .


- Run a container using this newly created image.

docker container run --rm -p 8800:8800 --name django-sample image_name


- Now open another terminal and run the below command to see the running containers.

docker container ls -a


output will be something like this:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
2db47136db22        image_name          "/bin/sh -c 'python …"   59 seconds ago      Up 57 seconds       0.0.0.0:8800->8800/tcp   django-sample


- Now go to localhost:8800/helloworld/  in your browser and you can see the template being rendered.

using docker instead of virtual environment for django app development


- In the previous terminal where container is running, you can see the request hits.

using docker instead of virtual environment for django app development  

Now if you change something in your code and refresh the browser, you won't see the changes. To see the changes as soon as you update the code (just like running python server on host machine instead of docker container), we need to mount our current directory when running the container.

Stop the running container.

docker stop [container-name or container-id]


As soon as container is stopped, it is also removed. This is because of --rm flag used in docker run command, which tells docker to remove the container as soon as it is stopped.

Now re-run the container with below command where we are mounting the pwd on host machine to /usr/src/app directory of docker container.

sudo docker container run -v "$(pwd)":/usr/src/app  --rm -p 8800:8800 --name django-sample image_name

You can provide absolute path as well instead of "$(pwd)" . Now if you change your response text in views.py  file, it will be reflected immediately in the browser. You can see in the terminal output of docker container that python server is reloaded as you change and save some file.

using docker instead of virtual environment for django app development  

Now you can develop your application without worrying about virtual environments. Everytime you are going to develop an Django application, follow the above steps to start the application in container.


Summary of steps:

- Create project.
- Create Docker file.
- Create Image.
- Run container by mounting the pwd.


Code:

Complete code is available on Github.


Docker or Virtualenv or Both?
Whether one should completely replace virtual environment with Docker or not or use both, is debatable? Best approach would be to try both yourself.
Virtual Environment - A pocket guide.

Few good articles comparing virtual environment and docker:
Python: If you have Docker, do you need virtualenv?
Docker and Virtualenv? A clean way to locally install python dependencies with pip in Docker
Does virtualenv serve a purpose (in production) when using docker?
VirtualEnv lives.
 
Next Step:
In the next part of this article we will see how to use docker-compose to run multiple container and how to use MySQL database in your dockerized Django application.


Featured image source: towardsdatascience.com

docker virtual-env   0   15287
0 comments on 'Using Docker Instead Of Virtual Environment For Django App Development'
Login to comment


Related Articles:
Comparing celery-rabbitmq docker cluster, multi-threading and scrapy framework for 1000 requests
comparing the performance of docker cluster, multi-threaded approach and scrapy framework to send 1000 requests and processing response....
Scraping 10000 tweets in 60 seconds using celery, RabbitMQ and Docker cluster with rotating proxy
Scraping large amount of tweets within minutes using celery and python, RabbitMQ and docker cluster with Python, Scraping huge data quickly using docker cluster with TOR, using rotating proxy in python, using celery rabbitmq and docker cluster in python to scrape data, Using TOR with Python...
Virtual Environment in Python - A Pocket Guide
A complete guide to start with virtual environments in python. How to install virtual environments. How to create a new virtual environment. Benefits of using virtual environment. docker vs virtual environment...
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap