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-docker
docker -- pull marketplace.gcr.io/google/mongodb4
Consult 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: 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.
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
See 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 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
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.
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 -- mongo
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'
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/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.
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
See 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 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
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.
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 mongo
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'
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. |