This is a Docker compose exercise made for the PXL Docker Compose course.
A node.js REST api based on mongoose, which requires a mongodb instance.
MONGO_URL=mongodb://db:27017/mongo-test
The api server will search for a mongodb server with the host address 'db' and access it on port 27017.
The name of the api database is mongo-test and should not be changed.
APP_PORT=80
The REST api will be served on port 80.
A react-based UI will look for the backend api on http://localhost:8080. The UI itself will be served to the user on port 80 by ngnix.
Create a Docker compose file and make sure that:
- the application is running on docker compose and the UI is accessible through http://localhost:8880.
- the startup sequence is as follows:
- mongodb server
- backend server
- frontend server
- the mongodb data is stored on a docker volume named 'data'.
- environment variables are read from a file.
- two separate networks exists so the frontend server cannot reach the database server directly.
The backend service starts only when the mongodb database service is up and running.
Tip: docker compose healthcheck
Show real-time container statistics (CPU, memory, network, filesystem) on http://localhost:9999
Your task is to create a Dockerfile to containerize a React-based frontend application. The application has the following requirements:
-
Development and Build Phase:
- Use a lightweight Node.js image (
node:12.2.0-alpine) as the base image for building the application. - Set the working directory to
/app. - Add the local
package.jsonto the container. - Use the npm package manager to:
- Install dependencies silently with
npm install --silent. - Globally install
react-scriptsversion3.0.1withnpm install react-scripts@3.0.1 -g --silent.
- Install dependencies silently with
- Copy all local project files to the container.
- Use the
npm run buildcommand to generate a production-ready build.
- Use a lightweight Node.js image (
-
Production Phase:
- Use a lightweight Nginx image (
nginx:1.16.0-alpine) as the base image to serve the React build. - Copy the build output from the development stage (
/app/build) to the Nginx HTML directory (/usr/share/nginx/html). - Expose port
80for serving the UI. - Use Nginx’s default executable with the command
nginx -g "daemon off;"to run the server.
- Use a lightweight Nginx image (
-
Application Behavior:
- The frontend UI should be accessible on port
80. - The React UI should make API requests to the backend located at
http://localhost:8080.
- The frontend UI should be accessible on port
Your goal is to write a multi-stage Dockerfile that accomplishes the above. Make sure your Dockerfile is efficient and only includes the necessary files in the final production image.
npm: For dependency installation and building the React application.nginx: For serving the built frontend application.
Your task is to create a Dockerfile to containerize a Node.js-based backend application. The application has the following requirements:
-
Base Image:
- Use the
node:8image as the base for the container.
- Use the
-
Application Setup:
- Set the working directory inside the container to
/usr/src/app. - Copy the
package.jsonandpackage-lock.jsonfiles (if present) to the container's working directory. - Use the npm package manager to install the application dependencies with
npm install.
- Set the working directory inside the container to
-
Source Code:
- Copy the entire application source code from the local machine to the container's working directory.
-
Port Exposure:
- Expose port
8080for the application to be accessible.
- Expose port
-
Start Command:
- Use the command
npm startto launch the application inside the container.
- Use the command