Dockerfile source for mongodb docker image.
This source repo was originally copied from: https://github.com/docker-library/mongo
This is not an official Google product.
This image contains an installation of MongoDB
For more information, see the Official Image Marketplace Page.
Configure gcloud as a Docker credential helper:
gcloud auth configure-dockerdocker -- pull marketplace.gcr.io/google/mongodb4Consult Launcher container documentation for additional information about setting up your Kubernetes environment.
This section describes how to spin up a MongoDB service using this image.
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: mongoRun 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 TCPFor 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.
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: 5GiRun 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 TCPSee the official docs for infomation on using and configuring MongoDB for things like replica sets and sharding.
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 wiredTigerRun 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 TCPYou 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 --helpMongoDB 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:
- --authRun 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 TCPOpen an admin CLI shell.
kubectl exec -it some-mongo -- mongo adminCreate 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.
This section describes how to use this image as a MongoDB client.
You can run a MongoDB client directly within the container.
kubectl exec -it some-mongo -- mongoAssume 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'Consult Launcher container documentation for additional information about setting up your Docker environment.
This section describes how to spin up a MongoDB service using this image.
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/mongodb4The 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.
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/dbOr 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/mongodb4See the official docs for infomation on using and configuring MongoDB for things like replica sets and sharding.
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 wiredTigerYou can also list all available options (several pages long).
docker run \
--name some-mongo-client \
--rm \
marketplace.gcr.io/google/mongodb4 \
--verbose --helpMongoDB 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 \
--authOpen an admin CLI shell.
docker exec -it some-mongo mongo adminCreate 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.
This section describes how to use this image as a MongoDB client.
You can run a MongoDB client directly within the container.
docker exec -it some-mongo mongoAssume 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'These are the ports exposed by the container image.
| Port | Description |
|---|---|
| TCP 27017 | Standard MongoDB port. |
These are the filesystem paths used by the container image.
| Path | Description |
|---|---|
| /data/db | Stores the database files. |