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-machine version 0.2.0 (8b9eaf2)  $ docker-compose --version docker-compose 1.2.0  

Next, clone a project from Realpython/dockerizing-django 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││&nbs P; │  ├──__init__.py││  │  ├──admin.py││  │  ├──mo Dels.py││  │  ├──templates││  │  │  ├──_base.html││&n bsp; │  │  └──home.html││  │  ├──tests.py││  │  ;  ├──urls.py││  │  └──views.py││  ├──settings.py││  ├──u Rls.py│  └──wsgi.py│├──manage.py│├──requirements.txt│└──static││└──main.css</code&g t;  

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.pem  INFO[0000] Creating client certificate: /Users/michael/.docker/machine/certs/cert.pem  INFO[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 for VM to start...  INFO[0094] "dev" has been created and is now the active machine.  INFO[0094] To point your Docker client at it, run this in your 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:

$ docker-machine 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:redis   volumes:     - /usr/src/app/static  env_file: .env  command: /usr/local/bin/gunicorn docker_django.wsgi:application -w 2 -b :8000 nginx:    restart: always  build: ./nginx/  ports:     - "80:80"   volumes:     - /www/static   volumes_from:     - web  links:     - web:web postgres:    restart: always  image: postgres:latest   volumes_from:     - data  ports:     - "5432:5432" redis:    restart: always  image: redis:latest   ports:     - "6379:6379" data:    restart: always   image: postgres:latest   volumes:     - /var/lib/postgresql  command: 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 up -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:

$ docker-compose 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-compose ps            Name                          Command               State           Ports----------------------------------------------------------------------------------------------dockerizingdjango_data_1       /docker-entrypoint.sh true       Up      5432/tcp  dockerizingdjango_nginx_1      /usr/sbin/nginx                  Up      0.0.0.0:80->80/tcp  dockerizingdjango_postgres_1   /docker-entrypoint.sh postgres   Up      0.0.0.0:5432->5432/tcp  dockerizingdjango_redis_1      /entrypoint.sh redis-server      Up      0.0.0.0:6379->6379/tcp  dockerizingdjango_web_1        /usr/local/bin/gunicorn do ...   Up      8000/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:

$ psql -h 192.168.99.100 -p 5432 -U postgres --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 Production  

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" has been created and is now the active machine.  INFO[0133] To point your Docker client at it, run this in your shell: eval "$(docker-machine env production)"  

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

$ docker-machine lsNAME         ACTIVE   DRIVER         STATE     URL  dev          *        virtualbox     Running   tcp://192.168.99.100:2376  production            digitalocean   Running   tcp://104.131.107.8:2376  

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

$ docker-machine active production $ 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 up -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 an emerging leader in application performance management, enabling enterprise users and developers to easily implement slow program code and real-time crawling of SQL statements. To read more technical articles, please visit the OneAPM official blog.

This article transferred from: http://news.oneapm.com/docker-django/

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.