docker-compose build mongodb container instance

    docker-compose can open multiple docker instances at once, which is much more convenient than Dockerfile to build docker containers. The main point of docker-compose is the configuration of yml files. The configuration of the yml file needs to pay attention to strictly control indentation.

    It should be noted that the docker-compose command is not installed with the docker installation. He needs additional installation. If it is not installed, you can download the corresponding system version on github: https://github.com/docker/compose / . If it is a Linux system, after downloading and decompressing, put the executable command docker-compose into the / usr / bin directory and give the executable attribute: chmod + x / usr / bin / docker-compose.

    The mongodb container construction is actually very simple, that is, you need to specify the source of the image. If you need to enable authentication, you need to configure the environment variables MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD. Generally, we want to externally map the disks in the image, so that even if the container exits, the data retained in the container will not be lost the next time it is started.

    The docker-compose configuration of mongodb is given below.

    docker-compose-mongodb.yml

version: '3'
services:
  mongodb:
    image: mongo:latest
    restart: always
    volumes:
      - /data/mongo/db:/data/db
      - /data/mongo/log:/var/log/mongodb
    ports:
      - 27018:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin

    The port mapping given here maps 27017 in the container to 27018, because mongodb is installed locally, in order to avoid port occupation conflicts, it is used 27018. 

    To start the container, we need to prepare the folders / data / mongo / db and / data / mongo / log.

    In the directory where the configuration file is located, we can execute the command to start the container.

    # docker-compose -f docker-compose-mongodb.yml up -d

    If mongo: latest does not exist in the local mirror list, it will be pulled from the remote warehouse, which will take some time.

    For such startup, we use the admin user and admin password as the initial authentication user and password by default. Generally in use, we will create a user, but the user needs to note that we need to switch to the admin database under use admin, and then authenticate db.auth ("admin", "admin"), and return 1, indicating that the authentication was successful . Next, we need to create a user for a database to use. At this time, we also need to switch to another database, such as mec, and the switch command is use mec. At this time, we create the hadoop user and specify the password through the command db.createUser ({user: 'hadoop', pwd: '123456', roles: [{role: 'dbOwner', db: 'mec'}]}).

    When creating a new user above, you must pay attention to switching to the database managed by the user. Otherwise, when we use it, an error may be reported, and the default is not to switch. The user we created is the managed admin database.

 

Published 529 original articles · praised 287 · 1.47 million views

Guess you like

Origin blog.csdn.net/feinifi/article/details/105098829