How to Use Docker components to develop a Django project?
The following is the translation
Docker is an open-source application container engine that allows developers to package their applications and dependencies to a portable container and then publish them to any popular Linux machine, you can also achieve virtualization. Since its release in 2013, Docker has been integrated in both code activity on Github and RedHat in RHEL6.5. Even Google's Compute Engine supports Docker running on it. This is evident in the heat!
This article details how to use Docker Machine "System Configuration" and Docker Compose "Multi-container application assembly" to provide a stack for Postgres, Redis, and Django project development.
In the end, the stack will include the following separate containers for each service:
- A Web/Django container
- A Nginx container
- One ipvs container
- A Redis container
- A Data container
With Docker v1.6.1, we will use Docker Compose v1.2.0 to orchestrate an application composed of multiple containers and use Docker Machine v0.2.0 to create local and cloud Docker hosts. Install Docker Compose and Machine respectively as instructed, and test the installation result:
$ docker-machine --version docker-machine version 0.2.0 (8b9eaf2) $ docker-compose --version docker-compose 1.2.0
Next, according to the following project structure
realpython/dockerizing-djangoClone a project or create a project by yourself:
├── 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 prepare the container to run ......
To enable Docker Machine, run:
$ docker-machine create -d virtualbox dev;INFO Creating CA: /Users/michael/.docker/machine/certs/ca.pemINFO Creating client certificate: /Users/michael/.docker/machine/certs/cert.pemINFO Downloading boot2docker.iso to /Users/michael/.docker/machine/cache/boot2docker.iso...INFO Creating SSH key...INFO Creating VirtualBox VM...INFO Starting VirtualBox VM...INFO Waiting for VM to start...INFO "dev" has been created and is now the active machine.INFO To point your Docker client at it, run this in your shell: eval "$(docker-machine env dev)"
This create Command sets up a new Machine "development environment 」. In fact, it downloads Boot2Docker and starts to run VM. Now you only need to specify Docker in the development environment:
$ eval "$(docker-machine env dev)"
Run the following command to view the currently running machine:
$ docker-machine ls NAME ACTIVE DRIVER STATE URL dev * virtualbox Running tcp://192.168.99.100:2376
Next, we will run Django, ipvs, and Redis containers.
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, ipvs, Redis, and Data.
- The Web Service is built by using the Dockerfile in the "Web" directory. The Python environment settings are also set here. The default port of the Django application is port 8000. This port is then forwarded to port 80 in 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 reverse proxy and acts on Django or static file directories.
- The Postgres service is installed from the official PostgreSQL image of Docker Hub. After installing Postgres, it runs on port 5432 of the default server.
- Redis is installed using the official Redis image. By default, the Redis service runs on port 6379.
- Finally, note that there is a separate container to store database Data, that is, Data. This helps ensure that even if the ipvs container completely destroys data, it still exists.
Now, run the container, build the image, and start the service:
$ docker-compose build $ docker-compose up -d
At this time, you can have time to have a cup of coffee or walk, because it will take some time for you to run it for the first time, and then you can build and run faster from the Docker cache.
Once the service runs, we need to create a database for Migration:
$ docker-compose run web /usr/local/bin/python manage.py migrate
Obtain the relevant IP address of the Docker Machine,-docker-machine ip-, and then enter the IP address in your browser:
Refresh the page when it appears. You should be able to see page updates. In essence, we use Redis INCR to increment each processing request and view
web/docker_django/apps/todo/views.pyCode to get more information.
Similarly, these five services are all run in different containers:
$ docker-compose psName Command State Ports----------------------------------------------------------------------------------------------dockerizingdjango_data_1 /docker-entrypoint.sh true Up 5432/tcpdockerizingdjango_nginx_1 /usr/sbin/nginx Up 0.0.0.0:80->80/tcpdockerizingdjango_postgres_1 /docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcpdockerizingdjango_redis_1 /entrypoint.sh redis-server Up 0.0.0.0:6379->6379/tcpdockerizingdjango_web_1 /usr/local/bin/gunicorn do ... Up 8000/tcp
To view which environment variables can be used for Web Services, run:
$ docker-compose run web env
To view the log, run:
$ docker-compose logs
You can also go to ipvs Shell--because we have already set up the port in the host environment by adding a user/role to the database via the docker-compose.yml file:
$ psql -h 192.168.99.100 -p 5432 -U postgres --password
Prepare for deployment? Stop running first
docker-compose stopAnd then let our applications run in the cloud!
Like running 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 it in Digital Ocean.
After you register Digital Ocean, generate a Personal Access Token "Personal Access Token" and run the following command:
$ docker-machine create \ -d digitalocean \ --digitalocean-access-token=ADD_YOUR_TOKEN_HERE \Production
This will take several minutes to provide droplet and set up a new Docker Machine product environment:
INFO Creating SSH key... INFO Creating Digital Ocean droplet... INFO "production" has been created and is now the active machine. INFO To point your Docker client at it, run this in your shell: eval "$(docker-machine env production)"
Now we have two machines running. One is local and the other is Digital Ocean:
$ docker-machine lsNAME ACTIVE DRIVER STATE URLdev * virtualbox Running tcp://192.168.99.100:2376production digitalocean Running tcp://22.214.171.124:2376
Set production to activate the machine and load the Docker environment to shell:
$ docker-machine active production $ eval "$(docker-machine env production)"
Finally, let's build a new Django application on the cloud. At this time, we need to use a slightly different Docker Compose file, which does not need to be installed in the container. Why? Because the container itself is very suitable for local development, we can update the local code of the "Web" directory and change the code to immediately affect the container. This is obviously unnecessary in production.
$ docker-compose build $ docker-compose up -d -f production.yml $ docker-compose run web /usr/local/bin/python manage.py migrate
Obtain the IP address associated with the Digital Ocean account and view it in the browser. If everything goes well, you should be able to see that your application is running.
Install Nginx + uWSGI + Django on Ubuntu Server 12.04
Deployment of Django + Nginx + uWSGI
Build a Django Python MySQL Linux development environment
Django details: click here
Django's: click here
Original article: Django Development With Docker Compose and Machine
This article is compiled by OneAPM engineers. For more technical articles, visit the official OneAPM blog.
This article permanently updates the link address: