In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use Docker components to develop Django projects", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use Docker components to develop Django projects" this article.
Docker is an open source application container engine that allows developers to package their applications and dependency packages into a portable container, publish them to any popular Linux machine, and virtualize them. Since its release in 2013, even Google's Compute Engine has supported Docker to run on Github, whether from code activity on Github or Docker support integrated by Redhat in RHEL6.5. You can see how hot it is!
This article describes in detail how to use Docker Machine "system configuration" and Docker Compose "multi-container application assembly" to provide stack to complete the development of Postgres, Redis and Django projects.
In * *, the stack will include a separate container for each service as follows:
A container for Web/ Django
A container for Nginx
A container for Postgres
A container for Redis
A Data container
Local Settin
Using Docker "v1.6.1" version we will use Docker Compose "v1.2.0" to orchestrate a multi-container application and use Docker Machine "v0.2.0" to create local and cloud Docker hosts. Follow the instructions to install Docker Compose and Machine respectively, 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 your own project according to 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
Now we are ready for the container to run.
Docker Machine
To turn on Docker Machine, simply 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 up a new Machine "development environment". In fact, it downloads Boot2Docker and starts running VM. Now just specify Docker in the development environment:
$eval "$(docker-machine env dev)"
Run the following command to view the machines that are currently running:
$docker-machine ls NAME ACTIVE DRIVER STATE URL dev * virtualbox Running tcp://192.168.99.100:2376
Next, we will get the Django,Postgres and Redis containers up and running.
Docker Compose
Let's 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 redis:latest ports 5432 "redis: restart: always image: redis:latest ports: -" 6379 redis:latest ports "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 through Dockerfile under the "Web" directory, where the Python environment setting is also set, and the Django application defaults to 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 proxying, acting on Django or static file directories.
The Postgres service is installed from the official PostgreSQL image of Docker Hub and runs on port 5432 of the default server after installing Postgres.
Redis is installed using the official Redis image, and the default Redis service runs on port 6379.
*. Note that there is a separate container for storing database data, that is, Data. This helps ensure that the data exists even if the Postgres container completely destroys it.
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 a walk, because it will take a while for you to run it many times, and then you can build a faster one 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
Get the relevant IP of Docker Machine,-docker-machine ip -, and type IP in your browser:
Refresh after the above image appears, you should be able to see the page update. In essence, we use Redis INCR to increment each processing request and look at 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/tcp dockerizingdjango_postgres_1 80-> 80/tcp dockerizingdjango_postgres_1 / docker-entrypoint.sh postgres Up 0.0.0.0 80/tcp dockerizingdjango_postgres_1 5432-> 5432/tcp dockerizingdjango_redis_1 / entrypoint.sh redis-server Up 0.0.0.0 usr/local/bin/gunicorn do 6379-> 6379/tcp dockerizingdjango_web_1 / usr/local/bin/gunicorn do. Up 8000/tcp
To see which environment variables are available for the Web service, run:
$docker-compose run web env
To view the log, run:
$docker-compose logs
You can also enter Postgres Shell-because we have set it in the database through the docker-compose.yml file by adding users / roles, and the port is forwarded to the host environment:
$psql-h 192.168.99.100-p 5432-U postgres-- password
Ready to deploy? Stop running docker-compose stop first and then let our application run in the cloud!
Deployment
Just as we run applications locally, we can now push to a cloud managed 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 the 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 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 locally and one on Digital Ocean:
$docker-machine ls NAME 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 shell:
$docker-machine active production $eval "$(docker-machine env production)"
*, let's build the Django application again on the cloud. At this point we need to use a slightly different Docker Compose file that does not need to be installed in a container. Why? Because the container itself is well suited for local development, we can update the local code in the "Web" directory and change the code to have an immediate impact on the container. In production, it is obvious 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 the Digital Ocean account and view it in the browser. If all goes well, you should be able to see your application running.
These are all the contents of the article "how to use Docker components to develop Django projects". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.