He that will do his work well must first sharpen his tools
Before starting web development, you need to set up a basic development environment. Personal use of nginx, Redis, mysql, Node.js.
Different ways to build environments
- Install apt(Ubuntu) and Brew (MAC OS) one by one
- Script: LNMP one-click installation package
- Source code compilation The above solutions all have a common shortcoming
- Once the system is reinstalled, it needs to be reinstalled and configured (with multiple computers, the development environment version is prone to be inconsistent).
- Without version control system, software configuration and maintenance is troublesome
A better solution — Docker
Build nginx, Redis, mysql services based on Docker (above 18.03).
The project structure
. ├ ─ ─ the envThe environment variable is dev by defaultHeavy Metal Exercises ─.Heavy Metal Exercises ─ Heavy metal ExercisesConfiguration files for different containers│ ├ ─ ─ mysql │ │ └ ─ ─ docker - compose. Yml │ ├ ─ ─ nginx │ │ ├ ─ ─ the conf │ │ ├ ─ ─ docker - compose. Prod. Yml │ │ └ ─ ─ ├ ─ sci-imp., sci-imp., sci-imp# proD environment variable└ ─ ─ the envCopy the code
Docker-compose uses the. Env file in the current directory for runtime, and does not support specifying the env file. Therefore, if multiple environments are required, you can only create the
The project content
Use the. Env file to configure the environment variables required for the entire project
# file .env
# Project name
COMPOSE_PROJECT_NAME=site
# compose documents
COMPOSE_FILE=container/nginx/docker-compose.yml:container/mysql/docker-compose.yml:container/redis/docker-compose.yml
# mysql config
MYSQL_ROOT_PASSWORD=123456
MYSQL_DATABASE=demo
# redis config
REDIS_PASSWORD=123456
# Customize environment variable local server IP
SITE_IP=host.docker.internal # host.docker.internal requires version 18.03 or later
Copy the code
** Take nginx’s docker-comemage. yml file as an example: **${SITE_IP} will not be replaced
version: "3"
services:
nginx:
image: nginx
volumes:
- ./conf/dev.template:/etc/nginx/conf.d/dev.template
ports:
- "80:80"
environment:
- SITE_IP=${SITE_IP}
command: /bin/bash -c "envsubst '$${SITE_IP}'< /etc/nginx/conf.d/dev.template > /etc/nginx/conf.d/dev.conf && exec nginx -g 'daemon off; '"
networks:
- default
- network_site
networks:
network_site:
driver: bridge
Copy the code
Other image configuration can be viewed from DockerHub redis, mysql
Start all
// dev docker-compose up // prod mode, which uses the. Env file under prodcd ./prod && docker-compose up
Copy the code
Separate start
docker-compose up nginx
docker-compose up mysql
docker-compose up redis
Copy the code
stop
# Stop a service
docker-compose stop nginx
# Stop all
docker-compose stop
Copy the code
With Docker, you can quickly install environments on multiple computers with only a repository of configuration.