Home > Article > Operation and Maintenance > Docker and Linux: How to implement network communication between containers?
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.
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
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
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
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
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"]
Dockerfile for db container:
FROM ubuntu:latest RUN apt-get update RUN apt-get install -y mysql-server EXPOSE 3306 CMD ["mysqld"]
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(); } } }
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!