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-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to use Docker to build a Django,Nginx,R,Python deployment environment. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

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.

?

one

two

three

four

five

six

seven

eight

Django==1.10.5

Rpy2==2.8.5

PyMySQL==0.7.9

Numpy==1.13.0

Pandas==0.20.2

Scikit-learn==0.18.1

Scipy==0.19.0

Uwsgi

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

The changes are as follows

?

one

two

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:

one

Python manage.py collectstatic

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

one

Tar czvf FileName.tar DirName

Build web environment

1. Put the packaged project files in the web folder

one

Cp / home/haoyu/AAPlatform.tar.

two。 Write Dockerfile

Here is mainly to build the basic environment.

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

twenty-five

twenty-six

twenty-seven

twenty-eight

twenty-nine

thirty

thirty-one

thirty-two

thirty-three

thirty-four

thirty-five

thirty-six

thirty-seven

thirty-eight

thirty-nine

forty

forty-one

forty-two

forty-three

forty-four

forty-five

forty-six

forty-seven

forty-eight

forty-nine

fifty

fifty-one

fifty-two

# basic image

FROM ubuntu:16.10

FROM daocloud.io/python:3.5

# maintainer Information

MAINTAINER haoyu

ADD AAPlatform.tar / usr/src/

# directory where app is located

WORKDIR / usr/src/AAPlatform

ENV LD_LIBRARY_PATH=/usr/local/lib

RUN apt-key adv-keyserver keyserver.ubuntu.com-recv-keys 40976EAF437D05B5

RUN apt-key adv-keyserver keyserver.ubuntu.com-recv-keys 3B4FE6ACC0B21F32

RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety main restricted" > / etc/apt/sources.list

RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-updates main restricted" > > / etc/apt/sources.list

RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety universe" > > / etc/apt/sources.list

RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-updates universe" > > / etc/apt/sources.list

RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety multiverse" > > / etc/apt/sources.list

RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-updates multiverse" > > / etc/apt/sources.list

RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-backports main restricted universe multiverse" > > / etc/apt/sources.list

RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-security main restricted" > > / etc/apt/sources.list

RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-security universe" > > / etc/apt/sources.list

RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-security multiverse" > > / etc/apt/sources.list

RUN apt-get update

RUN apt-get install-y make

RUN echo "deb http://cran.rstudio.com/bin/linux/ubuntu yakkety/" > > / etc/apt/sources.list

RUN apt-key adv-keyserver keyserver.ubuntu.com-recv-keys 51716619E084DAB9

RUN apt-get update

RUN apt-get install-y r-base # r-base-dev r-cran-mgcv r-base-core r-base-html r-recommended

# dependencies required to install app

RUN pip install-- no-cache-dir-r requirements.txt-I http://mirrors.aliyun.com/pypi/simple/-- trusted-host mirrors.aliyun.com

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:

one

two

three

four

five

six

seven

eight

nine

ten

#! / bin/bash

#

Docker build-t aaplatform/django-app.

Docker run-- name django\

-v / usr/src/AAPlatform\

-v / usr/src/AAPlatform/collect_static\

-p 12000VOG 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

?

one

two

three

four

five

six

FROM daocloud.io/nginx

MAINTAINER haoyu

RUN rm / etc/nginx/conf.d/default.conf

ADD 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:

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

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

one

two

three

four

five

six

seven

eight

nine

#! / bin/bash

#

Docker build-t nginx.

Docker run-- name nginx-server\

-- link django:web\

-v / www/collect_static\

-- volumes-from django\

-p 8888purl 80\

-d nginx

The combination of the whole environment

1. Build docker-start.sh

Start web and Nginx

?

one

two

three

four

five

six

seven

eight

nine

#! / bin/bash

#

Cd. / web

Echo "start web -"

. / start.sh

Cd.. / nginx

Echo "start nginx-"

. / start.sh

two。 Write stop.sh

Close docker

one

two

three

four

five

# / bin/bash

#

Sudo docker kill $(docker ps-a-Q)

Sudo docker rm $(docker ps-a-Q)

Run the test

1. Run the sh file

one

$. / 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

one

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

2.docker load

one

Docker load < aaplatform.tar

This is the end of the article on "how to use Docker to build a Django,Nginx,R,Python deployment environment". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Original link: http://blog.csdn.net/The_One_is_all/article/details/76063968

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