How do I develop a Django project using Docker components?

Source: Internet
Author: User
Tags cloud hosting docker hub docker compose docker machine digital ocean droplet

Docker is an open-source application container engine that allows developers to package their applications and dependencies into a portable container, and then publish them to any popular Linux machine or virtualize them. Since its release in 2013, whether it's code activity on Github or Redhat's integrated support for Docker in RHEL6.5, even Google's Compute Engine supports Docker running on top of it. A fiery degree can be seen!

This article details how to use the Docker machine"system Configuration" and Docker compose"multi-container application assembly" to provide stack completion Postgres, combined with Redis and Django project development.

In the end, the stack will include a separate container for each of the following services:

    • A Web/django container.
    • An Nginx container.
    • A Postgres container.
    • A Redis container
    • A Data container

Local Settings

Using the docker"v1.6.1" version we will use the Docker compose"v1.2.0" to orchestrate a multi-container application that uses Docker machine"v0.2.0" to create a local and cloud Docker host.
Follow the instructions to install the Docker Compose and machine separately, and then test the installation results:

$ docker-machine--version docker-machine0.2.0 (8b9eaf2) $ docker-compose--version docker-compose1.2.0

Next, realpython/dockerizing-django clone a project or create a project yourself based on the following project structure:

├──docker-compose. Yml├──nginx│├──dockerfile│└──sites-enabled│└──django_project├──production. Yml└──web│├──dockerfile│├──docker_django││├──__init__. PY││├──apps│││├──__init__. PY│││└──todo│││├──__init__. PY│││├──admin. PY│││├──models. PY│││├──templates││││├──_base. html││││└──home. html│││├──tests. PY│││├──urls. PY│││└──views. PY││├──settings. PY││├──urls. PY│└──wsgi. PY│├──manage. PY│├──requirements. txt│└──static││└──main. CSS</code>

Now we are ready to run the container ...

Docker Machine

To open Docker machine, just run:

$ docker-machine create-d VirtualBox dev;info[0000] Creating CA:/users/michael/.docker/machine/certs/ca.peminfo[0000] Creating client Certificate:/users/michael/.docker/machine/certs/cert.peminfo[0001] Downloading Boot2docker.iso to/users/michael/.docker/machine/cache/boot2docker.iso ... info[0035] Creating SSH Key ... info[0035] Creating VirtualBox VM ... info[0043] Starting VirtualBox VM ... info[0044] Waiting forVm toStart ... info[0094]"Dev"has been created and  isNow theActive machine.info[0094] to point your Docker client at it,RunThisinchYour Shell:eval"$ (docker-machine env dev)"

This create command sets a new machine"development environment." In fact, it is downloading the boot2docker and starting to run the VM. Now just specify Docker in the development environment:

eval"$(docker-machine env dev)"

Run the following command to view the machine that is currently running:

ls NAME  ACTIVE  DRIVER  STATE  URL dev * virtualbox Running tcp://192.168.99.100:2376

Next, we'll let the django,postgres and Redis containers run together.

Docker Compose

Let's take a look at the Docker-compose.yml file:

Web:restart:always build:./web expose:- "8000" Links:- postgres:postgres- Redis:redisVolumes:-/usr/src/app/staticEnv_file:. env command:/usr/local/bin/gunicorn docker_django.wsgi:application-w 2-b: 8000 Nginx:restart:always Bui LD:./nginx/ports:- "80:80" Volumes:-/www/staticVolumes_from:-WebLinks:- Web:WebPostgres:restart:always image:postgres:latest Volumes_from:-DataPorts:- "5432:5432" Redis:restart:always image:redis:latest Ports:- "6379:6379" Data:restart:always image:postgres:latest Volumes:-/var/lib/postgresqlCommand:true

Here, we define five services: Web, Nginx, Postgres, Redis, and Data.

    • The Web service is built by Dockerfile in the "web" directory, where Python environment settings are set, and Django applies the default 8000 port. This port is then forwarded to port 80 on the host environment – for example, Docker machine. The WEB service also adds environment variables to the container restore.env file.
    • The Nginx service is used for the reverse proxy, which is used for Django or static file directories.
    • The Postgres service is installed from the official PostgreSQL mirror of the Docker Hub and runs on the default server's 5432 port after installing Postgres.
    • Redis is installed using the official Redis image, and the default Redis service is running on port 6379.
    • Finally, note that there is a separate container to store database data, which is data. This helps ensure that even if the Postgres container is completely destroyed the data still exists.

Now, run the container, build the image, and start the service:

$ docker-compose build $ docker-compose-d

You can have time for a cup of coffee or walk, because it will take a while for you to run it for the first time, and then you can build faster from the Docker cache.

Once the service is running, we need to create a database migration:

run web /usr/local/bin/python manage.py migrate

Obtain the relevant Ip,–docker-machine ip– for the Docker machine and enter the IP in your browser:

After the refresh occurs, you should be able to see the page update. Essentially, we use Redis INCR to increment each processing request and view the web/docker_django/apps/todo/views.py code for more information.

Again, this creates five services, all running in different containers:

$ docker-composePS Name Command State Ports----------------------------------------------------------------------------------------------Dockerizingdjango_data_1/docker-entrypoint.ShtrueUp5432/tcpdockerizingdjango_nginx_1/usr/sbin/nginx up0.0. 0. 0: the - the/tcpdockerizingdjango_postgres_1/docker-entrypoint.SH postgres up0.0. 0. 0:5432 -5432/tcpdockerizingdjango_redis_1/entrypoint.SH Redis-serverUp0.0. 0. 0:6379 -6379/tcpdockerizingdjango_web_1/usr/Local/bin/gunicorn Do ...Up8000/tcp

To see which environment variables are available for WEB services, run:

$ docker-compose run web env

To view the log, run:

$ docker-compose logs

You can also enter the Postgres Shell-because we have set up the Docker-compose.yml file in the database by adding the user/role, the port is forwarded to the host environment:

-h192.168.99.100-p5432-U--password

Ready to deploy? Stop running and docker-compose stop let our app run in the cloud!

Deployment

As we run our applications locally, we can now push to a cloud hosting service provider that is exactly the same as the Docker machine environment. Now let's deploy to Digital Ocean.

After you register for Digital Ocean, generate a personal access token "personal access token", and then run the following command:

$ docker-machine create \ -d digitalocean \ --digitalocean-access-token=ADD_YOUR_TOKEN_HERE 

This will take a few minutes to provide droplet and set up a new Docker machine product environment:

INFO[0000] Creating SSH key... INFO[0001] Creating Digital Ocean droplet... INFO[0133"production"andisthe active machine. INFO[0133atitrunin"$(docker-machine env production)"

Now we have two machines running, one on the local, one in Digital Ocean:

lsNAME         ACTIVE   DRIVER         STATE     URLdev          *        virtualbox     Running   tcp://192.168.99.100:2376production            digitalocean   Running   tcp://104.131.107.8:2376

Set production to activate the machine and load the Docker environment to the shell:

eval"$(docker-machine env production)"

Finally, let's build the Django application again on the cloud. At this point we need to use a slightly different Docker Compose file, which does not need to be installed in the container. Why is it? Because the container itself is well suited for local development, we can update the local code of the "web" directory and change the code to affect the container immediately. In production, it is clear that this is not necessary.

$ docker-compose build $ docker-compose-d-f production.yml $ docker-compose run web /usr/local/bin/python manage.py migrate

Get the IP address associated with your Digital Ocean account and view it in the browser. If everything goes well, you should be able to see that your application is running.

Original address: Django Development with Docker Compose and machine

This article is compiled and collated by OneAPM engineers. OneAPM is the emerging leader in China's basic software industry, helping enterprise users and developers easily implement slow program code and real-time crawling of SQL statements. To read more technical articles, please visit the OneAPM official blog.

How do I develop a Django project using Docker components?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.