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 does Docker create MySQL containers and use Link to establish connections between containers

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains how Docker creates MySQL containers and uses Link to establish connections between containers. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Docker how to create MySQL containers and use Link to establish connections between containers"!

When using Docker, we often encounter applications where I need two or more containers, some of which require services provided by other containers. For example, we need one container to provide mysql database services, and two other containers to connect as clients to use mysql database services. Let's see how Docker can do this via Link.

1. Here we first create two container images, one to simulate mysql database, the other using mysql client to simulate some application using mysql services, this application can be any PHP, Python, Java, etc. application.

1.1 First create a mysql_server directory and create a Dockerfile under it with the following contents

1

2

3

4

5

6

7

8

9

10

11

12

13

FROM centos:centos6

MAINTAINER Fanbin Kong"kongxx@hotmail.com"

RUN yuminstall-y mysql-server mysql

RUN/etc/init.d/mysqldstart &&\

mysql -e"grant all privileges on *.* to 'root'@'%' identified by 'letmein';"&&\

mysql -e"grant all privileges on *.* to 'root'@'localhost' identified by 'letmein';"&&\

mysql -u root -pletmein -e"show databases;"

EXPOSE 3306

CMD ["/usr/bin/mysqld_safe"]

Then create an image from Dockerfile

1

sudodocker build -t kongxx/mysql_server.

1.2 Create a mysql_client directory and create a Dockerfile under it with the following contents

1

2

3

4

FROM centos:centos6

MAINTAINER Fanbin Kong"kongxx@hotmail.com"

RUN yuminstall-y mysql

Then create an image from Dockerfile

1

sudodocker build -t kongxx/mysql_client.

1.3 After creating the image, we can use the following command to view the results

1

2

3

$sudodocker images |grepkongxx

kongxx/mysql_client latest aa31f22f6fc5 2 hours ago 303.7 MB

kongxx/mysql_server latest 3b9b08c8dda4 2 hours ago 353.3 MB

2. The second step is to create our application scenario based on image

2.1 Start by creating a container that provides mysql database services

1

sudodocker run --name=mysql_server -d -P kongxx/mysql_server

2.2 Create two containers using the mysql database service created in the previous step

The first application container

1

sudodocker run --name=mysql_client1 --link=mysql_server:db -t -i kongxx/mysql_client/usr/bin/mysql-h db -u root -pletmein

Second application container

1

sudodocker run --name=mysql_client2 --link=mysql_server:db -t -i kongxx/mysql_client/usr/bin/mysql-h db -u root -pletmein

Special attention should be paid to the "-link=mysql_server:db" parameter, which tells Docker containers to use the "mysql_server" container and name its alias db, so that "db" can be used as the machine name for providing mysql database services in these two containers. So in the last boot parameter we use "/usr /bin/mysql -h db -u root-pletmein" to connect to mysql database.

2.3 After running the above two commands, we will create two mysql client containers, at this time we can use the following command to check the status

1

2

3

4

5

sudodockerps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

ac0c76c627c0 kongxx/mysql_client:latest /usr/bin/mysql-h db 10 seconds ago Up 9 seconds mysql_client2

763c4825722d kongxx/mysql_client:latest /usr/bin/mysql-h db 41 minutes ago Up 40 minutes mysql_client

32f7839f7e9d kongxx/mysql_server:latest /usr/bin/mysqld_safe About an hour ago Up About an hour 0.0.0.0:49153->3306/tcp mysql_client1/db,mysql_client2/db,mysql_server

Notice the last line, which is the "NAMES" column of the mysql_server container,"mysql_client/db,mysql_client2/db,mysql_server", which shows that mysql_client1 and mysql_client2 are connected to db.

At this point, I believe that everyone has a deeper understanding of "Docker how to create MySQL containers and use Link to establish connections between containers". Let's do it in practice! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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

Servers

Wechat

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

12
Report