With the development of virtualization technology, more and more Web projects use Docker for deployment operation and maintenance. We are trying to compose a Web project using Docker-compose with django-based back-end, vUE based front-end, postgresQL database and nginx reverse proxy.

Project preparation

Docker

  • Install the Docker
  • Install the docker – compose

django

  1. Created in python3.7django-admin startproject dockerdemo
  2. Modify settings.py
    • Modify theDEBUG=False
    • ALLOWED_HOSTS = [' 127.0.0.1 ', 'web']
    • Add the static file collection path toSTATIC_ROOT, the author set it to static
    • addSTATICFILES_DIRS, this configuration post DjangocollectstaticStatic files will be collected in this path toSTATIC_ROOTTo the path of.
    • In addition, the author uses whitenoise scheme to process static files, so it is necessary to configure the whitenoise middleware in the middleware.
      'django.middleware.security.SecurityMiddleware'.'whitenoise.middleware.WhiteNoiseMiddleware'.Copy the code
    • At the same time set upSTATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
    • Modify the database information (optional). You can also use Django’s default SQLite. Here I demonstrate a simple postgresQL configuration
    • DIRS under TEMPLATES is configured asos.path.join(BASE_DIR, 'web', 'dist')
  3. Configure views.py and urls.py to set the home page to vue’s index.html

vue

  1. A simple VUE project was created using VUE-CLI3
  2. configurationnpm run buildDist /static

nginx

Prepare the nginx configuration file for port forwarding.

Mirroring and choreography

Let’s start by identifying the steps needed to deploy a Web project. Here the author draws a flow chart.

Following the flow chart, we write Dockerfile and docker-comemage.yml

The database

The database needs to prepare the user and database used by the Web project first, which involves a point, is how to initialize the database. Postgresql is used as an example.

The official documentation provides two examples, one for configuring environment variables:

  • POSTGRES_DB
  • POSTGRES_USER
  • POSTGRES_PASSWORD

/docker-entrypoint-initdb.d/, the image will execute all scripts in the folder during initialization, we can create database and user in the script. Here is one approach to the script.

#! /bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
	CREATE USER $DATABASE_USER;
	CREATE DATABASE $DATABASE_NAME;
	GRANT ALL PRIVILEGES ON DATABASE $DATABASE_NAME TO $DATABASE_USER;
EOSQL
Copy the code

Note that initialization is done at the first build. Docker-compose will not be re-initialized if docker-compose is terminated or restarted after using the docker-compose stop command. The docker-compose down command is not initialized until the docker-compose down command has been executed.

web

Our Web project in a Dockerfile is divided into two parts: build and run. Use node:8 as the base image for the front-end build and Python :3.7 as the base image for the Django project to run, respectively. The interesting point is that a Dockerfile can be built in stages, that is, you can write multiple FROM and use as to give an alias, the following image can use this alias to get some content of the image, such as COPY — FROM, etc.

The front end

Front-end content is relatively simple, is to copy the code to the working directory, configure taobao mirror agent, and then execute NPM run build to build a front-end static file.

django

PIP installs dependencies and Gunicorn. Since Gunicorn cannot be installed if you develop in a Windows environment, we will install it in a separate Dockerfile. The image used is the image address provided by Tsinghua University. After the dependency installation is complete, follow the static file collection instructions.

nginx

Prepare a configuration file that listens on port 80 and forwards all incoming requests to the Django project. I prepared a relatively simple configuration file. Other requirements need to be set based on the scenario.

server { listen 80; server_name localhost; location / { proxy_pass http://web:8123; }}Copy the code

ignore

Write.dockerignore files that don’t need to be packaged, such as node_modules.

docker-compose.yml

This file is used to orchestrate a project, mainly the web, nginx and Postgres described above. There are other details.

  • Both Nginx and Postgres need to mount the data volume to a place where the corresponding configuration and data are stored
  • The database information is read using environment variables, so we prepare an. Env file and specify the environment variable file to read by env_file.
  • On the Web, we set the command to wait 8 seconds for the database to start. Data migration of the database followed, and finally gunicorn was used for boot.
  • Configure in the mirrordepends_onorlinksDepends_on also controls the startup sequence of the container.

The effect

Visit http://localhost/ or http://localhost/admin/ to see the final effect.

The code address

Github.com/will4906/do…

Refer to the link

  • Stackoverflow.com/questions/3…
  • Docs.docker.com/v17.09/get-…
  • www.cnblogs.com/yanshicheng…
  • Cloud.tencent.com/developer/a…