Here’s why

  • To deploy a Mocker platform, I chose apI-Mocker, an off-the-shelf project, on the recommendation of a friend
  • The project is divided into server node, client Vue, and mongoDB database
  • When trying to deploy directly, I found the need to install a lot of environment, Node, Mongo, nginx ah, special trouble, before simple use of docker, JUST want to use docker free environment direct deployment? Hence the attempt

Multicontainer communication

  • The project is divided into three parts, so you need to create three containers (Node, Mongo, nginx)
  • How do containers communicate with each other?
 Create a connection using the link command
 $ docker run --name <Name> -d -p <path1>:<path2> --link <containerName>:<alias> <containerName:tag/imageID>
Copy the code
- --link container connection instruction - < name > : < alias > - < name > : $curl <alias> - $curl <alias> - $curl <alias>Copy the code

Next, let’s try deployment

The implementation process

1, build mongo containers 2, build the node and establish connection with mongo container 3, build nginx containers with the node and connection is established

Build the Mongo container

  • Let’s take the Mongo image
    $ docker pull mongo:latest
Copy the code
 $ docker images
 REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
 mongo               latest              05b3651ee24e        2 weeks ago         382MB
Copy the code
  • Now let’s make this mirror image run
 $ docker run --name mock-mongo -d -p 27017:27017 mongo:latest --auth
 To create a file share with a local host, use the -v command
 # -v /data/db:/data/db
Copy the code

— Auth command enables mongo connection authentication. This function is enabled because the server cannot connect to the Mongo database if the node does not set authentication when connecting across containers

```
 nodejs.MongoError: [egg-mongoose]Authentication failed.
```  
Copy the code
  • Check the 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-mongoCopy the code
  • Since we have mongo authentication enabled, we need to enter the Mongo container and configure the account used to connect to Node
    $ docker exec -it mock-mongo /bin/bash
    $ mongo admin
    Create manager user
    $ db.createUser({user:"admin".pwd:"admin",roles:[{role:"dbAdmin",db:"admin"}]})
    # Account authorization
    $ db.auth('admin'.'admin')
    Copy the code

Now that our Mongo database is up and running, it’s time to create the Node container

Build the Node container and connect it to the Mongo container

  • The mongo container alias, port number, and login password should be agreed upon before building the Node container

    • Mongo container alias: db
    • Mongo port number: 27017
    • Account password: admin: admin
  • Let’s start by modifying the node server configuration

    File configuration dockerfile/API – mocker/server/config/config. The default. The js modified mongo connection configuration, the db for a mock – mongo container alias

      mongoose: {
        url: 'mongodb://admin:admin@db:27017/api-mock? authSource=admin'
      },
    Copy the code
  • Now we write a Dockerfile file to build the image

      # specify the base image
      FROM node:latest
        
      # maintainers
      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 the container is built, only when the container is started
      CMD ["make"."prod_server"]
    
    Copy the code
  • We use the dockerfile file we have written to build the image

      $ docker build -t="The mock - server: 1.0.0" .
    Copy the code
  • Let’s look at the mirror 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 382MBCopy the code
  • Now comes the crucial step of running the Mocker-Server image and establishing the connection between the server and the database

       $ docker run -d-i-t -p 7001:7001 --name mock-server1 --link mock-mongo:db mock-server:1.0.0 /bin/bashCopy the code
  • Let’s take a look at the container now 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-mongoCopy the code
  • Check the connection status between node and Mongo containers
 $ docker exec -it mock-server /bin/bash
 $ curl db
Copy the code

Now that our server is connected to the database, we need to deploy our client

Build the nginx container and connect it to the Node container

  • Before establishing nginx, we need to agree on the node container alias, the port number for nginx forwarding, and the domain name and port number for clients to access nginx

    • Node Server alias: node
    • Port number mapped to the Node container: 7001
    • Nginx domain name: 127.0.0.1
    • Nginx port number: 90
  • Let’s start by pulling the Nginx image and creating the 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, the connection is successfulNODE_PORT_7001_TCP = TCP: / / 172.17.0.3:7001 NODE_PORT_7001_TCP_PORT = 7001 NODE_ENV_YARN_VERSION = 1.9.4Copy the code
  • Now we are looking at a container that is already running

     $ 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-mongoCopy the code
  • Due to the independent deployment of the front end, we need to modify the configuration of nginx. The configuration can be modified in the following ways

    • To create the container, use the -v command to mount the configuration file to the local host, and restart nginx in the container after the local modification
    • Copy the configuration file to the local host, replace the corresponding file of the container, and restart nginx in the container
    • .

    Our current operating environment is 17 edition 15 inch macbook Pro, which requires special configuration for mounting, so I adopted the second method

  • Modifying configuration files

    • Container configuration file path/etc/nginx/conf. D/default. Conf
    • Copy the configuration file to the local PC
        $ docker cp mock-nginx:/etc/nginx/conf.d/default.conf ~/nginx/default.conf
    Copy the code
    • Add the following configuration to the nginx configuration file
    server {
       location /mock-api/ {
           # node is an alias for the instruction server container
           proxy_pass http://node:7001/;
       }
    
       location /mock {
         autoindex on;
         alias/root/dist; }}Copy the code
    • Overwrite the container configuration and restart nginx
    $ docker cp ~/nginx/default.conf mock-nginx:/etc/nginx/conf.d/default.conf
    # Enter container
    $ docker exec -it mock-nginx /bin/bash
    # Restart nginx. If the following message is displayed, the restart is successful
    $ nginx -s reload
    2018/11/03 17:23:14 [notice] 68#68: signal process started
        
    Copy the code
    • This brings us to our final and exciting step

    • Modify the network domain name requested by our front-end project and upload the package

    // api-mocker/client/config 
    // module.exports > build > serverRoot
    
    module.exports = {
        build: {
            serverRoot: '127.0.0.1:90 / mock - API'}}Copy the code
    • Upload the packaged dist file to the /root/dist directory of the nginx configuration
       $ docker cp ~/Sites/api-mocker/client/dist mock-nginx:/root
    Copy the code
    > < p style = "max-width: 100%; clear: both; min-height: 1emCopy the code

test

  • We’ve done all the mind-blowing stuff, and now we’re going to test it out

  • Visit the front end project: http://127.0.0.1:90/mock we will see that the following screen indicates that our front end project has been successfully deployed

  • When we try to register an account and see a success message, it means that our entire project has been successfully deployed

Now that our deployment is complete, we can have fun writing projects using the mock interface

conclusion

It was the first time to write an article and deploy like this. I sorted out my ideas and hoped to bring some help to you. Finally, I attached the common docker commands I sorted out and the project used the configuration file SegmentFault link