Home > Article > Operation and Maintenance > How to access docker database
In recent years, Docker has become a very popular containerization platform. As a lightweight virtualization solution based on container technology, Docker has been widely used in DevOps, cloud computing and other fields. Among them, the Docker database function has attracted much attention. Through Docker database, we can easily create, manage, and deploy database containers. But how to easily access these Docker databases? The following will introduce you in detail how to access the Docker database.
1. Install Docker
First, in order to use the Docker database, we need to install Docker first. Docker provides many different installation methods, and we can choose the corresponding installation method according to different operating systems.
For linux users, you can use apt-get or yum command to install; for windows and mac users, you can go to the Docker official website to download the corresponding version for installation.
2. Create a Docker container
Docker database needs to run in the form of a container. Therefore, we need to create a container in Docker to run the corresponding database.
1. Pull the Docker image
Before creating the Docker container, we need to pull the corresponding Docker image. Docker Hub is an open registration center that maintains a large number of Docker images. We can get the Docker image we need from Docker Hub. Taking MYSQL as an example here, we can use the following command to pull the latest version of the Docker image of mysql by default.
docker pull mysql
2. Start the Docker container
After pulling the Docker image, we need to start the container and pass the corresponding configuration parameters into the container. Here, we can use the docker run command to start the Docker container.
(1) Docker starts the mysql container and specifies the container name as test:
docker run --name test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql
(2) Parameter description:
--name test: Name the container test.
-p 3306:3306: Map the 3306 port inside the container to the 3306 port of the host to facilitate subsequent connections.
-e MYSQL_ROOT_PASSWORD=root: Set the password of the MySQL root user to root.
-d mysql: Pull the mysql image from Docker Hub and run a mysql container in the background.
3. Access the Docker container
After the Docker container is started, we need to access the container to perform related operations. And we can connect the Docker container in two ways.
1. Use the host to access
We can connect by connecting to the host where the Docker container is located.
(1) First, you need to obtain the IP address of the container on the host:
docker inspect test|grep IPAddress
Output:
"SecondaryIPAddresses ": null,
"IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2",
You can see that the IP address of the Docker container is 172.17.0.2.
(2) Use the mysql client to connect to the Docker container:
mysql -h 172.17.0.2 -P3306 -uroot -p
Enter the password at the prompt. Log in to the MySQL database.
2. Use the access method inside the container
The second way is to use the access method inside the container. We can use the docker exec command to execute the corresponding command inside the Docker container.
(1) First get the container ID:
docker ps
Get the container ID based on the output result. For example, our container ID above is d7fe3107d754.
(2) Use the docker exec command to enter the container:
docker exec -it d7fe3107d754 /bin/bash
At this time, we can execute Linux commands inside the container. Access the Docker database. For example, the following command allows us to enter the MySQL client:
mysql -uroot -p
This way we can enter the MySQL database inside the Docker container.
Summary:
Through the above introduction, we can see that accessing the database in a Docker container is not difficult. You only need to pull the Docker image, start the Docker container, and then connect using the host or inside the container. This makes database creation, management, and deployment easy.
Docker database is a very important part of Docker technology and has been widely used in various scenarios. The method mentioned in the article is just one of them. Readers can flexibly choose different access methods according to their own needs and environment to achieve more efficient Docker database access.
The above is the detailed content of How to access docker database. For more information, please follow other related articles on the PHP Chinese website!