Use Dockerfile for deployment, refer to the tutorial:

Docker- Deployable SpringBoot backend project

Docker- Deploy the Vue front-end project

Points to remember:

  • Port mapping in each service container Host Port: Container Port The host port can be changed and the container port uses the default port of each service
  • The containers of each service are isolated from each other
  • When deploying multiple projects using Docker, you only need to ensure that the host ports mapped by each service container are different, and the default ports are used in each service container.
  • If you modify the configuration file of a service mounted to the host, you only need to restart a single service container for the modification to take effect: docker restart Container name
  • If the docker-compose. Yml file is modified, you need to run the docker-compose down command to delete all related services and run the docker-compose up command to restart the docker-compose file for the modification to take effect
  • Projects to be deployed can have an independent service configuration environment. You only need to ensure that the host ports mapped to each service container are different.

Reference links:

Docker tutorial

First, environmental preparation

1. Environmental

Host Host: a VM

Host host system: centos7

Host host system environment: Docker, docker-compose software environment has been installed on the default virtual host

Concept 2.

Local host: a computer used locally

Host host: host (virtual machine, server, or localhost) with docker environment deployedCopy the code

Image: Docker Image, which is equivalent to a root file system. For example, the official ubuntu:16.04 image includes a complete set of root files for ubuntu 16.04’s minimum system.

Container: The relationship between an Image and a Container is similar to that between a class and an instance in object-oriented programming. An Image is a static definition and a Container is an entity of the Image runtime. Containers can be created, started, stopped, deleted, paused, and so on.

Repository: A Repository can be thought of as a code control center that holds images.

Ii. Document preparation

1. Build a directory structure for the file

You can prepare the directory structure and project files of the project to be deployed on the local host in advance and upload them uniformly to the host host (that is, the host with the Docker environment deployed).Copy the code

** Overall project files: ** The directory structure of a project is as follows,

  • amieemc

    • env
    • server
    • web

    Dockerfile

    docker-compose.yml

amieemc  # Project's overall folder
	|- env # 1. The environment folder on which the project depends, by mounting the files in the container associated with this data volume
		|- nginx #1.1 Nginx folder
			|- conf
				|- nginx.conf # 1.1.1 Nginx configuration file folder, used to mount nginx container for configuration association
			|- logs # 1.1.2 Nginx log folder
		|- postgres # 1.2 Postgres database folder
			|- data # 1.2.1 The postgres data folder is associated with the postgres container data files to prevent data loss caused by the deletion of the container
		|- redis # 1.3 Redis folder
			|- data # 1.3.1 Redis data folder, and redis container data files associated to prevent data loss due to container deletion
  |- server # 2. Code folder for back-end projects
		|- ... # 2.1 Code files for back-end projects
	|- web # 3. Front-end project code folder-...#3.1 Front-end project code file
	|- docker-compose.yml Docker-comemage. yml configuration file for unified deployment
	|- Dockerfile # 5. The Dockerfile configuration file used to build project images
Copy the code

** Back-end project files: ** In the case of the AMI project, the files in the back-end project server are as follows:

Front-end project file: The content of the file is as follows, which is the file in DIST after being packaged through NPM Run build

2. Contents of the configuration file

2.1 Backend project image configuration file Dockerfile

Dockerfile file contents:

FROM java:8  # Mirror and version numbers that the project depends on
VOLUME /tmp  # define anonymous data volume. If you forget to mount the data volume when starting the container, it will be automatically mounted to the anonymous volume.
EXPOSE 8005  Use the same container port number as server.port in application.yml
WORKDIR /usr/local/amieemc # specify backend projects in the container's working directory, no need to create them in advanceCOPY the server/ami - eemc - 0.0.1. Jar/usr /local/amieemc Copy the jar files of the back-end project on the host to the working directory in the container
ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/ usr/local/amieemc/ami - eemc - 0.0.1. Jar"]
Copy the code

COPY instruction detail:

COPY < source address > < destination address >Copy the code

Source address: the source address derived from the current context directory where the Dockerfile resides.

Destination address: the working directory of the back-end project in the container;

ENTRYPOINT: similar to CMD directives, but not overridden by directives specified by the docker Run command line arguments, and these command line arguments are sent as arguments to the program specified by the ENTRYPOINT directive.

This is the instruction to execute when running the back-end project.

ENTRYPOINT ["<executeable>"."<param1>"."<param2>". ]Copy the code

Executeable: an executable program instruction;

Param1: multiple parameters can be passed when the specified executeable program is executed.

Docker run will automatically execute the above ENTRYPOINT specified instructions when executing docker run. For example, docker run will execute the following command to start the backend project:

java -Djava.security.egd=file:/dev/./urandom -jar /usr/local/ amieemc/ami - eemc - 0.0.1. JarCopy the code

Docker Dockerfile

2.2 Unified deployment configuration file docker-comemage.yml

Docker-comemage. yml file contents:

version: '3' Docker-compose version number
services:
  amieemc: Configure the amIEEMC background service
    build: # build Dockerfile as an image, then run the image with a prefix (current directory name)
      context: . # specify the context directory where the Dockerfile resides."." indicates the current directory
      dockerfile: Dockerfile # Dockerfile File name
    container_name: amieemc_v1 # container name
    volumes: 
      - /project/amieemc/server:/usr/local/amieemc Mount the amieEMC backend file directory on the host to the corresponding directory in the container, you need to create the directory on the host in advance
    ports:
      - "0.0.0.0:8005:8005" # host host port number: container port number
    depends_on: # amieEMC services depend on other services. The dependent services are started first, followed by back-end project services, in the specified order
      - postgres
      - redis
      - nginx
  # configuration redis
  redis:
    image: redis:latest # redis mirror
    container_name: amieemc_v1_redis # redis The container name of the image
    ports:
      - "0.0.0.0:6379:6379" # Host port and container port mapping, redis in the background project service application. Yml configured port, should be the host port
    volumes:
      - /project/amieemc/env/redis/data:/data # mount the Redis storage directory to the container to persist the database data and avoid data loss caused by the container stop
    command: "redis-server --appendonly yes" This command is used to override the container default command
  # configuration postgres
  postgres:
    image: postgres:latest
    container_name: amieemc_v1_postgres
    ports:
      - "0.0.0.0:5432:5432"
    volumes:
      - /project/amieemc/env/psotgres/data:/data/db Mount postgres data directory to container to persist database data
    environment: Configure postgres environment variables
      POSTGRES_PASSWORD: root Postgres = 'postgres'; postgres =' postgres'
  # configure nginx
  nginx:
    image: nginx:latest
    container_name: amieemc_v1_nginx
    ports:
      - "0.0.0.0:8090:80" # note here configuration of the port and/project/amieemc/env/nginx/conf/nginx. Conf file listening ports
    volumes:
      - /project/amieemc/web:/usr/local/nginx/html
      - /project/amieemc/env/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
      - /project/amieemc/env/nginx/logs:/var/log/nginx
Copy the code

Description:

(1) AmieEMC in the file is the service name of the back-end project. The image of the back-end project is built through Dockerfile, and then the container of the back-end project is run by Docker Run to start the back-end project.

(2) In the configuration of back-end project service AMIEEMC, attention should be paid to port mapping ports. The container port number, EXPOSE port number in Dockerfile, and server.port number in application.yml should be the same. The host port number can be different, which is used for external access. The container port number is the port number used by the service in the container.

(3) the back-end service project through depends_on depend on other services, port configuration for “host host port: container port”, the container port number for the service in the container port, generally use the default service, as far as possible do not modify, mapping host host port (external access use) can be different, The external accesses the service using the port number of the mapped host host.

(3) because each container is isolated, the deployment of multiple objective situation, the project relies on other services in the container port can be consistent, because each other, but the mapping host host port must be repeated, not because of external access is host host port, if there is a repeated complains, lead to port conflicts.

2.3 Contents of configuration files related to front-end and back-end projects

Properties, application. Yml, config.properties and logback.xml files.

Database. properties: Changes the IP address, port number, database name, user name, and password of the database. IP is the IP address of the host host, and the port number and password are the port number and password of the host host of the Postgres service configured in the docker-comemage. yml file. The default user name is Postgres.

Application. Yml: Modify the redis configuration. The IP address is the IP address of the host host and the port number is the port number of the host host of the Redis service configured in the docker-comedy. yml file.

**config.properties: ** This configuration file is used to modify verify’s machine code by executing the verify.sh file after deploying the project to the host and replacing the value of Verify in the configuration file.

Logback. XML: this file changes the log file path. The original project is deployed on a Windows OPERATING system, and the log file path is Drive letter :/logs.

(2) Modification of front-end project configuration file

The front-end project configuration files are mainly serverconfig. js in the Web folder and nginx configuration file nginx.conf.

Serverconfig. js: This file mainly changes the “IP: port number” of the corresponding back-end service. The IP is the IP address of the host host, and the port number is the port number of the host host in the ports configured for the project service in the docker-comedy. yml file.

Conf: This file modifies the port number of the front-end project. The port number is the port number of the host in the ports configured by the nginx service in the docker-comemage. yml file.

Three, deployment,

1. Upload the project file to the host

Create a folder under the root directory (not root but /) of the host to store the project files. The following uses the project folder as an example:

Use the file upload tool (finalshell is used here) to upload the project file amieEMC from the local host to the project folder, and then use the CD command in the command window to enter the folder where docker-comemess. yml is located (amieEMC project folder is used here).

1.1 Back-end AMI project to obtain machine code

Run the CD command to go to the server folder of the back-end project, run the “./verify.sh” command to obtain the machine code, and copy the verify content to the config.properties file.

If a message is displayed indicating that you do not have the permission to execute the file, run the chmod +x verify.sh command to modify the permission to execute the file.

# Change file execution permission
chmod +x verify.sh 
# run file
./verify.sh
Copy the code

1.2 Starting the Service

In the directory where docker-comedy. yml is located, run the following command to start the service. If the “-d” parameter is used in the startup command, the service will run in the background. You can view the startup log of the service by using the command. If -d is not added, the service startup information is displayed in the current window.

-d start the service
docker-compose up -d
View service startup logs
docker-compose logs -f
Copy the code

After the initial startup, you can run the following command to view information about the image and container

# view all mirrors
docker images
# View the running container
docker ps 
# View all containers (running or not running)
docker ps -a
Copy the code

After the first successful startup, the backend project service will report an error because the corresponding database and table have not been created in postgres and other services will be normal. In this case, communicate with the postgres service through a database tool such as navicate for premium. Then create the database, import the corresponding database tables, and restart the Postgres service again.

Restart a container for a single serviceDocker restart amieemc_v1_postgresCopy the code

At this point, the entire front-end and back-end project is deployed. You can access the project by entering “http://host host IP: host host port number mapped to the front-end project (that is, the nginx service)” in the browser (for example, http://host host IP :8090).

1.3 Restarting a Service

Configuration file modification of hardpoints: due to the container in the configuration file mount to host the corresponding in the host configuration file, so service containers loaded at run time is the file in a mount point, when the mount point of the file changes, such as nginx configuration files and backend configuration files of the project, can restart the corresponding service container to make the changes to take effect.

Restart a container for a single serviceDocker restart Specifies the container nameCopy the code

1.4 Delete all services of the project

Docker-comemage. yml and Dockerfile: When the docker-comemage. yml and Dockerfile are modified, you need to delete the original container and restart it to make the modification take effect.

Note: When executing the docker-compose command for the corresponding project, you need to enter the docker-compose. Yml folder corresponding to each project

Delete all service containers for a project
docker-compose down
Copy the code

Refer to the link


Docker command