We all hate complex environment configuration, here is how to use docker one-click configuration environment.

Code first:

Example project, technology stack: Docker nginx Egg mysql React

Original link welcome star ~

Docker

Docker is a virtualization technology at the operating system level. The traditional virtual machine (VM) technology virtualizes a set of hardware. On the hardware, a virtual operating system runs application processes on the system. However, containers created by Docker do not have their own kernel, and there is no hardware virtualization, which greatly simplifies the cost of container creation and maintenance.

The core concepts of Docker include: Image containers. The relationship between them is similar to the relationship between classes and instances in programming. Images are static and can be created in advance. The container has its own running state, allowing access to the container, manipulation of the container, and the container runs in an isolated environment.

Those who are not familiar with Docker can take a look at Docker first — from getting started to practice

Docker Compose

Docker Compose is an official Docker project that defines and runs multiple Docker container applications. Quickly deploy distributed applications by writing docker-comemage. yml configuration file. File configurations include mirroring, port mapping, file mapping, and dependency between containers.

Please refer to the Docker Compose official documentation

Deploying front-end engineering

For projects where the front and back ends are separated, deploying the front end engineering is usually simple. Use Nginx to map the packed static resources.


# ./docker-compose.yml

  nginx:

    build:

      context: ./webapp

    ports:

      - "80:80"

      - "443:443"

    volumes:

      - ./nginx/conf.d:/etc/nginx/conf.d

      - ./nginx/cert:/etc/nginx/cert

      - ./nginx/nginx.conf:/etc/nginx/nginx.conf

    networks:

      - backend

    restart: always

Copy the code

Docker-comemage. yml is mainly configured

  • Build: -> context:./webapp Specifies that the image is generated by./webapp/Dockerfile

  • Ports -> “80:80” access port 80 of the localhost to access port 80 of the container

  • volumes: -> ./nginx/… You can modify the nginx configuration running in the container by mapping the local file to the container and directly modifying the local file


# ./webapp/DockerfileFROM node:12.10.0-alpine as client RUN mkdir /opt/webapp WORKDIR /opt/webapp COPY./ package.json. RUN NPM install --production --registry=https://registry.npm.taobao.org COPY . . RUN npm run build FROM nginx WORKDIR /usr/app/ COPY --from=client /opt/webapp/build/ /usr/share/nginx/html/Copy the code

The./ webApp /Dockerfile configuration is divided into two phases: the first phase relies on Node, mainly the installation project relies on NPM install and packages the NPM run build. The second phase relies on nginx and puts the static resources packaged in phase 1 into the nginx directory.

At this point, the front-end engineering is in place and can be accessed by running docker-compose up under localhost/.

Deploy the back-end project

EGG

Run the Egg project first


# ./docker-compose.yml

  fe_article_server:

    build:

      context: ./server

    ports:

    - "7001:7001"

    depends_on:

    - db

    - redis

    networks:

    - backend

Copy the code

Configuration:

  • Build: -> context:./server Specifies that the image is generated by./server/Dockerfile

  • Ports -> “7001:7001” local port mapping, this does not matter, will pass nginx proxy it

  • Depends_on: -> It depends on db and Redis. Mysql and Redis configurations will be expanded later


# ./webapp/DockerfileFROM the node: 12.10.0 - alpine RUN apk - update - no - cache add tzdata bash curl \ && cp/usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ &&echo "Asia/Shanghai"> /etc/timezone \ && apk del tzdata RUN mkdir -p /usr/src/app WORKDIR /usr/src/app COPY ./package.json /usr/src/app/ RUN  npm install --production --registry=https://registry.npm.taobao.org COPY . /usr/src/app COPYwait-for-it.sh /

CMD /wait-for-it.sh db:3306 -- npm run start

Copy the code

Similar to WebApp, RUN apk… Set the Shanghai time zone, NPM install install project dependencies, NPM run start start project, egg runs on port 7001 by default.

MySQL

We use the official image directly, just need to configure docker-comemage. yml does not need Dockerfile


# ./docker-compose.ymlDb: image: mysql:5.7 ports: -3306:3306 environment: - MYSQL_DATABASE=article - MYSQL_USER=worker - MYSQL_PASSWORD=wptworker - MYSQL_ROOT_PASSWORD=wptroot - TZ=UTC volumes: - ./db/conf.d:/etc/mysql/conf.d:ro - ./db/logs:/var/log/mysql:rw

      - ./db/initdb.d:/docker-entrypoint-initdb.d:ro

    networks:

    - backend

Copy the code

This section describes how to configure the default mysql database, account password, and configuration file mapping. Docker – compose up database directly after start in port 3306, you can use the command line or a graphical tool connection management database

Redis

Redis and mysql are basically the same


# ./docker-compose.yml

  redis:

    build: ./redis

    ports:

      - "6379:6379"

    volumes:

      - ./redis/redis.conf/:/usr/local/etc/redis.conf

      - ./redis/data:/usr/local/redis/data

      - ./redis/redis.log:/usr/local/redis/redis.log

    restart: always

    container_name: redis

    networks:

      - backend

Copy the code

# ./redis/Dockerfile

FROM redis

CMD [ "redis-server"."/usr/local/etc/redis.conf" ]

Copy the code

The official image is used in Dockerfile, and a line of configuration is added to specify the location of redis.conf, which is mapped in the YML file.

Hit the pit

Dockerfile


COPY ./package.json .

RUN npm install --production --registry=https://registry.npm.taobao.org

COPY . .

RUN npm run build

Copy the code

Here we copied it twice.


COPY . .

RUN npm install --production --registry=https://registry.npm.taobao.org

RUN npm run build

Copy the code

COPY once is fine, but Docker builds images that are layered in cache. As soon as there are any changes in the directory, these three instructions are re-executed and the previous cache is invalidated.

NPM install will use the cache as long as the dependencies remain the same. You should know that installing dependencies is slow.

node-sass

If you have problems installing Node-sass, you can replace the node:10-alpine image with blairguk/ Node-sass-alpine :8.11.0.

The localhost that accesses the host in the container

Nginx agents will most likely need to delegate other ports on the local machine. You can read the local IP address to access the host, but this is tied to the current environment. Qoomon/Docker-host/qoomon/ Docker-host/qoomon/ Docker-host Attached configuration:


# ./docker-compose.yml

  dockerhost:

      image: qoomon/docker-host

      cap_add: [ 'NET_ADMIN'.'NET_RAW' ]

      restart: on-failure

      networks:

      - dev

Copy the code

conclusion

Before we used Docker, deploying projects in new environments was often tedious and painful. Mysql, Nginx, Redis, and front-end projects are installed and configured one by one. For now, docker-compose up 🚀🚀🚀 is pretty cool.

Of course, in addition to convenient deployment, Docker also has great value in distributed and virtual machine technology. Interested students should learn it well

Refer to the article

Docker – From getting started to practice

Docker official documentation