To run multi container Docker applications, you can utilize a docker-compose.yml
file which defines and runs the necessary services with a single command. Both Linux and Windows programs can be executed in Docker
, creating lightweight virtual environments for your apps.
Here is an illustration of the desired folder structure:
|--client
|--Dockerfile
|--components
|--index.js
|--server
|--Dockerfile
|--index.js
|--nginx
|-- Dockerfile
|--default.conf
|--docker.compose.yml
The Dockerfile
for the react client:
FROM node:alpine as builder
WORKDIR '/app'
COPY ./package.json . /
RUN npm install
COPY . .
RUN npm run build
The Dockerfile
for nginx:
FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf
The default configuration default.conf
for nginx:
upstream client {
server client:3000;
}
upstream api {
server api:5000;
}
server {
listen 80;
location / {
proxy_pass http://client;
}
location /sockjs-node {
proxy_pass http://client;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
location /api {
rewrite /api/(.*) /$1 break;
proxy_pass http://api;
}
}
The Dockerfile
for the node/express server:
FROM node:alpine
WORKDIR '/app'
COPY ./package.json ./
RUN npm install
COPY . .
CMD [ "npm", "run", "start" ]
Your docker-compose.yml
file allows flexibility to switch databases for your API. Building and running everything is achieved by executing docker-compose up --build
from the project's main directory (where the docker-compose
file is located).
version: '3'
services:
nginx:
restart: always
build:
dockerfile: Dockerfile
context: ./nginx
ports:
- '80:80'
mongo:
container_name: mongo
image: mongo
ports:
- '27017:27017'
api:
restart: always
build:
dockerfile: Dockerfile
context: ./server
volumes:
- /app/node_modules
- ./server:/app
links:
- mongo
ports:
- '5000:5000'
depends_on:
- mongo
client:
build:
dockerfile: Dockerfile
context: ./client
volumes:
- /app/node_modules
- ./client:/app
links:
- api