Express.JS + mongoDB + mongo-express on the docker compose

![docker.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/88d75605-1244-2b3f-0eb7-e0af79766066.png)


# STEP1, Create a Express.js App

```terminal:terminal

    // create directory and initialize app
        mkdir express-mongo
        cd express-mongo    
        npm init -y
    
    // create file necessary
        touch server.js
        touch Dockerfile
        touch .dockerignore

    // install modules
        npm install express mongodb
 
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/d59e53fc-2d3f-f84a-6b43-ecc7ac42df6f.png)

### server.js
```javascript:server.js
    const express = require("express");
    const app = express();
    const PORT = 3000;
    const { MongoClient } = require("mongodb");
    
    app.use(express.urlencoded({ extended: true }));
    app.use(express.json());
    
    const uri = "mongodb://root:password@mongodb:27017";
    const client = new MongoClient(uri);
    
    const options = {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    };
    
    app.get("/", async (req, res) => {
      try {
        await client.connect();
        const db = client.db("user-account");
        const collection = db.collection("users");
        const result = await collection.findOne({ userid: 1 }, options);
    
        res.json(result);
      } catch (error) {
        console.log(error);
      } finally {
        await client.close();
      }
    });
   
    app.listen(PORT, () => {
      console.log(`Server is running on port ${PORT}`);
    });
    
```

### Dockerfile
```Dockerfile:Dockerfile

    FROM node:alpine3.18
    
    WORKDIR /usr/src/app
    
    COPY . /usr/src/app/
    
    RUN npm install
    
    EXPOSE 3000
    
    CMD [ "node", "server.js" ]
    
```

### .dockerignore
```.dockerignore
    node_modules
```

# STEP2, Create a docker-compose.yml 

```yml:docker-compose.yml

    version: "3.9"
    
    services:
      node:
        build: .
        container_name: express_app
        ports:
          - 3000:3000
        restart: always
        volumes:
          - ./:/usr/src/app
        networks:
          - mongo-network
        depends_on:
          - mongodb
    
      mongodb:
        image: mongo
        container_name: mongo_db
        restart: always
        ports:
          - 27017:27017
        environment:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: password
        volumes:
          - mongo_db:/data/db
          - config_db:/data/configdb
        networks:
          - mongo-network
    
      mongo-express:
        image: mongo-express
        container_name: mongo_express
        restart: always
        ports:
          - 8081:8081
        environment:
          ME_CONFIG_OPTIONS_EDITORTHEME: ambiance
          ME_CONFIG_MONGODB_ADMINUSERNAME: root
          ME_CONFIG_MONGODB_ADMINPASSWORD: password
          ME_CONFIG_MONGODB_SERVER: mongodb
        networks:
          - mongo-network
        depends_on:
          - mongodb
    
    networks:
      mongo-network:
    
    volumes:
      mongo_db:
      config_db:
  
```

# STEP3, docker compose up -d

```terminal:terminal
    docker compose up -d
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/02d1f53d-712a-2aaf-fee7-63f2ff158a93.png)

### all container is running successfully
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/5752d799-1304-56c1-cbdf-cc6af2eff50b.png)

# STEP4, insert data to mongoDB

open http://localhost:8081

### create database "user-account"
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/b7cedbe6-ef33-ae46-70ec-0209b38e555d.png)

### create collection "users"
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/9eb24978-5d74-cb76-aec4-f8461f246a4d.png)

# STEP5, Access to http://localhost:3000
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/218f769e-9b4c-ee13-6300-2180d4da71e4.png)

### you can see the data from mongodb successfully!!

# STEP6, Down docker compose
```terminal:terminal
    
    docker compose down

```