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.
17.12.1-ce
for this article.
- 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.
- In the previous terminal where container is running, you can see the request hits.
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
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.
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.
docker-compose
to run multiple container and how to use MySQL database in your dockerized Django application.Featured image source: towardsdatascience.com