Skip to content
logo
Percona Server for MongoDB 5.0
Running Percona Server for MongoDB in a Docker Container
Initializing search
    percona/psmdb-docs
    percona/psmdb-docs
    • Home
    • Percona Server for MongoDB feature comparison
      • Overview
      • Installing Percona Server for MongoDB on Debian and Ubuntu
      • Installing Percona Server for MongoDB on Red Hat Enterprise Linux and CentOS
      • Installing Percona Server for MongoDB from binary tarball
      • Running Percona Server for MongoDB in a Docker Container
        • Connecting from another Docker container
        • Connecting with the mongo shell
        • Percona Memory Engine
        • Hot Backup
        • $backupCursor and $backupCursorExtend aggregation stages
        • Authentication overview
        • Enable SCRAM authentication
        • Set up LDAP authentication with SASL
        • Set up x.509 authentication and LDAP authorization
        • Set up Kerberos authentication
        • AWS IAM authentication
        • Setting up AWS IAM authentication
        • LDAP authorization
        • Set up LDAP authentication and authorization using NativeLDAP
        • Data at rest encryption
        • HashiCorp Vault integration
        • Using the Key Management Interoperability Protocol (KMIP)
        • Local key management using a keyfile
        • Migrating from key file encryption to HashiCorp Vault encryption
      • Auditing
      • Profiling Rate Limit
      • Log Redaction
      • Additional text search algorithm - ngram
      • Tune parameters
        • Upgrade from 4.4 to 5.0
        • Upgrading Percona Server for MongoDB
      • Uninstall Percona Server for MongoDB
      • Percona Server for MongoDB 5.0 Release Notes
      • Percona Server for MongoDB 5.0.15-13 (2023-03-16)
      • Percona Server for MongoDB 5.0.14-12 (2022-12-08)
      • Percona Server for MongoDB 5.0.13-11 (2022-10-12)
      • Percona Server for MongoDB 5.0.11-10 (2022-09-01)
      • Percona Server for MongoDB 5.0.10-9 (2022-08-09)
      • Percona Server for MongoDB 5.0.9-8 (2022-06-20)
      • Percona Server for MongoDB 5.0.8-7 (2022-05-10)
      • Percona Server for MongoDB 5.0.7-6 (2022-04-20)
      • Percona Server for MongoDB 5.0.6-5 (2022-02-10)
      • Percona Server for MongoDB 5.0.5-4 (2021-12-28)
      • Percona Server for MongoDB 5.0.4-3 (Release Candidate) (2021-12-08)
      • Percona Server for MongoDB 5.0.3-2 (Release Candidate) (2021-10-14)
      • Percona Server for MongoDB 5.0.2-1 (Release Candidate) (2021-08-16)
    • Glossary
    • Copyright and licensing information
    • Trademark policy

    • Connecting from another Docker container
    • Connecting with the mongo shell

    Running Percona Server for MongoDB in a Docker Container¶

    Docker images of Percona Server for MongoDB are hosted publicly on Docker Hub.

    For more information about using Docker, see the Docker Docs.

    Note

    Make sure that you are using the latest version of Docker. The ones provided via apt and yum may be outdated and cause errors.

    By default, Docker will pull the image from Docker Hub if it is not available locally.

    To run the latest Percona Server for MongoDB 5.0 in a Docker container, use the following command:

    Run this command as root or by using the sudo command

    $ docker run -d --name psmdb --restart always \
    percona/percona-server-mongodb:5.0
    

    The command does the following:

    • The docker run command instructs the docker daemon to run a container from an image.
    • The -d option starts the container in detached mode (that is, in the background).
    • The --name option assigns a custom name for the container that you can use to reference the container within a Docker network. In this case: psmdb.
    • The --restart option defines the container’s restart policy. Setting it to always ensures that the Docker daemon will start the container on startup and restart it if the container exits.
    • percona/percona-server-mongodb:5.0 is the name and version tag of the image to derive the container from.

    Connecting from another Docker container¶

    The Percona Server for MongoDB container exposes standard MongoDB port (27017), which can be used for connection from an application running in another container.

    For example, to set up a replica set for testing purposes, you have the following options:

    • Interconnect the mongod nodes in containers on a default bridge network. In this scenario, containers communicate with each other by their IP address.
    • Create a user-defined network and interconnect the mongod nodes on it. In this scenario, containers communicate with each other by name.
    • Automate the container provisioning and the replica set setup via the Docker Compose tool.

    In the following example, rs101, rs102, rs103 are the container names for Percona Server for MongoDB and rs is the replica set name.

    When you start Docker, a default bridge network is created and all containers are automatically attached to it unless otherwise specified.

    1. Start the containers and expose different ports

      $ docker run --rm -d --name rs101 -p 27017:27017  percona/percona-server-mongodb:5.0 --port=27017 --replSet rs
      $ docker run --rm -d --name rs102 -p 28017:28017  percona/percona-server-mongodb:5.0 --port=28017 --replSet rs
      $ docker run --rm -d --name rs103 -p 29017:29017  percona/percona-server-mongodb:5.0 --port=29017 --replSet rs
      
    2. Check that the containers are started

      $ docker container ls
      

      Output:

      CONTAINER ID  IMAGE                                         COMMAND               CREATED         STATUS             PORTS                     NAMES
      3a4b70cd386b  percona/percona-server-mongodb:5.0  --port=27017 --re...  3 minutes ago   Up 3 minutes ago   0.0.0.0:27017->27017/tcp  rs101
      c9b40a00e32b  percona/percona-server-mongodb:5.0  --port=28017 --re...  11 seconds ago  Up 11 seconds ago  0.0.0.0:28017->28017/tcp  rs102
      b8aebc00309e  percona/percona-server-mongodb:5.0  --port=29017 --re...  3 seconds ago   Up 3 seconds ago   0.0.0.0:29017->29017/tcp  rs103
      
    3. Get the IP addresses of each container

      $ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' rs101
      $ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' rs102
      $ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' rs103
      
    4. Interconnect the containers and initiate the replica set. Replace rs101SERVER, rs102SERVER and rs103SERVER with the IP address of each respective container.

      $ docker exec -ti rs101 mongo --eval 'config={"_id":"rs","members":[{"_id":0,"host":"rs101SERVER:27017"},{"_id":1,"host":"rs102SERVER:28017"},{"_id":2,"host":"rs103SERVER:29017"}]};rs.initiate(config);'
      
    5. Check your setup

      $ docker exec -ti rs101 mongo --eval 'rs.status()'
      

    You can isolate desired containers in a user-defined network and provide DNS resolution across them so that they communicate with each other by hostname.

    1. Create the network:

      $ docker network create my-network
      
    2. Start the containers and connect them to your network, exposing different ports

      $ docker run --rm -d --name rs101 --net my-network -p 27017:27017  percona/percona-server-mongodb:5.0 --port=27017 --replSet rs
      $ docker run --rm -d --name rs102 --net my-network -p 28017:28017  percona/percona-server-mongodb:5.0 --port=28017 --replSet rs
      $ docker run --rm -d --name rs103 --net my-network -p 29017:29017  percona/percona-server-mongodb:5.0 --port=29017 --replSet rs
      

      Alternatively, you can connect the already running containers to your network:

      $ docker network connect my-network rs101 rs102 rs103
      
    3. Interconnect the containers and initiate the replica set.

      $ docker exec -ti rs101 mongo --eval 'config={"_id":"rs","members":[{"_id":0,"host":"rs101:27017"},{"_id":1,"host":"rs102:28017"},{"_id":2,"host":"rs103:29017"}]};rs.initiate(config);'
      
    4. Check your setup

      $ docker exec -ti rs101 mongo --eval 'rs.status()'
      

    As the precondition, you need to have Docker Engine and Docker Compose on your machine. Refer to Docker documentation for how to get Docker Compose.

    1. Create a compose file and define the services in it.

      docker-compose.yaml
      version: "3"
      services:
        rs101:
          image: percona/percona-server-mongodb:5.0
          container_name: rs101
          hostname: rs101
          ports:
            - "27017:27017"
          networks:
            - my-network
          command: "--port=27017 --replSet rs"
      
        rs102:
          image: percona/percona-server-mongodb:5.0
          container_name: rs102
          hostname: rs102
          ports:
            - "28017:28017"    
          networks:
            - my-network
          command: "--port=28017 --replSet rs"
      
        rs103:
          image: percona/percona-server-mongodb:5.0
          container_name: rs103
          hostname: rs103
          ports:
            - "29017:29017"    
          networks:
            - my-network
          command: "--port=29017 --replSet rs"
      
        rs-init:
          image: percona/percona-server-mongodb:5.0
          container_name: rs-init
          restart: "no"
          networks:
            - my-network
          depends_on:
            - rs101
            - rs102
            - rs103
          command: >
            mongo --host rs101:27017 --eval 
            '
            config = {
            "_id" : "rs",
            "members" : [
              {
                "_id" : 0,
                "host" : "rs101:27017"
              },
              {
                "_id" : 1,
                "host" : "rs102:28017"
              },
              {
                "_id" : 2,
                "host" : "rs103:29017"
              }
            ]
            };
            rs.initiate(config);
            ' 
      networks:
        my-network:
          driver: bridge
      
    2. Build and run the replica set with Compose

      $ docker compose up -d
      
    3. Check your setup

      $ docker exec -ti rs101 mongo --eval 'rs.status()'
      

    Connecting with the mongo shell¶

    To start another container with the mongo shell that connects to your Percona Server for MongoDB container, run the following command:

    $ docker run -it --rm percona/percona-server-mongodb:5.0 mongo mongodb://MONGODB_SERVER:PORT/DB_NAME
    

    Set MONGODB_SERVER, PORT, and DB_NAME with the IP address of the psmdb container, the port of your MongoDB Server (default value is 27017), and the name of the database you want to connect to.

    You can get the IP address by running this command:

    $ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' psmdb
    

    Contact Us

    For free technical help, visit the Percona Community Forum.

    To report bugs or submit feature requests, open a JIRA ticket.

    For paid support and managed or consulting services , contact Percona Sales.


    Last update: March 31, 2023
    Created: December 8, 2022
    Percona LLC and/or its affiliates, © 2023
    Made with Material for MkDocs

    Cookie consent

    We use cookies to recognize your repeated visits and preferences, as well as to measure the effectiveness of our documentation and whether users find what they're searching for. With your consent, you're helping us to make our documentation better.