Skip to content

GoogleCloudPlatform/mongodb-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongodb-docker

Dockerfile source for mongodb docker image.

Upstream

This source repo was originally copied from: https://github.com/docker-library/mongo

Disclaimer

This is not an official Google product.

About

This image contains an installation of MongoDB

For more information, see the Official Image Marketplace Page.

Prerequisites

Configure gcloud as a Docker credential helper:

gcloud auth configure-docker

Pull command

docker -- pull marketplace.gcr.io/google/mongodb4

Table of Contents

Using Kubernetes

Consult Launcher container documentation for additional information about setting up your Kubernetes environment.

Run a MongoDB server

This section describes how to spin up a MongoDB service using this image.

Start a MongoDB instance

Copy the following content to pod.yaml file, and run kubectl create -f pod.yaml.

apiVersion: v1
kind: Pod
metadata:
  name: some-mongo
  labels:
    name: some-mongo
spec:
  containers:
    - image: marketplace.gcr.io/google/mongodb4
      name: mongo

Run the following to expose the port. Depending on your cluster setup, this might expose your service to the Internet with an external IP address. For more information, consult Kubernetes documentation.

kubectl expose pod some-mongo --name some-mongo-27017 \
  --type LoadBalancer --port 27017 --protocol TCP

For information about how to retain your database across restarts, see Use a persistent data volume.

See Configurations for how to customize your MongoDB service instance.

Use a persistent data volume

We can store MongoDB data on a persistent volume. This way the database remains intact across restarts.

Copy the following content to pod.yaml file, and run kubectl create -f pod.yaml.

apiVersion: v1
kind: Pod
metadata:
  name: some-mongo
  labels:
    name: some-mongo
spec:
  containers:
    - image: marketplace.gcr.io/google/mongodb4
      name: mongo
      volumeMounts:
        - name: data
          mountPath: /data/db
          subPath: data
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: data
---
# Request a persistent volume from the cluster using a Persistent Volume Claim.
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: data
  annotations:
    volume.alpha.kubernetes.io/storage-class: default
spec:
  accessModes: [ReadWriteOnce]
  resources:
    requests:
      storage: 5Gi

Run the following to expose the port. Depending on your cluster setup, this might expose your service to the Internet with an external IP address. For more information, consult Kubernetes documentation.

kubectl expose pod some-mongo --name some-mongo-27017 \
  --type LoadBalancer --port 27017 --protocol TCP

Configurations

See the official docs for infomation on using and configuring MongoDB for things like replica sets and sharding.

Using flags

You can specify options directly to mongod when starting the instance. For example, you can set --storageEngine to wiredTiger to enable WiredTiger storage engine.

A common use-case is adding the parameter --bind_ip_all to bind the container to all possible IPv4 addresses.

Check other parameters at mongod Reference.

Copy the following content to pod.yaml file, and run kubectl create -f pod.yaml.

apiVersion: v1
kind: Pod
metadata:
  name: some-mongo
  labels:
    name: some-mongo
spec:
  containers:
    - image: marketplace.gcr.io/google/mongodb4
      name: mongo
      args:
        - --storageEngine wiredTiger

Run the following to expose the port. Depending on your cluster setup, this might expose your service to the Internet with an external IP address. For more information, consult Kubernetes documentation.

kubectl expose pod some-mongo --name some-mongo-27017 \
  --type LoadBalancer --port 27017 --protocol TCP

You can also list all available options (several pages long).

kubectl run \
  some-mongo-client \
  --image marketplace.gcr.io/google/mongodb4 \
  --rm --attach --restart=Never \
  -- --verbose --help

Authentication and authorization

MongoDB does not require authentication by default, but it can be configured to do so by using --auth option.

Copy the following content to pod.yaml file, and run kubectl create -f pod.yaml.

apiVersion: v1
kind: Pod
metadata:
  name: some-mongo
  labels:
    name: some-mongo
spec:
  containers:
    - image: marketplace.gcr.io/google/mongodb4
      name: mongo
      args:
        - --auth

Run the following to expose the port. Depending on your cluster setup, this might expose your service to the Internet with an external IP address. For more information, consult Kubernetes documentation.

kubectl expose pod some-mongo --name some-mongo-27017 \
  --type LoadBalancer --port 27017 --protocol TCP

Open an admin CLI shell.

kubectl exec -it some-mongo -- mongo admin

Create a user some-user and set password as some-pass.

db.createUser({
  "user" : "some-user",
  "pwd" : "some-pass",
  "roles" : [
    {
      "role" : "userAdminAnyDatabase",
      "db" : "admin"
    }
  ]
});

For more information, see authentication and authorization sections on the official MongoDB documentation.

Mongo CLI

This section describes how to use this image as a MongoDB client.

Connect to a running MongoDB container

You can run a MongoDB client directly within the container.

kubectl exec -it some-mongo -- mongo

Connect to a remote MongoDB server

Assume that we have a MongoDB server running at some-host. We want to log on to some-db as some-user with some-pass as the password.

kubectl run \
  some-mongo-client \
  --image marketplace.gcr.io/google/mongodb4 \
  --rm --attach --restart=Never \
  -it \
  -- sh -c 'exec mongo some-host/some-db --username some-user --password some-pass --authenticationDatabase admin'

Using Docker

Consult Launcher container documentation for additional information about setting up your Docker environment.

Run a MongoDB server

This section describes how to spin up a MongoDB service using this image.

Start a MongoDB instance

Use the following content for the docker-compose.yml file, then run docker-compose up.

version: '2'
services:
  mongo:
    container_name: some-mongo
    image: marketplace.gcr.io/google/mongodb4
    ports:
      - '27017:27017'

Or you can use docker run directly:

docker run \
  --name some-mongo \
  -p 27017:27017 \
  -d \
  marketplace.gcr.io/google/mongodb4

The MongoDB server is accessible on port 27017.

For information about how to retain your database across restarts, see Use a persistent data volume.

See Configurations for how to customize your MongoDB service instance.

Use a persistent data volume

We can store MongoDB data on a persistent volume. This way the database remains intact across restarts. Assume that /my/persistent/dir/mongo is the persistent directory on the host.

Use the following content for the docker-compose.yml file, then run docker-compose up.

version: '2'
services:
  mongo:
    container_name: some-mongo
    image: marketplace.gcr.io/google/mongodb4
    ports:
      - '27017:27017'
    volumes:
      - /my/persistent/dir/mongo:/data/db

Or you can use docker run directly:

docker run \
  --name some-mongo \
  -p 27017:27017 \
  -v /my/persistent/dir/mongo:/data/db \
  -d \
  marketplace.gcr.io/google/mongodb4

Configurations

See the official docs for infomation on using and configuring MongoDB for things like replica sets and sharding.

Using flags

You can specify options directly to mongod when starting the instance. For example, you can set --storageEngine to wiredTiger to enable WiredTiger storage engine.

A common use-case is adding the parameter --bind_ip_all to bind the container to all possible IPv4 addresses.

Check other parameters at mongod Reference.

Use the following content for the docker-compose.yml file, then run docker-compose up.

version: '2'
services:
  mongo:
    container_name: some-mongo
    image: marketplace.gcr.io/google/mongodb4 \
    command:
      - --storageEngine wiredTiger
    ports:
      - '27017:27017'

Or you can use docker run directly:

docker run \
  --name some-mongo \
  -p 27017:27017 \
  -d \
  marketplace.gcr.io/google/mongodb4 \
  --storageEngine wiredTiger

You can also list all available options (several pages long).

docker run \
  --name some-mongo-client \
  --rm \
  marketplace.gcr.io/google/mongodb4 \
  --verbose --help

Authentication and authorization

MongoDB does not require authentication by default, but it can be configured to do so by using --auth option.

Use the following content for the docker-compose.yml file, then run docker-compose up.

version: '2'
services:
  mongo:
    container_name: some-mongo
    image: marketplace.gcr.io/google/mongodb4 \
    command:
      - --auth
    ports:
      - '27017:27017'

Or you can use docker run directly:

docker run \
  --name some-mongo \
  -p 27017:27017 \
  -d \
  marketplace.gcr.io/google/mongodb4 \
  --auth

Open an admin CLI shell.

docker exec -it some-mongo mongo admin

Create a user some-user and set password as some-pass.

db.createUser({
  "user" : "some-user",
  "pwd" : "some-pass",
  "roles" : [
    {
      "role" : "userAdminAnyDatabase",
      "db" : "admin"
    }
  ]
});

For more information, see authentication and authorization sections on the official MongoDB documentation.

Mongo CLI

This section describes how to use this image as a MongoDB client.

Connect to a running MongoDB container

You can run a MongoDB client directly within the container.

docker exec -it some-mongo mongo

Connect to a remote MongoDB server

Assume that we have a MongoDB server running at some-host. We want to log on to some-db as some-user with some-pass as the password.

docker run \
  --name some-mongo-client \
  --rm \
  -it \
  marketplace.gcr.io/google/mongodb4 \
  sh -c 'exec mongo some-host/some-db --username some-user --password some-pass --authenticationDatabase admin'

References

Ports

These are the ports exposed by the container image.

Port Description
TCP 27017 Standard MongoDB port.

Volumes

These are the filesystem paths used by the container image.

Path Description
/data/db Stores the database files.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published