官术网_书友最值得收藏!

Preparing  MongoDB

Let's create our MongoDB container. We will use the official image provided by the Docker Hub.

First, we need to pull the image:

docker pull mongo:3.4.10

Then, we will see the Docker Engine downloading the image contents.

To create an isolation from our containers, we will create a separated network for our application and database. The network should use the bridge driver to allow the container communications.

Let's create a docker network:

docker network create cms-application

The command output should be an ID of a created network. Your ID will probably be different compared to mine:

To check if the network was created successfully, the docker network ls command can help us.

We will start our MongoDB. The network should be cms-application, but we will map the database port to a host port. For debugging purposes, we will connect a client to a running database, but please don't do this in a non-development environment.

Exposing a port over host is not a best practice. Hence, we use a Docker container, which is one of the main advantages is process isolation. In this case, we will have no control over the network. Otherwise, we may cause some port conflicts.

To start, type the following command:

docker run -d --name mongodb --net cms-application -p 27017:27017 mongo:3.4.10
Also, we can stop the Docker MongoDB container using docker stop mongodb and start our container again by using the following command:  docker start mongodb.

The output will be a hash which represents the ID of the container.

The parameter instructions are:

  • -d: This instructs Docker to run the container in a background mode
  • --name: The container name; it will be a kind of hostname in our network
  • --net: The network where the container will be attached
  • -p: The host port and container port, which will be mapped to a container on a host interface

Now, we have a pretty standard MongoDB instance running on our machines, and we can start to add a persistence in our CMS application. We will do that soon.

主站蜘蛛池模板: 富平县| 临漳县| 宁阳县| 绩溪县| 永仁县| 封开县| 邓州市| 滕州市| 忻州市| 黑山县| 东阳市| 武城县| 杭州市| 阿合奇县| 遂宁市| 博客| 惠东县| 多伦县| 丰城市| 柞水县| 汽车| 烟台市| 重庆市| 汾西县| 盱眙县| 龙山县| 峨眉山市| 平顺县| 隆化县| 红安县| 大宁县| 赣榆县| 汾西县| 闽侯县| 鄂伦春自治旗| 岳池县| 长垣县| 江西省| 富裕县| 准格尔旗| 石河子市|