docker deployment Django project (front and back end separation project)

Keywords: Docker MySQL Nginx Python

First step

Switch yum source: https://blog.csdn.net/wangshuminjava/article/details/84380308

The second step

1. Install docker

  • https://www.runoob.com/docker/centos-docker-install.html
  • Modify the docker image source: https://blog.csdn.net/l848168/article/details/83378298

2. Install docker compose

  • http://get.daocloud.io/

The third step

1. Put the project on the server

For example, create a wwwroot folder under the home directory and put the project in it

2. Create the Dockerfile file in the root directory of the project

# Pull Linux environment with python 3.7 from the warehouse
FROM python:3.7

# Setting python environment variables
ENV PYTHONUNBUFFERED 1

# Add these two lines
RUN apt-get update
RUN apt-get install python3-dev default-libmysqlclient-dev -y

# Create a code folder and make it a working directory
RUN mkdir /code
WORKDIR /code
# Update pip
RUN pip install pip -U -i https://mirrors.aliyun.com/pypi/simple/
# Copy requirements.txt to the code directory of the container
ADD requirements.txt /code/
# Install the library and use alisource to install it
RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
# Copy the current directory to the container's code directory
ADD . /code/
FROM python:3.7 Instruction pull a Linux operating system environment containing python 3.7 from the warehouse
RUN and WORKDIR The instructions are all for the container. The function is to create a directory in the docker container and set it as a working directory. Note that the host does not have this directory
ADD requirements.txt /code/ This means that the requirements.txt file of the current directory of the host (that is, the directory where the Dockerfile is located) is copied to the / code directory of the container
ADD . /code/ Copy all contents of the current directory to the container / code / directory, and pay attention to the middle point

3. Create the docker-compose.yml file in the root directory of the project

version: "3" 
services:
  app:
    restart: always
    build: .  # 'click' for current directory
    command: "python3 manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
  db:
    image: mysql:5.7
    volumes:
      - "./mysql:/var/lib/mysql"
    ports:
      - "3306:3306"
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=mypassword
      - MYSQL_DATABASE=django_app
version Represents the version of docker-compose.yml. The latest version is 3
restart In addition to normal operation, the container will restart at any time, such as encountering bug, process crash, docker restart, etc
build Specify a path containing the Dockerfile through which to build the container image. Note that "." represents the current directory
command The command that the container needs to execute at run time. This is the familiar running development server
volumes Volume, this is a very important concept. Mapping of host and container directories.
ports Defines the port mapping between the host and the container; the host port maps to the port of this container
depends_on This means that this container needs to wait for the db container to start
image Pull MySQL 5.7 image from the warehouse. You can make your own version
environment Define the environment variables of the container, and set the password of the root user of MySQL and the name of the database.

4. Modify the database configuration in Django setting.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django_app',
        'USER': 'root',
        'PASSWORD': 'mypassword',
        'HOST': 'db', #Note that HOST fills in the name of the docker container, db 
        'PORT': '3306',
        'OPTIONS': {'charset': 'utf8'},
    }
}

Note: please confirm that no other program occupies port 3306, such as MySQL installed on the host.

5. Run docker

docker-compose up

Step 4 install nginx (Note: front and rear end separation project)

1. Install nginx

yum install nginx

2. Enter the directory of nginx

cd /etc/nginx/conf.d

3. Edit your front-end project configuration

vi your_name.conf

Your name.conf is the name you choose freely, as long as it is xxx.conf

server {
    listen 80;
    server_name localhost; #If there is a domain name, it is localhost

    location / {
        root /wwwroot/h5; #Put the front-end project directory address here
        index index.html;
    }
}

Save to exit editing;

Execute: nginx -s reload restart nginx

56 original articles published, 48 praised, 130000 visitors+
Private letter follow

Posted by realchamp on Sun, 15 Mar 2020 00:39:41 -0700