Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Docker deploys multi-container communication

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how Docker deploys multi-container communication, the content is detailed and easy to understand, the operation is simple and quick, and has a certain reference value. I believe you will gain something after reading this article on how to deploy multi-container communication in Docker. Let's take a look.

Here's the reason.

If you want to deploy a mocker platform, you choose api-mocker as an off-the-shelf project on the recommendation of a friend.

The project is divided into server node, client vue, and database mongodb.

When I try to deploy directly, I find that I need to install a lot of environments, such as node, mongo, nginx, especially troublesome. Having simply used docker before, I wonder if I can deploy directly without using docker environment. So there was this attempt.

Multi-container communication

The project is divided into three parts, so you need to set up three containers (node, mongo, nginx)

So how do containers communicate with each other?

# establish a connection through the link instruction $docker run-- name-d-p:-- link:

-link Container connection instruction

-

< containername >

:

< alias >

-

< 被连接容器名称 >

:

< 容器访问别名 >

-Note: aliases are used to access connected containers in containers that actively establish connections.

-the following instructions check the connection status in the container

$curl

Next, we started to try to deploy

Realization process

1. Build a mongo container

2. Build node container and establish connection with mongo container

3. Build nginx container and establish connection with node container

Build a mongo container

Let's pull the mongo image first.

$docker pull mongo:latest

Let's make this mirror run.

$docker images repository tag image id created size mongo latest 05b3651ee24e 2 weeks ago 382mb

-- auth directive enables connection authentication of mongo. This is due to the fact that the server cannot connect to the mongo database without authentication when node connects across containers.

`

Nodejs.mongoerror: [egg-mongoose] authentication failed.

`

View the container

$docker ps-acontainer id image command created status ports names0d440be90935 mongo "docker-entrypoint.s..." 14 hours ago up 14 hours 0.0.0.027017/tcp mock-mongo 27017-> 27017/tcp mock-mongo

Since authentication is enabled in our mongo, we need to go to the mongo container to configure the account used for node connection.

$docker exec-it mock-mongo / bin/bash$ mongo admin# create 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're going to set up a node container.

Build the node container and establish a connection with the mongo container

Before we start building the node container, we need to agree on the mongo container alias, port number and login account password.

Mongo container alias:

Db mongo port number: 27017

Account password: admin:admin

Let's first modify the configuration of the node server

File configuration dockerfile/api-mocker/server/config/config.default.js modifies the mongo connection configuration, and db is an alias for the preset mock-mongo container

Mongoose: {url: 'mongodb://admin:admin@db:27017/api-mock?authsource=admin'}

Now let's write a dockerfile file to build the image

# specify the basic image from node:latest # maintainer maintainer qiushiyuan1994@qq.com # working directory workdir / www # copy the local files to the container, do not extract 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 # and call the cmd only when the container is started ["make", "prod_server"]

We use the written dockerfile file to build the image

$docker build-t = "mock-server:1.0.0".

Let's check 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 382mb

Now the critical step is to run the mocker-server image and establish a connection between the server and the database

The copy code is as follows:

$docker run-d-I-t-p 7001 link mock-mongo:db mock-server:1.0.0 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 running now.

$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 about a minute ago up 7001-> 7001/tcp mock-server 0d440be90935 mongo "docker-entrypoint.s …" 16 hours ago up 16 hours 0.0.0.027017/tcp mock-mongo 27017-> 27017/tcp mock-mongo

Check the connection status between node container and mongo container

$docker exec-it mock-server / bin/bash $curl db

Now that our server has established a connection to the database, we are going to start deploying our client

Build the nginx container and establish a connection with the node container

Before establishing nginx, we need to agree on the alias of the node container, the port number forwarded by nginx, and the domain name and port number for client access to nginx.

Node server alias: node

Port number of node container mapping: 7001

Nginx domain name: 127.0.0.1

Nginx port number: 90

Let's pull the nginx image and build the container.

$docker pull nginx:latest$ docker run-p 90:80-- link mock-node:node nginx:latest-- name mock-nginx# to check the connection status of the container $docker exec-it mock-nginx / bin/bash$ env# indicates that the connection is successful if you see the following data

Now we're looking at the containers that are 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 about a minute ago up 90-> 80/tcp mock-nginx ee780b903c64 mock-server:1.0.0 "/ bin/bash" about a minute ago up 11 seconds 0.0.0.0 80/tcp mock-nginx ee780b903c64 mock-server:1.0.0 7001-> 7001/tcp mock-server 0d440be90935 mongo "docker-entrypoint.s …" 24 hours ago up 24 hours 0.0.0.027017/tcp mock-mongo 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

When setting up the container, use the-v command to mount the configuration file to the local host, and restart nginx in the container after local modification

Copy the configuration file to the local host, modify and replace the corresponding file of the container, and then restart nginx in the container

...

Our current operating environment is version 17 15-inch macbook pro, and mounting requires special configuration, so I adopted the second method.

Profile modification

In-container configuration file path / etc/nginx/conf.d/default.conf

Copy the configuration file locally

$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 command server container alias proxy_pass http://node:7001/;} location / mock {autoindex on; alias / root/dist;}}

Override the configuration in the container and restart nginx

$docker cp ~ / nginx/default.conf mock-nginx:/etc/nginx/conf.d/default.conf# enters the container $docker exec-it mock-nginx / bin/bash# to restart nginx, and the following prompt indicates that the restart is successful $nginx-s reload2018/11/03 17:23:14 [notice] 68pm 68: signal process started

And then it's our final, exciting, final step.

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

/ / api-mocker/client/config / / module.exports > build > serverrootmodule.exports = {build: {serverroot: '127.0.0.1

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, the container construction of nginx can also be implemented by writing a dockfile file. We will not elaborate here. If you are interested in mounting configuration files and logs to the local host, you can try to copy the code yourself.

test

We have completed all the thrilling configurations. Now let's test them.

Visit the front-end project: http://127.0.0.1:90/mock, we will see that the following interface shows that our front-end project has been successfully deployed

We try to sign up for an account and see a successful prompt, which means that our entire project has been deployed successfully.

This is the end of the article on "how Docker deploys multi-container communications". Thank you for reading! I believe you all have a certain understanding of "how to deploy multi-container communications in Docker". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report