Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Docker to build Django,Nginx,R,Python deployment Environment

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use Docker to build a Django,Nginx,R,Python deployment environment, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Basic environment:

Ubuntu 16.10

Docker 17.06.0-ce

Compress your own project files

1. It should be noted here that when compressing, you also need to write your own Python package in requirement.txt, so that when you build the environment, you will know what package you need to install together. The requirement.txt content of my project is as follows.

Django==1.10.5rpy2==2.8.5PyMySQL==0.7.9numpy==1.13.0pandas==0.20.2scikit-learn==0.18.1scipy==0.19.0uwsgi

two。 Change your settings.py to set up the deployment environment for the Nginx server agent

The changes are as follows

STATIC_ROOT = os.path.join (BASE_DIR, 'collect_static') ALLOWED_HOST = [' *']

Note: the deployed environment cannot be the same as the static file of the development environment, otherwise it will cause an error.

After modification, run the following command:

Python manage.py collectstatic

At this time, you can compress the file with peace of mind.

Tar czvf FileName.tar DirName

Build web environment

1. Put the packaged project files in the web folder

Cp / home/haoyu/AAPlatform.tar.

two。 Write Dockerfile

Here is mainly to build the basic environment.

# basic image FROM ubuntu:16.10FROM daocloud.io/python:3.5# maintainer information MAINTAINER haoyu ADD AAPlatform.tar / usr/src/ # app directory WORKDIR / usr/src/AAPlatformENV LD_LIBRARY_PATH=/usr/local/libRUN apt-key adv-- keyserver keyserver.ubuntu.com-- recv-keys 40976EAF437D05B5RUN apt-key adv-- keyserver keyserver.ubuntu.com-- recv-keys 3B4FE6ACC0B21F32RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety Main restricted "> / etc/apt/sources.listRUN echo" deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-updates main restricted "> > / etc/apt/sources.listRUN echo" deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety universe "> > / etc/apt/sources.listRUN echo" deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-updates universe "> > / etc/apt/sources.listRUN echo" deb http://mirrors.tuna .tsinghua.edu.cn / ubuntu/ yakkety multiverse "> / etc/apt/sources.listRUN echo" deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-updates multiverse "> / etc/apt/sources.listRUN echo" deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-backports main restricted universe multiverse "> > / etc/apt/sources.listRUN echo" deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-security main restricted "> > / etc/ Apt/sources.listRUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-security universe" > > / etc/apt/sources.listRUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-security multiverse" > > / etc/apt/sources.listRUN apt-get update RUN apt-get install-y make RUN echo "deb http://cran.rstudio.com/bin/linux/ubuntu yakkety/" > > / etc/apt/sources.listRUN apt-key adv-- Keyserver keyserver.ubuntu.com-- recv-keys 51716619E084DAB9RUN apt-get update RUN apt-get install-y r-base # r-base-dev r-cran-mgcv r-base-core r-base-html r-recommended# depends on RUN pip install-- no-cache-dir-r requirements.txt-I http://mirrors.aliyun.com/pypi/simple/-- trusted-host mirrors.aliyun.com to install app

Basically, the environment built by Django and R is here.

two。 Write start.sh

Here is to run the Django of docker

The contents are as follows:

#! / bin/bash # docker build-t aaplatform/django-app .docker run-- name django\-v / usr/src/AAPlatform\-v / usr/src/AAPlatform/collect_static\-p 12000 aaplatform/django-app 8000\-d feiyu/django-app / usr/local/bin/uwsgi-- http: 8000-- chdir / usr/src/AAPlatform-w AAPlatform.wsgi#-d aaplatform/django-app / usr/local/bin/gunicorn myblog.wsgi:application-w 1-b: 8000

Build Nginx environment

1. Write Dockerfile files

Configure the Nginx-related environment

FROM daocloud.io/nginxMAINTAINER haoyu RUN rm / etc/nginx/conf.d/default.confADD nginx-conf/ / etc/nginx/conf.d/

two。 Write Server configuration file

The default configuration file of the system needs to be changed

In nginx-conf:

Server {listen 80; server_name localhost; charset utf-8; root / usr/src/AAPlatform; access_log / var/log/nginx/django.log; location ^ ~ / static {alias / usr/src/AAPlatform/collect_static;} location / {proxy_pass http://web:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for }}

Ensure that the Nginx environment can be built.

3. Write start.sh

Get docker up and running

#! / bin/bash # docker build-t nginx .docker run-- name nginx-server\-- link django:web\-v / www/collect_static\-- volumes-from django\-p 8888 run 80\-d nginx

The combination of the whole environment

1. Build docker-start.sh

Start web and Nginx

#! / bin/bash#cd. / webecho "start web -". / start.shcd.. / nginxecho "start nginx-". / start.sh

two。 Write stop.sh

Close docker

# / bin/bash#sudo docker kill $(docker ps-a-Q) sudo docker rm $(docker ps-a-Q)

Run the test

1. Run the sh file

$. / docker-start.sh

two。 Visit the website

Http://127.0.0.1:8888/login

Note: you are accessing the Nginx server.

Export the deployed image file

1.docker save

Docker save 00273588addb-o aaplatform.tar aaplatform/django-app:latest

2.docker load

Docker load < aaplatform.tar Thank you for reading this article carefully. I hope the article "how to use Docker to build a Django,Nginx,R,Python deployment environment" shared by the editor will be helpful to you. At the same time, I also hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report