# Deploying 3-Tier Application on Docker | Containerizing NodeJs, Mongo Express, MongoDB

[https://gitlab.com/nanuchi/developing-with-docker](https://gitlab.com/nanuchi/developing-with-docker)

#### To start the application

Step 1: Create a docker network

```plaintext
docker network create mongo-network
```

Step 2: start MongoDB

```plaintext
docker run -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password --name mongodb --net mongo-network mongo
```

Step 3: start mongo-express

```plaintext
docker run -d -p 8081:8081 -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=password --net mongo-network --name mongo-express -e ME_CONFIG_MONGODB_SERVER=mongodb mongo-express
```

*NOTE: creating docker-network in optional. You can start both containers in a default network. In this case, just emit* `--net` flag in `docker run` command

Step 4: open mongo-express from browser

```plaintext
http://ip address :8081
```

`user-account` *db* `users`*collection*

1. Create a Dockerfile with the content you provided:
    
    ```plaintext
    FROM node:13-alpine
    
    ENV MONGO_DB_USERNAME=admin \
        MONGO_DB_PWD=password
    
    RUN mkdir -p /home/app
    
    COPY ./app /home/app
    
    WORKDIR /home/app
    
    RUN npm install
    EXPOSE 3000
    
    CMD ["node", "server.js"]
    ```
    
2. Save the Dockerfile in a directory along with your application code. Let's assume the directory is named "my-app".
    
3. Open a terminal or command prompt and navigate to the directory containing the Dockerfile and your application code ("my-app" directory).
    
4. Build the Docker image using the following command:
    
    ```plaintext
    sudo docker build -t my-app-image .
    ```
    
    This command will build the Docker image with the tag "my-app-image". The dot (.) at the end specifies the build context as the current directory.
    
5. Once the image is successfully built, you can run a container from it and connect it to the "mongo-network" network using the following command:
    
    ```plaintext
    docker run -d --name my-app-container --net mongo-network -p 3000:3000 my-app-image
    ```
    
    This command starts a container named "my-app-container" from the "my-app-image" image, connects it to the "mongo-network" network, and maps port 3000 of the container to port 3000 on the host machine.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687954336435/564a1d73-b6d8-4e64-890a-ee4041f3c738.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687954440676/ed415aeb-a916-4d65-950b-0b9c1cf1f050.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687954508817/b2c8d9b1-8335-4c62-a16f-0f3cc6b36882.png align="center")
