Skip to content

Commit cf4d804

Browse files
committed
add docker-compose, docker client with nginx
1 parent 5f5cb4b commit cf4d804

File tree

14 files changed

+142
-64
lines changed

14 files changed

+142
-64
lines changed

client/.dockerignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
node_modules
2-
.env
32
npm-debug
43
build

client/Dockerfile

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
FROM node:12-alpine as build
22

3-
RUN mkdir /app/client
4-
WORKDIR /app/client
5-
COPY package.json /app/client/package.json
6-
RUN npm install --only=prod
7-
COPY . /app/client/
3+
WORKDIR /app
4+
#copy files client to container
5+
COPY . /app/
6+
7+
# prepare for build
8+
RUN npm install --silent
89
RUN npm run build
910

10-
FROM nginx:1.16.0-alpine
11+
# prepare nginx
12+
FROM nginx
1113
RUN rm /etc/nginx/conf.d/default.conf
12-
COPY nginx.conf /etc/nginx/nginx.conf
13-
COPY --from=build /app/client/build /usr/share/nginx/html
14+
COPY nginx.conf /etc/nginx/conf.d/
15+
COPY --from=build app/build /usr/share/nginx/html
16+
17+
# start nginx
1418
EXPOSE 80
1519
CMD ["nginx", "-g", "daemon off;"]

client/nginx.conf

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
events {
2-
worker_connections 768;
3-
# multi_accept on;
4-
}
51

6-
http {
2+
server {
3+
listen 80;
74

8-
server {
9-
listen 80 default_server;
5+
location / {
6+
root /usr/share/nginx/html;
7+
index index.html index.htm index.nginx-debian.html;
8+
try_files $uri $uri/ /index.html;
9+
}
1010

11-
root /usr/share/nginx/html;
11+
location /api {
12+
proxy_pass http://server:5500;
13+
}
1214

13-
# Add index.php to the list if you are using PHP
14-
index index.html index.htm index.nginx-debian.html;
15+
error_page 500 502 503 504 /50x.html;
16+
17+
location = /50x.html {
18+
root usr/share/nginx/html;
19+
}
20+
}
21+
1522

16-
location / {
17-
try_files $uri /index.html;
18-
}
19-
20-
location /welcome {
21-
proxy_pass http://server:5000;
22-
}

client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"react-scripts": "3.4.1",
2020
"typescript": "^3.7.5"
2121
},
22-
"proxy": "http://localhost:5000",
22+
"proxy": "http://localhost:5500",
2323
"scripts": {
2424
"start": "react-scripts start",
2525
"build": "react-scripts build",

client/src/App.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
color: white;
2525
}
2626

27+
.navbar > * {
28+
padding-left: 1rem;
29+
}
30+
2731
.App-link {
2832
color: #61dafb;
2933
}

client/src/App.tsx

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
import React, { useEffect, useState } from 'react';
2-
import logo from './logo.svg';
3-
import axios from 'axios';
4-
1+
import React from 'react';
2+
import Home from './containers/Home';
3+
import Test from './containers/Test';
4+
import Navbar from './components/Navbar';
5+
import { Switch, Route, BrowserRouter } from 'react-router-dom';
56
import './App.css';
67

78
function App() {
8-
const [welcomeMsg, setWelcomeMsg] = useState<string>('');
9-
10-
useEffect(() => {
11-
(async () => {
12-
const res = await axios.get('/api/start');
13-
setWelcomeMsg(res.data.msg);
14-
})();
15-
}, []);
16-
179
return (
1810
<div className="App">
19-
<header className="App-header">
20-
<img src={logo} className="App-logo" alt="logo" />
21-
<p>{welcomeMsg}</p>
22-
</header>
11+
<BrowserRouter>
12+
<Navbar />
13+
<Switch>
14+
<Route exact path="/" component={Home} />
15+
<Route path="/test" component={Test} />
16+
</Switch>
17+
</BrowserRouter>
2318
</div>
2419
);
2520
}

client/src/components/Navbar.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
import { Link } from 'react-router-dom';
3+
4+
const Navbar = () => {
5+
return (
6+
<div
7+
style={{
8+
height: '64px',
9+
width: '100%',
10+
position: 'fixed',
11+
top: 0,
12+
left: 0,
13+
display: 'flex',
14+
alignItems: 'center',
15+
justifyContent: 'flex-start',
16+
paddingLeft: '1rem',
17+
background: 'white',
18+
boxShadow: '0 0 8px 3px rgba(0,0,0,0.2)'
19+
}}
20+
className="navbar"
21+
>
22+
<Link to="/">Home</Link>
23+
<Link to="/test">Test React Router</Link>
24+
</div>
25+
);
26+
};
27+
28+
export default Navbar;

client/src/containers/Home.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React, { useEffect, useState } from 'react';
2+
import axios from 'axios';
3+
4+
const Home = () => {
5+
const [welcomeMsg, setWelcomeMsg] = useState<string>('');
6+
7+
useEffect(() => {
8+
(async () => {
9+
const res = await axios.get('/api/start');
10+
setWelcomeMsg(res.data.msg);
11+
})();
12+
}, []);
13+
14+
return (
15+
<div className="App-header">
16+
<p>{welcomeMsg}</p>
17+
</div>
18+
);
19+
};
20+
21+
export default Home;

client/src/containers/Test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
3+
const Test = () => {
4+
return (
5+
<div
6+
style={{
7+
height: '100vh',
8+
background: 'lightgreen',
9+
display: 'flex',
10+
alignItems: 'center',
11+
justifyContent: 'center',
12+
fontSize: '2rem'
13+
}}
14+
>
15+
This is A router Test
16+
</div>
17+
);
18+
};
19+
20+
export default Test;

docker-compose.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
version: '2'
1+
version: '3'
22
services:
33
server:
44
build: ./server
5-
ports:
6-
- '5000:5000'
7-
restart: always
5+
networks:
6+
- webapp
87
container_name: server
98
client:
109
build: ./client
10+
networks:
11+
- webapp
1112
ports:
1213
- '80:80'
13-
restart: always
1414
container_name: client
15+
16+
networks:
17+
webapp:
18+
driver: bridge

0 commit comments

Comments
 (0)