Skip to content

Commit 9b990ff

Browse files
committed
Dockerizing react client
1 parent e31a8da commit 9b990ff

File tree

8 files changed

+117
-0
lines changed

8 files changed

+117
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ before_install:
1313
- sudo mv docker-compose /usr/local/bin
1414

1515
before_script:
16+
- export REACT_APP_USERS_SERVICE_URL=http://127.0.0.1
1617
- docker-compose up -d --build
1718

1819
script:

docker-compose-prod.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,19 @@ services:
3333
restart: always
3434
ports:
3535
- 80:80
36+
depends_on:
37+
- users
38+
- client
39+
40+
client:
41+
container_name: client
42+
build:
43+
context: ./services/client
44+
dockerfile: Dockerfile-prod
45+
args:
46+
- NODE_ENV=production
47+
- REACT_APP_USERS_SERVICE_URL=${REACT_APP_USERS_SERVICE_URL}
48+
ports:
49+
- '3007:80'
3650
depends_on:
3751
- users

docker-compose.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,21 @@ services:
3535
restart: always
3636
ports:
3737
- 80:80
38+
depends_on:
39+
- users
40+
- client
41+
42+
client:
43+
build:
44+
context: ./services/client
45+
dockerfile: Dockerfile
46+
volumes:
47+
- './services/client:/usr/src/app'
48+
- '/usr/src/app/node_modules'
49+
ports:
50+
- 3007:3000
51+
environment:
52+
- NODE_ENV=development
53+
- REACT_APP_USERS_SERVICE_URL=${REACT_APP_USERS_SERVICE_URL}
3854
depends_on:
3955
- users

services/client/.dockerignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
node_modules
2+
coverage
3+
build
4+
env
5+
htmlcov
6+
.dockerignore
7+
Dockerfile
8+
Dockerfile-prod

services/client/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# base image
2+
FROM node:11.12.0-alpine
3+
4+
# set working directory
5+
WORKDIR /usr/src/app
6+
7+
# add `/usr/src/app/node_modules/.bin` to $PATH
8+
ENV PATH /usr/src/app/node_modules/.bin:$PATH
9+
10+
# install and cache app dependencies
11+
COPY package.json /usr/src/app/package.json
12+
COPY package-lock.json /usr/src/app/package-lock.json
13+
RUN npm ci
14+
RUN npm install [email protected] -g --silent
15+
16+
# start app
17+
CMD ["npm", "start"]

services/client/Dockerfile-prod

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
###########
2+
# BUILDER #
3+
###########
4+
5+
# base image
6+
FROM node:11.12.0-alpine as builder
7+
8+
# set working directory
9+
WORKDIR /usr/src/app
10+
11+
# install app dependencies
12+
ENV PATH /usr/src/app/node_modules/.bin:$PATH
13+
COPY package.json /usr/src/app/package.json
14+
COPY package-lock.json /usr/src/app/package-lock.json
15+
RUN npm ci
16+
RUN npm install [email protected] -g --silent
17+
18+
# set environment variables
19+
ARG REACT_APP_USERS_SERVICE_URL
20+
ENV REACT_APP_USERS_SERVICE_URL $REACT_APP_USERS_SERVICE_URL
21+
ARG NODE_ENV
22+
ENV NODE_ENV $NODE_ENV
23+
24+
# create build
25+
COPY . /usr/src/app
26+
RUN npm run build
27+
28+
29+
#########
30+
# FINAL #
31+
#########
32+
33+
# base image
34+
FROM nginx:1.15.9-alpine
35+
36+
# copy static files
37+
COPY --from=builder /usr/src/app/build /usr/share/nginx/html
38+
39+
# expose port
40+
EXPOSE 80
41+
42+
# run nginx
43+
CMD ["nginx", "-g", "daemon off;"]

services/nginx/dev.conf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ server {
33
listen 80;
44

55
location / {
6+
proxy_pass http://client:3000;
7+
proxy_redirect default;
8+
proxy_set_header Host $host;
9+
proxy_set_header X-Real-IP $remote_addr;
10+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
11+
proxy_set_header X-Forwarded-Host $server_name;
12+
}
13+
14+
location /users {
615
proxy_pass http://users:5000;
716
proxy_redirect default;
817
proxy_set_header Host $host;

services/nginx/prod.conf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ server {
33
listen 80;
44

55
location / {
6+
proxy_pass http://client:80;
7+
proxy_redirect default;
8+
proxy_set_header Host $host;
9+
proxy_set_header X-Real-IP $remote_addr;
10+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
11+
proxy_set_header X-Forwarded-Host $server_name;
12+
}
13+
14+
location /users {
615
proxy_pass http://users:5000;
716
proxy_redirect default;
817
proxy_set_header Host $host;

0 commit comments

Comments
 (0)