Docker 简明教程
Docker - Setting MongoDB
MongoDB 是一个著名的面向文档的数据库,被许多现代网络应用程序所使用。由于 MongoDB 是一个流行的开发数据库,因此 Docker 也确保了它对 MongoDB 的支持。
MongoDB is a famous document-oriented database that is used by many modern-day web applications. Since MongoDB is a popular database for development, Docker has also ensured it has support for MongoDB.
我们现在将看到启动并运行 Docker 容器以下 MongoDB 的各个步骤。
We will now see the various steps for getting the Docker container for MongoDB up and running.
Step 1 − 第一步是从 Docker Hub 拉取镜像。当您登录 Docker Hub 时,您将能够搜索并看到 Mongo 的镜像,如下所示。只需在搜索框中输入 Mongo,然后单击搜索结果中出现的 Mongo(官方)链接。
Step 1 − The first step is to pull the image from Docker Hub. When you log into Docker Hub, you will be able to search and see the image for Mongo as shown below. Just type in Mongo in the search box and click on the Mongo (official) link which comes up in the search results.
Step 2 − 您将在 Docker Hub 的存储库详细信息中看到 Mongo 的 Docker pull 命令。
Step 2 − You will see that the Docker pull command for Mongo in the details of the repository in Docker Hub.
Step 3 − 在 Docker 主机上,使用上面所示的 Docker pull 命令从 Docker Hub 下载最新的 Mongo 镜像。
Step 3 − On the Docker Host, use the Docker pull command as shown above to download the latest Mongo image from Docker Hub.
Step 4 − 既然我们有了 Mongo 的镜像,让我们首先运行一个 MongoDB 容器,该容器将成为我们的 MongoDB 实例。为此,我们将执行以下命令 −
Step 4 − Now that we have the image for Mongo, let’s first run a MongoDB container which will be our instance for MongoDB. For this, we will issue the following command −
sudo docker run -it -d mongo
可以注意有关上述命令的以下几点 −
The following points can be noted about the above command −
-
The –it option is used to run the container in interactive mode.
-
The –d option is used to run the container as a daemon process.
-
And finally we are creating a container from the Mongo image.
然后,您可以发出 docker ps 命令以查看正在运行的容器 −
You can then issue the docker ps command to see the running containers −
注意以下几点 −
Take a note of the following points −
-
The name of the container is tender_poitras. This name will be different since the name of the containers keep on changing when you spin up a container. But just make a note of the container which you have launched.
-
Next, also notice the port number it is running on. It is listening on the TCP port of 27017.
Step 5 − 现在,我们启动另一个容器,该容器将起到客户端的作用,用于连接到 MongoDB 数据库。为此,我们发出以下命令:
Step 5 − Now let’s spin up another container which will act as our client which will be used to connect to the MongoDB database. Let’s issue the following command for this −
sudo docker run –it –link=tender_poitras:mongo mongo /bin/bash
可以注意有关上述命令的以下几点 −
The following points can be noted about the above command −
-
The –it option is used to run the container in interactive mode.
-
We are now linking our new container to the already launched MongoDB server container. Here, you need to mention the name of the already launched container.
-
We are then specifying that we want to launch the Mongo container as our client and then run the bin/bash shell in our new container.
现在你将进入新容器。
You will now be in the new container.
Step 6 − 在新容器中运行 env 命令,以查看如何连接到 MongoDB 服务器容器的详细信息。
Step 6 − Run the env command in the new container to see the details of how to connect to the MongoDB server container.
Step 6 − 现在,是从客户端容器连接到 MongoDB 服务器的时候了。我们可以通过以下命令执行此操作:
Step 6 − Now it’s time to connect to the MongoDB server from the client container. We can do this via the following command −
mongo 172.17.0.2:27017
需要注意有关上述命令的以下几点:
The following points need to be noted about the above command
-
The mongo command is the client mongo command that is used to connect to a MongoDB database.
-
The IP and port number is what you get when you use the env command.
一旦运行此命令,您将连接到 MongoDB 数据库。
Once you run the command, you will then be connected to the MongoDB database.
然后,你就可以在命令提示符下运行任何 MongoDB 命令。在我们的示例中,我们运行以下命令:
You can then run any MongoDB command in the command prompt. In our example, we are running the following command −
use demo
该命令是 MongoDB 命令,用于切换到数据库名称 demo 。如果数据库不可用,它将被创建。
This command is a MongoDB command which is used to switch to a database name demo. If the database is not available, it will be created.
现在,你已经成功地创建了一个客户端和服务器 MongoDB 容器。
Now you have successfully created a client and server MongoDB container.