Hosting and connecting to mongodb database

Hello there!

I have a simple application I’d like to deploy over here consisting of a react application, a node/express app and a mongodb database. The react app communicates with the express app, which works as an api, fetching data from the databse and returning it to the frontend app.

I have a docker-compose file for it (which will be written down below, with some irrelevant info ommitted), and the app works wonders locally, but from what I’ve gathered, in order to deploy it here in fly.io, I would have to deploy each component of the application as its own separate app.
This is no real issue when it comes to the react and express apps, I can just use their respective dockerfiles to launch the application and those two work wonders.
The issue is that the express app is seemingly unable to communicate with the mongodb container.

I created a dockerfile just to deploy the mongodb app itself, the contents of which are just the following:

FROM mongo:latest
EXPOSE 27017
CMD [ "mongod" ]

And the original docker-compose.yaml I’m trying to deploy:

version: '3'

services:
  client:
    build: ./client
    container_name: atlas-client
    ports:
      - "3000:3000"
  server:
    build: ./server
    container_name: atlas-server
    ports:
      - "9000:9000"
    volumes:
      ......
  db:
    image: mongo
    container_name: mongodb
    ports:
      - "27017:27017"

Does anyone more knowledgeable than me (it’s my first time deploying on an environment I can’t just docker-compose my way out of) know of a way to host this database?
I’m afraid I have wasted too much time trying to hammer this out all by myself :sweat_smile:

Have a nice evening!

Edit: for some extra context, I configured the ports correctly, and I am passing the hostname of each application as environment variables

Docker-compose runs multiple processes (in isolation) on a single machine. Fly.io runs the image produced by a Dockerfile on a single virtual machine. Different models require different approaches.

If you want to stay close to the docker-compose model, you will need to install mongodb in the same dockerfile you use for your application. Assuming you are starting with the Dockerfile that flyctl launch produces which starts with FROM debian:bullseye ..., you would need to follow the instructions here: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-debian/ ; since you won’t be needing sudo, the lines would look like:

RUN apt-get update; apt-get install -y wget gnupg
RUN wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | apt-key add -
RUN echo "deb http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0 main" > /etc/apt/sources.list.d/mongodb-org-6.0.list
RUN apt-get update; apt-get install -y mongodb-org

That’s step 1. Next you need to make sure that the mongodb databases are placed on a volume so that they survive restart. These instructions are pretty straightforward: Volumes · Fly Docs. For destination, you will need to match what the value dbPath in /etc/mongod.conf, which is /var/lib/mongodb.

The final step is starting multiple processes. You’ve undoubtedly already got a package.json with a list of “scripts”. Install an npm package called concurrently and add a start script that looks something like this:

"start": "NODE_ENV=production concurrently --kill-others-on-fail npm:client npm:server npm:db"

Add three more scripts, one for the client, server, and db.

This should get you up and running. There are other ways to configure things - for example to put different processes on different virtual machines and connect them via Private Networking · Fly Docs.