Skip to content

Commit 2bc4ef3

Browse files
committed
Add Dockerfiles and development Docker Compose script
1 parent a654c65 commit 2bc4ef3

File tree

13 files changed

+147
-13
lines changed

13 files changed

+147
-13
lines changed

client/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

client/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ---------------------------------------------------------------------------- #
2+
# Stage 1 (Build) #
3+
# ---------------------------------------------------------------------------- #
4+
FROM node:18-alpine AS builder
5+
6+
WORKDIR /app
7+
8+
COPY package.json .
9+
RUN yarn install
10+
11+
COPY . .
12+
13+
RUN ["yarn", "build-docker-compose"]
14+
15+
# ---------------------------------------------------------------------------- #
16+
# Stage 2 (Serve) #
17+
# ---------------------------------------------------------------------------- #
18+
19+
FROM nginx:1.23-alpine
20+
21+
COPY nginx.conf /etc/nginx/nginx.conf
22+
COPY --from=builder /app/build /app/build

client/nginx.conf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# It's necessary to have an `events` directive, even if it's just empty.
2+
# Source: https://stackoverflow.com/questions/54481423/nginx-startup-prompt-emerg-no-events-section-in-configuration.
3+
events {}
4+
http {
5+
server {
6+
root /app/build;
7+
index index.html;
8+
9+
location / {
10+
# This `include` directive is necessary for correctly loading CSS
11+
# files.
12+
# Source: https://stackoverflow.com/questions/10075304/nginx-fails-to-load-css-files.
13+
include /etc/nginx/mime.types;
14+
try_files $uri /index.html;
15+
}
16+
}
17+
}

client/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545
"socket.io-client": "^2.3.0"
4646
},
4747
"scripts": {
48-
"start": "NODE_ENV=development react-scripts start",
49-
"build": "NODE_ENV=production react-scripts build",
48+
"start": "REACT_APP_ENVIRONMENT=development react-scripts start",
49+
"build": "REACT_APP_ENVIRONMENT=production react-scripts build",
50+
"build-docker-compose": "REACT_APP_ENVIRONMENT=docker react-scripts build",
5051
"test": "react-scripts test",
5152
"eject": "react-scripts eject"
5253
},

client/src/.env

Whitespace-only changes.

client/src/constants/api-routes.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ export const PORT = 5000;
33
// Change back to "/api" on deployment. Change IP address between
44
// http://localhost:5000/api and https://techsuite.dev/api on dev/deployment
55
// export const BASE_URL = "https://techsuite.dev/api";
6-
export const BASE_URL = process.env.NODE_ENV === 'production' ? "https://techsuite.dev/api" : `http://localhost:${PORT}/api`;
6+
// export const BASE_URL = `http://localhost:${PORT}/api`;
7+
export const BASE_URL = process.env.REACT_APP_ENVIRONMENT === 'production' ? "https://techsuite.dev/api" : `http://localhost:${PORT}/api`;
78

89
export const GLOBAL_SOCKET_NAMESPACE = "/ts-socket";
910
export const PORT_EXT = `:${PORT}`; // Change back to empty string on deployment
1011

1112
// Change from: http:// window.location.hostname + PORT_EXT + GLOBAL_SOCKET_NAMESPACE
1213
// to: https://techsuite.dev` + GLOBAL_SOCKET_NAMESPACE
1314
// export const SOCKET_URI = `http://` + window.location.hostname + PORT_EXT + GLOBAL_SOCKET_NAMESPACE;
14-
export const SOCKET_URI = process.env.NODE_ENV === 'production' ?
15-
`https://techsuite.dev` + GLOBAL_SOCKET_NAMESPACE
15+
export const SOCKET_URI = process.env.REACT_APP_ENVIRONMENT === 'production'
16+
? `https://techsuite.dev` + GLOBAL_SOCKET_NAMESPACE
1617
: `http://` + window.location.hostname + PORT_EXT + GLOBAL_SOCKET_NAMESPACE;

docker-compose.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This docker-compose file is meant to be run for development and serves as a
2+
# guideline for a production deployment.
3+
version: '3.8'
4+
5+
# Techsuite consists of 3 services:
6+
# 1. React client at port 3000.
7+
# 2. Flask REST API and websocket server at port 5000.
8+
# 3. PostgreSQL database server at port 4000.
9+
services:
10+
client:
11+
build:
12+
context: client
13+
dockerfile: Dockerfile
14+
ports:
15+
- 3000:80
16+
db:
17+
build:
18+
context: server
19+
dockerfile: Dockerfile.db
20+
environment:
21+
# These credentials are used for creating the db superuser. They're not
22+
# the credentials we use from the API side.
23+
POSTGRES_PASSWORD: 1989
24+
POSTGRES_USER: admin
25+
ports:
26+
- 5432:5432
27+
volumes:
28+
- db-data:/var/lib/postgres/data
29+
api:
30+
build:
31+
context: server
32+
dockerfile: Dockerfile.api
33+
depends_on:
34+
- db
35+
environment:
36+
PORT: 5000
37+
DATABASE_URI: postgresql://tim:1989@db:5432/techsuite
38+
BASE_URI: http://localhost:5000/api
39+
ports:
40+
- 5000:5000
41+
volumes:
42+
db-data:

server/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
venv

server/Dockerfile.api

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ----------------------------------- Setup ---------------------------------- #
2+
FROM python:3.10-alpine
3+
4+
WORKDIR /api
5+
6+
COPY requirements.txt .
7+
8+
# There are issues with installing psycopg2 on Alpine Linux (and other Linux
9+
# distributions).
10+
# See a good discussion of dependency issues: https://github.com/psycopg/psycopg2/issues/684.
11+
RUN apk update \
12+
&& apk add postgresql-dev gcc python3-dev musl-dev \
13+
&& pip install psycopg2 \
14+
&& pip install gunicorn \
15+
&& pip install -r requirements.txt
16+
17+
COPY . .
18+
19+
# -------------------------------- Production -------------------------------- #
20+
# Create all tables
21+
RUN echo "create_all()" | python3 -i src/db_manage.py 2> /dev/null
22+
23+
# Using Gunicorn and gevent-websocket server.
24+
# See:
25+
# - https://flask.palletsprojects.com/en/2.2.x/deploying/gunicorn/.
26+
# - https://stackoverflow.com/questions/9444405/gunicorn-and-websockets.
27+
WORKDIR /api/src
28+
29+
30+
CMD gunicorn -k "geventwebsocket.gunicorn.workers.GeventWebSocketWorker" -b 0.0.0.0:5000 server:app

server/Dockerfile.db

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM postgres:14-alpine
2+
3+
EXPOSE 4000
4+
5+
WORKDIR /
6+
7+
COPY db-init.sh /docker-entrypoint-initdb.d
8+
9+
# Create all the tables and initialise the database with a set of existing
10+
# channels, users, connections, messages, etc.
11+
# COPY ./seeds/full-2.sql /docker-entrypoint-initdb.d
12+
13+
# No need to override the default postgres CMD in this Dockerfile.

0 commit comments

Comments
 (0)