Docker-compose: Docker-compose: Docker-compose: Docker-compose: Docker-compose: Docker-compose: Docker-compose: Docker-compose: Docker-compose The following article specifically said, what is wrong with the place welcome everyone comment.

The project in this article uses Gitlab+Ansible+Docker automatic deployment node service (written by Nest framework). The steps are as follows:

  1. Write docker-compose, docker-comemess.prod. yml configuration files
  2. Modify the package. The json
  3. Create two directories on the remote server, pull the Node service repository, and switch to different branches to represent the test and online Node services respectively
  4. .gitlab-ci.yml file preparation
  5. Ansible. yml file written
  6. Run the following command in the node service (test/online) directory on the remote serverdocker ps -aCheck whether the container is started normally. If the container is suspended in the Up state, the container is started successfully.Docker logs -f Container IDViewing container Logs
/opt/ XXX /server-prod/server docker-compose up -d Docker-compose -f docker-compose. Prod. yml up -d docker-compose -f docker-composeCopy the code

Specific steps

  1. Write docker-compose, docker-comemess.prod. yml configuration files
Mysql: image: mysql container_name: mysql_test restart: always environment: - MYSQL_ROOT_PASSWORD=test_sql - NODE_ENV=development ports: - 13306:3306 volumes: - /var/lib/mysql # Test node server: # node server container_name: server-test # Container name Image: node:14.15.0 ports: - "7007:5088" environment: -node_env =development volumes: -.:/data working_dir: /data depends_on: # mysql restart: on-failure:5 # mysql restart: on-failure:5 # mysql restart: on-failure:5 #Copy the code
# docker-comage.prod. yml version: '3.0' services: mysql_prod restart: always environment: - MYSQL_ROOT_PASSWORD=prod_sql - NODE_ENV=production ports: -13307 :3306 Volumes :/var/lib/mysql # online Node server Prod-server: container_name: server-prod image: Node :14.15.0 ports: - "7077:5089" environment: -node_env =production volumes: -.:/data working_dir: /data depends_on: - prod-mysql restart: on-failure:5 command: yarn start:prodCopy the code
  1. Make the following changes in package.json
# cross-env specifies NODE_ENV for development or online environment... "scripts": { ... "build": "nest build", "start": "yarn && cross-env NODE_ENV=development nest start", "start:prod": "yarn && yarn build && cross-env NODE_ENV=production node dist/src/main", ... },...Copy the code
  1. Create two directories on the remote server to store the test and online Node services respectively
Git clone node: /opt/ XXX /server-test/server Switch to the master branch /opt/ XXX /server-prod/serverCopy the code
  1. .gitlab-ci.yml file preparation
DEV_BRANCH is a CI variable. Master and dev branches - DOCKER_CONTAINER and DEV_DOCKER_CONTAINER correspond to online and test-started docker containers - ROOT_PATH and DEV_ROOT_PATH correspond to the path where the remote server is online and the test node service is stored. - CI_PROJECT_NAME Indicates the warehouse name on gitlab. -dev_deploy - master_deploy MASTER_deploy: stage: master_deploy image: ansible image address script: - echo \"${HOST}\" ansible_ssh_user=\"${USER}\" ansible_ssh_pass=\"${PASS}\" ansible_ssh_host=\"${HOST}\" > hosts - echo  ansible-playbook ansible.yaml -e hosts=${HOST} -i ./hosts - ansible-playbook ansible.yaml -e "HOST=${HOST} DEST_PATH=${ROOT_PATH}/${CI_PROJECT_NAME} DOCKER_CONTAINER_NAME=${DOCKER_CONTAINER} CUR_BRANCH=${BRANCH}" -i ./hosts - Rm -f hosts only: -master tags: -k8s dev_deploy: stage: dev_deploy image: Ansible image address script: - echo \"${HOST}\" ansible_ssh_user=\"${USER}\" ansible_ssh_pass=\"${PASS}\" ansible_ssh_host=\"${HOST}\" > hosts - echo  ansible-playbook ansible.yaml -e hosts=${HOST} -i ./hosts - ansible-playbook ansible.yaml -e "HOST=${HOST} DEST_PATH=${DEV_ROOT_PATH}/${CI_PROJECT_NAME} DOCKER_CONTAINER_NAME=${DEV_DOCKER_CONTAINER} CUR_BRANCH=${DEV_BRANCH}" -i  ./hosts - rm -f hosts only: - dev tags: - k8sCopy the code
  1. Ansible. yml file written
# CD Go to the node server directory, switch branches, pull the latest code, and restart the docker container - name: deploy hosts: "{{HOST}}" become_user: root become: yes tasks: Git checkout branch command: git checkout "{{CUR_BRANCH}}" args: chdir: "{{DEST_PATH}}" git pull command: git pull args: chdir: "{{ DEST_PATH }}" - name: docker restart container command: docker restart "{{ DOCKER_CONTAINER_NAME }}" args: chdir: "{{ DEST_PATH }}"Copy the code
  1. Run the following command in the remote server Node service (test/online) directory
/opt/ XXX /server-prod/server docker-compose up -d /opt/ XXX /server-prod/server docker-compose -f docker-compose.prod.yml up -dCopy the code

After successful startup, check the container startup status through Docker ps -A, as shown in the following figure:

Description:

  • Test environment: When the local dev branch code is submitted or other branches are merged into the DEV branch, node service will be deployed to the remote server through ITLAB CI and Ansible automation, branch switching in the remote directory of the corresponding server, pull the latest code, and restart the corresponding test Docker container

  • Online environment: When the local master branch code is submitted or other branches are merged to the Master branch, node services will be deployed to the remote server through ITLAB CI and Ansible automation, and branches will be switched under the remote directory of the corresponding server, the latest code will be pulled, and the corresponding online Docker container will be restarted

Problems encountered

Problem 1: There is no node_modules and dist directory in the test/online remote node service directory, that is, there is no docker container log file in the remote server at the same time. The following error is reported (it took a long time to solve this problem)

Screen: Dist and node_modules are not found in the docker-comemess. yml directory. Yml docker-comemess. yml: yarn && yarn start in package.json

Solution:

Docker-compose. yml version: '3.0' services:... server: ... command: yarn && yarn start # package.json "scripts": { ... "build": "nest build", "start": "cross-env NODE_ENV=development nest start", "start:prod": "cross-env NODE_ENV=production yarn build && node dist/src/main", ... }, # docker-compose.yml version: '3.0' services:... server: ... Command: yarn start # package.json "build": "nest build", "start": "yarn && cross-env NODE_ENV=development nest start", "start:prod": "yarn && yarn build && cross-env NODE_ENV=production node dist/src/main", ... }, scheme 2: "scripts": {... "build": "nest build", "start": "cross-env NODE_ENV=development nest start", "prestart": "yarn", "start:prod": "yarn build && cross-env NODE_ENV=production node dist/src/main", "prestart:prod": "yarn", ... },Copy the code

Note:

  1. Cross-env is placed before the executable command. In this project, the server reports cross-env not found when the environment variable is placed at the end. NODE_ENV displays undefined
  2. Script executable commands note the order in which they are executed, for exampleyarn && yarn build && cross-env NODE_ENV=production node dist/src/main
  3. The script in the pre

Fault 2: The front-end online domain name mapping does not take effect. After the nginx configuration file maps the online domain name, it is found that the page does not take effect when accessing the online domain name

The nginx test configuration file is the same as the nginx test configuration file except for the domain name and API proxy. Finally, it turned out to be an online Nginx configuration file with the wrong suffix, written xxx.confg, here I want to kill myself

Solution: Change the online nginx configuration file to the correct suffix, that is, xxx.conf

Problem 3: Gitlab CI execution is abnormal, and the specific error message is probably that the /server directory cannot be found

Check: print out CI variables and spliced out directory variables in CI, check which step is wrong, check and find that they are normal, the only difference is that environment variables are set behind CI variables

Solution: Try to change the environment variable to All default, the result is good, remember, do not randomly configure the environment variable behind CI, if modified, the corresponding Gitlab also needs to map, the location of the environment variable is shown in the figure below:

master_deploy: ... script: ... - echo ${ROOT_PATH} - echo ${CI_PROJECT_NAME} - echo ${ROOT_PATH}/${CI_PROJECT_NAME} - echo ${DOCKER_CONTAINER} - echo ${BRANCH} ... .Copy the code

This is the end of this article, and there will be another document on the full stack project from development to automated deployment (using Vue + Nest + Typeorm + Mysql+ Gitlab CI + Ansible + Docker).

Reference article:

Docs.microsoft.com/zh-cn/dotne…