Docker container connection implementation steps analysis

Docker container connection implementation steps analysis

Generally speaking, after the container is started, we use the services provided by the container through port mapping. In fact, port mapping is only one way to use container services. In addition to this method, you can also use container connection to use container services.

For example, there are two containers, one container runs a SpringBoot project, and the other container runs a MySQL database. SpringBoot can directly access the MySQL database through the container connection without having to access the MySQL service through port mapping.

To keep the case simple, let me give you another example:

There are two containers, one nginx container and the other ubuntu. I start the nginx container, but do not assign port mappings, then start ubuntu, connect through the container, and access nginx in ubuntu.

The specific steps are as follows:

First, start an nginx container, but do not assign a port. The command is as follows:

docker run -d --name nginx1 nginx

The command execution results are as follows:


After the container is started successfully, it is inaccessible in the host machine.

Start Ubuntu

Next, start an ubuntu and establish a connection with nginx as follows:

docker run -dit --name ubuntu --link nginx1:mylink ubuntu bash

Here, --link is used to establish a connection. nginx1 is the container to which the connection is to be established, and the following mylink is the alias of the connection.

After successful operation, enter the ubuntu command line:

docker exec -it ubuntu bash

Then, there are two ways to view nginx information:

The first

Enter env directly in the Ubuntu console to view the environment variable information:


You can see that Docker creates a series of environment variables for nginx. Each variable is prefixed with MYLINK, which is the alias we just gave to the connection. Developers can use these environment variables to configure their applications to connect to nginx. The connection is secure and private. The access results are as follows:

The second

Another way is to view the hosts file of Ubuntu, as follows:


As you can see, several aliases have been given to nginx1 in the hosts file of Ubuntu. You can directly use these aliases to access nginx1.

Tips:

By default, the curl command is not installed in the Ubuntu container. You need to install it manually. The installation command is as follows:

apt-get update
apt-get install curl

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:
  • Detailed explanation of three ways to connect Docker containers to each other
  • Docker SSH sample code to connect to docker container
  • Introduction to Docker connection spring boot and mysql container method
  • Detailed explanation of docker: Allow host ssh to connect to docker container
  • How does Docker use links to establish connections between containers?
  • Docker multi-container connection (taking Tomcat+MySQL as an example)
  • Detailed explanation of building a Mysql container + Tomcat container connection environment through Docker
  • Detailed explanation of creating a MySQL container with Docker and connecting to the container through the command line
  • Docker uses Link to establish connections between containers

<<:  Rendering Function & JSX Details

>>:  Mysql NULL caused the pit

Recommend

After docker run, the status is always Exited

add -it docker run -it -name test -d nginx:latest...

JavaScript implementation of verification code case

This article shares the specific code for JavaScr...

Steps to modify the MySQL database data file path under Linux

After installing the MySQL database using the rpm...

Reasons why MySQL cancelled Query Cache

MySQL previously had a query cache, Query Cache. ...

A complete list of common Linux system commands for beginners

Learning Linux commands is the biggest obstacle f...

Tips for List Building for Website Maintenance Pages

And, many times, maintenance requires your website...

Solution to PHP not being able to be parsed after nginx installation is complete

Table of contents Method 1 Method 2 After install...

Interpretation of 17 advertising effectiveness measures

1. 85% of ads go unread <br />Interpretatio...

JS native 2048 game source code sharing (the latest on the Internet)

I have been learning about algorithms recently an...

Nginx operation and maintenance domain name verification method example

When configuring the interface domain name, each ...

How to use Typescript to encapsulate local storage

Table of contents Preface Local storage usage sce...

How to use time as a judgment condition in MySQL

Background: During the development process, we of...

Detailed explanation of the wonderful uses of SUID, SGID and SBIT in Linux

Preface Linux's file permission management is...

HTML tutorial, easy to learn HTML language (2)

*******************Introduction to HTML language (...