Necessary services

The required files are provided for editing. The changes you need to make in them are also explained in this lesson.

Three services are needed in our main Compose file, namely, mongodb, nodejs, nginx.

Docker network:

Connect the three containers/services with a Docker network named quiznet.

MongoDB database (mongodb container)

Add the following configurations under the service of mongodb in docker-compose.yml:

  1. Define the variables for accessing the database with the user ID quizuser and password quizpass.
  2. Specify the image, name of the container, and network. Also, expose the default port of MongoDB.
  3. Specify the Docker volume, quizdata, for question storage. The folders /data/db should be mounted to quizdata.
  4. Also add logging property with driver set as none.

This completes our setting up of mongodb.

Node.js application (nodejs container)

Making Dockerfile for nodejs

Specify the Dockerfile commands that you need for your Node application in /nodejs/nodejs.Dockerfile. The Docker file should include the commands for specifying the image, environment variables, and work directories. There should also be commands for setting the user, copying required files, installing application modules, building npm, sharing the volume, and exposing the port of 8000. Finally, there should be a command to launch the application

πŸ“Œ You can add the following in addition to the commands added:

##environment variables
ENV HOME=/home/node/app
ENV PATH=${PATH}:${HOME}/node_modules/.bin
#share volume
VOLUME ${HOME}/static

πŸ“Œ We are building Dockerfile for development purposes. Make sure to set the required environment variables.

Adding nodejs to Compose

Add the following configurations under the service of nodejs in docker-compose.yml:

  1. Add the environment variable, name of the container, dependencies, and network. Specify the command for the npm, and expose the required ports.

  2. Specify the Dockerfile to be used and the following volumes:

    - ./nodejs:/home/node/app
    - nodejsfiles:/home/node/app/static
    - nodejsfiles:/home/node/app/.config
    - nodejsfiles:/home/node/app/.npm
    - nodejsfiles:/home/node/app/node_modules
    

This completes our setting up of nodejs.

NGINX reverse proxy (nginx container)

An NGINX web server container will be used to serve files from the shared static volume. This is more efficient than serving via Express.js as NGINX is faster. The NGINX is prefered for its performance and is also easy to configure.

Other HTTP requests (such as the /question endpoint) are forwarded to the nodejs container. NGINX will act as a reverse proxy.reverseProxy

Making Dockerfile for NGINX

Specify the Dockerfile commands that you need for your Node application in /nginx/nginx.Dockerfile. Include an image, copy the configuration file to the /etc/nginx/nginx.conf in the container, and expose its default port.

Adding nginx to Compose

Add the following configurations under the service of nginx in docker-compose.yml:

  1. Specify the Dockerfile to be used, name of the container, dependency, network, and ports.
  2. Also add logging property with driver set as none.

Get hands-on with 1400+ tech skills courses.