Docker and Linux: How to implement network communication between containers?

WBOY
Release: 2023-07-28 23:38:09
Original
1766 people have browsed it

Docker and Linux: How to implement network communication between containers?

Introduction:
Container technology plays an important role in the development and deployment of modern applications. By using container technology, we can package the application and its dependencies into an independent container, thus ensuring the portability and consistency of the application. However, when we need to connect multiple containers to enable network communication, configuring network communication between containers becomes very important. This article will introduce how to implement network communication between containers in Docker and Linux environments.

  1. Network mode in Docker
    Docker provides four different network modes to support container network communication, which are:
  2. Bridge mode (bridge): Default mode , the container is connected to the host network through a virtual bridge.
  3. Host mode (host): The container directly uses the host network without network isolation.
  4. none mode: The container has no network interface and is completely isolated from the external network.
  5. Container mode (container): Containers share a network namespace and can directly access other containers.
  6. Network communication example between containers
    Next, we will use a simple example to demonstrate how to implement network communication between containers in Docker and Linux environments. Suppose we have two containers, one is a web container and the other is a db container. We hope that the web container can access the database provided by the db container.

First, we need to create a network for communication between containers. We can create a bridge network named my_network using the following command:

$ docker network create my_network
Copy after login

Next, we need to create and run the web container to provide web services. We can use the following command to create a container named web_container and connect it to the my_network network:

$ docker run -d --name web_container --network my_network web_image
Copy after login

Where, web_image is the web container image we built ourselves.

Then, we need to create and run the db container to provide database services. We can use the following command to create a container named db_container and connect it to the my_network network:

$ docker run -d --name db_container --network my_network db_image
Copy after login

Among them, db_image is the db container image we built ourselves.

Now, we have created two containers and connected them to the same network. Next, we need to ensure that the web container can access the database provided by the db container.

In the web container, we can use the name of db_container to access it. For example, we can use the following connection string in the code in the web container to connect to the database:

jdbc:mysql://db_container:3306/my_database
Copy after login

In this connection string, db_container is the name of the db container, 3306 is the default port number of the database, my_database is the name of the database.

Through the above steps, we have successfully implemented network communication between the web container and the db container. The web container can access the database service provided by the db container through the container name.

Conclusion:
In Docker and Linux environments, we can achieve network communication between containers by configuring the network mode and creating a network. By properly setting up network connections, we can establish communication channels between containers, enabling multi-container deployment and distributed architectures for applications.

Code example:

Dockerfile for web container:

FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y apache2
EXPOSE 80
CMD ["apache2ctl", "-D", "FOREGROUND"]
Copy after login

Dockerfile for db container:

FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y mysql-server
EXPOSE 3306
CMD ["mysqld"]
Copy after login

Java code example in web container:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    public static void main(String[] args) {
        String url = "jdbc:mysql://db_container:3306/my_database";
        String user = "root";
        String password = "password";
        try (Connection conn = DriverManager.getConnection(url, user, password);
             Statement stmt = conn.createStatement()) {
            String query = "SELECT * FROM my_table";
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
                System.out.println(rs.getString("column1"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

The above is an introduction and example on how to implement network communication between containers in Docker and Linux environments. With the right network configuration and connection settings, we can easily communicate between containers and build more flexible and scalable application architectures.

The above is the detailed content of Docker and Linux: How to implement network communication between containers?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template