Setup: Nodejs / MongoDB

This demonstrates how one can configure a service running a Nodejs webserver and connects to MongoDB.

It also demonstrates how to copy data to a database with two steps. The first step's result is cached so that it does not need to run the next time this is started.

See the complete example code on GitHub


path: app
public: true
port: 8080
cmd: node /app.js
- mongo
- MONGODB_URL=mongodb://mongo:27017/test
image: mongo:4.0
- name: mongodump
path: mongodump
out: /dump
cache: true
- name: load
service: mongo
- mongodump:/
cmd: mongorestore /dump

From this configuration, we can see the following:

  • In this configuration, we have two services, "app" and "mongo". We also have two steps used to setup the staging environment.

  • About the service "app":

    • It will run a web server listening on port 8080.

    • The code is defined in the directory "app" where there is also a Dockerfile. The name of the Dockerfile is just "Dockerfile" so there is no need to specify the path to the file.

    • The option public is set to true so that it is possible to reach this server from the web.

    • It is also dependent on the service "mongo" and will not start before that service is started.

    • The service "app" has an environment variable called MONGODB_URL which is used to connect to the mongo service. The host "mongo" in the URL is used to connect to the mongo service. Services can connect to each other using their name as the host.

  • About the service "mongo"

    • It is using an existing image so it does not need to be built.

  • About the step "mongodump"

    • It is not refering to a service or image. Instead, its code is defined under the specified path ./mongodump where there is also a Dockerfile with the name "Dockerfile".

    • When running the default command specified in the Dockerfile, it will output files in a directory at path /dump.

    • The output will be cached so that this step does not need to run every time this staging environment is created.

  • About the step "load"

    • It is running a command on the service "mongo". Because of this, it is implicitly dependent on that service to run and we don't need to specify this using "depends_on". It will therefore run after the service "mongo" is started.

    • It will copy the output from the step "mongodump" and place them in its root directory.

    • After the files are copied, it will run the specified command to populate the MongoDB database.