The reason is this I wanted to deploy a mocker platform, so I chose the ready-made project api-mocker on the recommendation of a friend. The project is divided into server node, client vue, and database mongoDB When I tried to deploy directly, I found that I needed to install a lot of environments, such as node, mongo, and nginx. It was very troublesome. I had used docker before, so I was wondering if I could use docker to deploy directly without the environment? So there was this attempt Multi-container communication The project is divided into 3 parts, so 3 containers (node, mongo, nginx) need to be established How do containers communicate with each other? # Establish a connection through the link command$ docker run --name <Name> -d -p <path1>:<path2> --link <containerName>:<alias> <containerName:tag/imageID>
Next, we start trying to deploy Implementation process 1. Build a mongo container 2. Build the node container and establish a connection with the mongo container 3. Build the nginx container and establish a connection with the node container Build the mongo container Let's pull the mongo image first $ docker pull mongo:latest Now let's run this image. $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE mongo latest 05b3651ee24e 2 weeks ago 382MB The --auth directive turns on the Mongo connection identity verification. The verification is turned on because the identity verification is not set when the node is connected across containers. The server cannot connect to the Mongo database.
View Container $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0d440be90935 mongo "docker-entrypoint.s..." 14 hours ago Up 14 hours 0.0.0.0:27017->27017/tcp mock-mongo Since we have enabled authentication for mongo, we need to enter the mongo container and configure the account used when the node connects $ docker exec -it mock-mongo /bin/bash $ mongo admin # Create a manager user $ db.createUser({user:"admin", pwd:"admin", roles:[{role:"admin", db:"admin"}]}) # Account authorization $ db.auth('admin','admin') Now that our mongo database is running, we will create a node container. Build the node container and establish a connection with the mongo container Before we start building the node container, we must first agree on the mongo container alias, port number, and login account password
Let's first modify the configuration of the node server File configuration dockerfile/api-mocker/server/config/config.default.js Modify the mongo connection configuration, db is the alias of the pre-set mock-mongo container mongoose: { url: 'mongodb://admin:admin@db:27017/api-mock?authSource=admin' }, Now we write a Dockerfile to build the image # Specify the base image FROM node:latest # Maintainer MAINTAINER [email protected] # Working directory WORKDIR /www #Copy the local file to the container without decompressing it COPY api-mocker node-server/api-mocker EXPOSE 7001 WORKDIR /www/node-server/api-mocker/server RUN npm install WORKDIR /www/node-server/api-mocker # Called after building the container, and called when the container starts CMD ["make", "prod_server"] We use the written Dockerfile file to build the image $ docker build -t="mock-server:1.0.0" . Let's check out the image $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE mock-server 1.0.0 957ad2aa1f97 8 minutes ago 674MB mongo latest 05b3651ee24e 2 weeks ago 382MB Now comes the critical step. We will run the mocker-server image and establish a connection between the server and the database. Copy the code as follows: $ docker run -d -i -t -p 7001:7001 --name mock-server1 --link mock-mongo:db mock-server:1.0.0 /bin/bash Let's take a look at the container that is currently running $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ee780b903c64 mock-server:1.0.0 "/bin/bash" About a minute ago Up 11 seconds 0.0.0.0:7001->7001/tcp mock-server 0d440be90935 mongo "docker-entrypoint.s..." 16 hours ago Up 16 hours 0.0.0.0:27017->27017/tcp mock-mongo Check the connection status between the node container and the mongo container $ docker exec -it mock-server /bin/bash $ curl db Now that our server and database have established a connection, we will start deploying our client. Build the nginx container and establish a connection with the node container Before setting up nginx, we must first agree on the node container alias, the port number forwarded by nginx, and the domain name and port number for client access to nginx
Let's first pull the nginx image and create a container $ docker pull nginx:latest $ docker run -p 90:80 --link mock-node:node nginx:latest --name mock-nginx # Check the container connection status $ docker exec -it mock-nginx /bin/bash $env # If you see the following data, it means the connection is successful NODE_PORT_7001_TCP=tcp://172.17.0.3:7001 NODE_PORT_7001_TCP_PORT=7001 NODE_ENV_YARN_VERSION=1.9.4 Now let's take a look at the running containers $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 09644025d148 nginx "nginx -g 'daemon of..." 5 hours ago Up 5 hours 0.0.0.0:90->80/tcp mock-nginx ee780b903c64 mock-server:1.0.0 "/bin/bash" About a minute ago Up 11 seconds 0.0.0.0:7001->7001/tcp mock-server 0d440be90935 mongo "docker-entrypoint.s..." 24 hours ago Up 24 hours 0.0.0.0:27017->27017/tcp mock-mongo Due to the independent deployment of the front end, we need to modify the configuration of nginx. There are several ways to modify the configuration of nginx:
Our current operating environment is a 17-version 15-inch MacBook Pro, which requires special configuration for mounting, so I used the second method. Configuration file modification
$ docker cp mock-nginx:/etc/nginx/conf.d/default.conf ~/nginx/default.conf Add the following configuration to the nginx configuration file server { location /mock-api/ { # node is the alias of the command server container proxy_pass http://node:7001/; } location /mock { autoindex on; alias /root/dist; } } Overwrite the configuration in the container and restart nginx $ docker cp ~/nginx/default.conf mock-nginx:/etc/nginx/conf.d/default.conf # Enter the container$ docker exec -it mock-nginx /bin/bash # Restart nginx. If you see the following prompt, it means the restart is successful. $ nginx -s reload 2018/11/03 17:23:14 [notice] 68#68: signal process started Now comes our final exciting step Modify the network domain name requested by our front-end project and package and upload it //api-mocker/client/config // module.exports > build > serverRoot module.exports = { build: { serverRoot: '127.0.0.1:90/mock-api' } } Upload the packaged dist file to the /root/dist directory of the nginx configuration $ docker cp ~/Sites/api-mocker/client/dist mock-nginx:/root > Of course, nginx container construction can also be implemented by writing a dockfile file. We will not explain it in detail here. Mount the configuration file and log to the local host. If you are interested, you can try it yourself. Copy the code test We have completed all the exciting configurations, now let's test it Visit the front-end project: http://127.0.0.1:90/mock We will see the following interface indicating that our front-end project has been deployed successfully We try to register an account and see a successful prompt, which means that our entire project has been deployed successfully. At this point, our deployment is complete, and we can happily mock the interface and write the project. Sprinkle flowers~~~ Summarize It was difficult to write an article for the first time and to deploy it like this for the first time. I sorted out my thoughts and hope it can be of some help to you. Finally, I attach my own commonly used docker commands and the configuration files used in this project. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: How to install and modify the initial password of mysql5.7.18
>>: How to install and modify the initial password of mysql5.7.18 under Centos7.3
Because I want to write a web page myself, I am al...
Introduction The Docker community has created man...
The mysql explain command is used to show how MyS...
Preface Use js to achieve a year rotation selecti...
This example uses jQuery to implement a mouse dra...
Table of contents 1. Introduction to built-in obj...
FTP is mainly used for file transfer, and is gene...
Chapter 1 Source Code Installation The installati...
There are two ways to install nodejs in linux. On...
Table of contents 1. js memory 2. Assignment 3. S...
After the release of CentOS8.0-1905, we tried to ...
Why do we need to build a nexus private server? T...
Starting from IE 8, IE added a compatibility mode,...
Firewall A firewall is a set of rules. When a pac...
This is an effect created purely using CSS. To pu...