Skip to content

Commit c1d482f

Browse files
Merge pull request #67 from Tietokilta/feature/containers-v2
Update container setup
2 parents 9dd2213 + 5e94708 commit c1d482f

15 files changed

Lines changed: 327 additions & 75 deletions

File tree

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
packages/*/node_modules/
3+
packages/*/build/
4+
packages/*/dist/

Dockerfile

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
1-
FROM node:8.9.4-alpine
1+
# Build stage:
2+
FROM node:14-alpine as builder
23

3-
ADD . /opt/ilmomasiina
4+
# Copy source files
5+
COPY . /opt/ilmomasiina
46
WORKDIR /opt/ilmomasiina
5-
RUN adduser -D athene
6-
RUN chown -R athene /opt/ilmomasiina
7-
USER athene
8-
RUN npm install
9-
CMD npm start
7+
8+
# Install dependencies (we're running as root, so the postinstall script doesn't run automatically)
9+
RUN npm ci && npm run bootstrap
10+
11+
# Build all packages
12+
RUN npm run build
13+
14+
# Main stage:
15+
FROM node:14-alpine
16+
17+
# Default to production
18+
ENV NODE_ENV=production
19+
20+
# Create user for running
21+
RUN adduser -D -h /opt/ilmomasiina ilmomasiina
22+
USER ilmomasiina
23+
24+
WORKDIR /opt/ilmomasiina
25+
26+
# Copy backend dependencies from build stage
27+
COPY --from=builder /opt/ilmomasiina/packages/ilmomasiina-backend/node_modules /opt/ilmomasiina/node_modules
28+
29+
# Copy compiled ilmomasiina-models as "src" into backend dependencies (TODO: implement a better build for this)
30+
COPY --from=builder /opt/ilmomasiina/packages/ilmomasiina-models/dist /opt/ilmomasiina/node_modules/@tietokilta/ilmomasiina-models/src
31+
32+
# Copy built backend from build stage
33+
COPY --from=builder /opt/ilmomasiina/packages/ilmomasiina-backend/dist /opt/ilmomasiina/dist
34+
35+
# Copy built frontend from build stage
36+
COPY --from=builder /opt/ilmomasiina/packages/ilmomasiina-frontend/build /opt/ilmomasiina/frontend
37+
38+
# Start server
39+
CMD ["node", "dist/bin/server.js"]

Dockerfile.dev

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This Dockerfile is intended for development use only.
2+
3+
# Only the files required to bootstrap the project will be copied to the container.
4+
# Node modules will be installed at the build phase and included in the container image.
5+
# Sources and other files are intended to be provided using bind mounts.
6+
7+
FROM node:14-alpine
8+
9+
WORKDIR /opt/ilmomasiina
10+
11+
# Copy static resources from the repository root
12+
COPY .eslint* .postcssrc lerna.json package*.json ./
13+
14+
# Copy static resources from each package
15+
COPY packages/ilmomasiina-frontend/package*.json \
16+
packages/ilmomasiina-frontend/tsconfig.json \
17+
packages/ilmomasiina-frontend/.eslint* \
18+
packages/ilmomasiina-frontend/
19+
20+
COPY packages/ilmomasiina-backend/package*.json \
21+
packages/ilmomasiina-backend/tsconfig.json \
22+
packages/ilmomasiina-backend/.eslint* \
23+
packages/ilmomasiina-backend/
24+
25+
COPY packages/ilmomasiina-models/package*.json \
26+
packages/ilmomasiina-models/tsconfig.json \
27+
packages/ilmomasiina-models/
28+
29+
ENV NODE_ENV=development
30+
31+
# Install dependencies (we're running as root, so the postinstall script doesn't run automatically)
32+
RUN npm install && npm run bootstrap

README.md

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,55 @@
11
# Ilmomasiina
22

3-
Ilmomasiina is the event registration system originally created by Athene, forked by Tietokilta and currently under heavy development for our new site. Once finished, it will be available for all organizations to use, along with migration tools from the Athene-made version.
3+
Ilmomasiina is the event registration system originally created by Athene, forked by Tietokilta and currently under
4+
heavy development for our new site. Once finished, it will be available for all organizations to use, along with
5+
migration tools from the Athene-made version.
46

5-
The latest development version is in the `dev` branch. **Please note that the code is currently in alpha phase.** It likely has major bugs, and easy migrations will not be provided between versions until we reach beta.
7+
The latest development version is in the `dev` branch. **Please note that the code is currently in alpha phase.**
8+
It likely has major bugs, and easy migrations will not be provided between versions until we reach beta.
69

710
## Contributing
811

9-
Progress and planning is tracked in GitHub issues. Please see and update the [project board](https://github.com/Tietokilta/ilmomasiina/projects/1) for ongoing work.
12+
Progress and planning is tracked in GitHub issues.
13+
Please see and update the [project board](https://github.com/Tietokilta/ilmomasiina/projects/1) for ongoing work.
1014

11-
All help is appreciated. Please contact @PurkkaKoodari or another member of Tietokilta's Digitoimikunta if you wish to actively help with development – there are still major changes to be done that may conflict with yours. Start by reading the [docs](docs/README.md) to get familiar with the project.
15+
All help is appreciated. Please contact @PurkkaKoodari or another member of Tietokilta's Digitoimikunta if you wish to
16+
actively help with development – there are still major changes to be done that may conflict with yours.
17+
Start by reading the [docs](docs/README.md) to get familiar with the project.
1218

1319
## Documentation
1420

1521
See the [docs](docs/README.md) folder.
1622

1723
## Requirements
1824

19-
- Node.js 14
20-
- npm 6
21-
- MySQL or MariaDB (latest, older supported versions unknown)
25+
- If using Docker:
26+
- Docker: 17.04.0+
27+
- docker-compose or Compose V2 (included in the latest releases of docker-cli)
28+
- For development, or if running without Docker:
29+
- Node.js: 14+
30+
- npm: 6+
2231

23-
Containerization for these is in progress.
32+
To run the project (dev or production), only Docker and Docker Compose are required.
33+
For actual development, you'll want to have Node.js and npm installed locally in order to manage dependencies.
2434

2535
<!--
26-
## Using Docker container
27-
In project root directory
28-
```bash
29-
docker-compose up
30-
```
31-
This should build and run the environment so that it is accesible at [localhost:3000](http://localhost:3000). You will need to create an `.env` file in project root (see [ENV.md](ENV.md)).
32-
3336
### Create fake data
34-
Use `docker exec ilmomasiina_backend_1 npm run create-fake-data` to create some data to dockerized Ilmomasiina. The server does not like an empty database, so this is a really good idea to do when first starting the server.
37+
Use `docker exec ilmomasiina_backend_1 npm run create-fake-data` to create some data to dockerized Ilmomasiina.
38+
The server does not like an empty database, so this is a really good idea to do when first starting the server.
3539
-->
40+
41+
<!--
3642
## MySQL Setup
37-
<!--Only follow this if you don't use the Docker container.-->
43+
Only follow this if you don't use the Docker container.
3844
3945
### Mac
4046
1. Install `mysql` (8.x) with Homebrew (https://gist.github.com/nrollr/3f57fc15ded7dddddcc4e82fe137b58e)
4147
2. Start the mysql service with `brew services start mysql`
4248
3. Open the mysql terminal with `mysql -u root`
43-
4. In the mysql terminal, create a new user e.g. `CREATE USER 'juuso'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';`
44-
5. Fix permissions (this is probably too permissive, but it works): `GRANT ALL PRIVILEGES ON *.* TO 'sampo'@'localhost' WITH GRANT OPTION;`
49+
4. In the mysql terminal, create a new user e.g.
50+
`CREATE USER 'juuso'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';`
51+
5. Fix permissions (this is probably too permissive, but it works):
52+
`GRANT ALL PRIVILEGES ON *.* TO 'sampo'@'localhost' WITH GRANT OPTION;`
4553
6. Type `exit` to exit the mysql terminal, and sign in with your new user e.g. `mysql -u juuso -p password`
4654
7. Create the `ilmomasiina` database with `CREATE DATABASE ilmomasiina;`
4755
@@ -53,43 +61,72 @@ Use `docker exec ilmomasiina_backend_1 npm run create-fake-data` to create some
5361
5. Fix permissions (this is probably too permissive, but it works): `GRANT ALL PRIVILEGES ON *.* TO 'sampo'@'localhost' WITH GRANT OPTION;`
5462
6. Exit with `exit` and sign in with your new user e. g. `mysql -u juuso -p` (don't use `mysql -u juuso -p password`)
5563
7. Follow Mac instructions step 6
64+
-->
5665

57-
## Getting started
58-
If you are using the Docker container, only follow step 1 as rest are automatically executed.
66+
## Production setup
5967

60-
1. Create an `.env` file at the root of the project. For the contents of the .env file, check [ENV.MD](./ENV.MD)
61-
2. `npm install`
62-
3. **(IMPORTANT)** `npx lerna bootstrap`
63-
4. `npm start`
68+
TODO. (This file has lots of outdated information as comments.)
6469

65-
**Optional**: You can create mockup data for development by running `npm run create-fake-data`. During development, database can be resetted with `npm run reset-db`.
70+
## For developers
6671

67-
### Troubleshooting (Ubuntu)
68-
If `npm start` gives error `Error: You must provide a 'secret' in your authentication configuration`, it probably means that the `.env` file is not loaded correctly. A quick fix for this is to run `source .env`.
72+
See the [documentation](docs/README.md) for more information.
6973

70-
## Mailgun setup
74+
### Development setup using Docker Compose
7175

72-
Mailgun provides 10 000 free messages per month which is suitable for small projects. With minor changes sending mail could be also done via Sendgrid. Using your own mail server gets you labelled as spam pretty fast.
76+
The entire development setup can be run within Docker using Docker Compose. The
77+
[docker-compose dev setup](./docker-compose.yml) is located at the root of this repository, and contains a
78+
pre-configured Postgres server, so an external database server is not required.
7379

74-
Add mailgun credentials to .env configuration.
80+
1. Create a `.env` file at the root of this repository. You can copy [.env.example](./.env.example) to begin.
81+
2. Go to the repository root and run `docker-compose up`. This builds the dev container and starts the frontend and
82+
backend servers in parallel.
83+
3. Access the app at <http://localhost:3000>.
7584

76-
### Create first admin user
85+
Due to how the dev Docker is set up, you will need to rebuild the development image if you change the dependencies,
86+
package.json or ESLint configs.
7787

78-
Add this line to .env configuration.
88+
### Non-containerized development setup
7989

90+
You can also run the development server outside Docker.
91+
92+
1. Install Postgres or MySQL. You can use Docker for this. SQLite may also work. but is not currently tested.
93+
2. Create a `.env` file at the root of this repository. You can copy [.env.example](./.env.example) to begin.
94+
3. Run `npm install` to install Lerna and other global dependencies. The postinstall script should automatically run
95+
`lerna bootstrap` to setup cross-dependencies between packages and install package dependencies.
96+
4. Run `npm start`. This will start the frontend and backend dev servers in parallel.
97+
- If you want cleaner output, you can run `npm start` separately in `packages/ilmomasiina-frontend` and
98+
`packages/ilmomasiina-backend`.
99+
- Currently, there is no way to run the Webpack development server directly within the backend process.
100+
5. Access the app at <http://localhost:3000>.
101+
102+
<!-- TODO
103+
### Creating first admin user
104+
> By default, only logged-in admin users can create new admin users using the `/admin` endpoint.
105+
> To create the first one, admin registration needs to be allowed.
106+
107+
Allow admin registration temporarily by adding this line to the `.env` file:
80108
```
81109
ADMIN_REGISTRATION_ALLOWED=true
82110
```
83111
84-
Create new user with POST request.
112+
If the Ilmomasiina was already running, restart it to apply the new env configuration.
85113
114+
Now, create a new user with POST request to `/admin`.
115+
Below is an example using curl:
86116
```
87-
curl 'http://localhost:3000/api/users' -H 'Content-Type: application/json' --data-binary '{ "email": "ville@athene.fi", "password": "password" }'
117+
curl 'http://localhost:3000/api/users' \
118+
-H 'Content-Type: application/json' \
119+
--data-binary '{ "email": "ville@athene.fi", "password": "password" }'
88120
```
89121
90-
**Important**: Disallow admin user creation by removing the line.
122+
**Important**: Disallow admin user creation by removing the previously added line from the `.env` file and restarting
123+
the docker containers.
124+
125+
## Mailgun setup
126+
127+
Mailgun provides 10 000 free messages per month which is suitable for small projects. With minor changes sending mail could be also done via Sendgrid. Using your own mail server gets you labelled as spam pretty fast.
91128
92-
By default, only logged in admin users can create new admin users (via `/admin`).
129+
Add mailgun credentials to .env configuration.
93130
94131
## Production
95132
@@ -127,3 +164,4 @@ git pull otax/master
127164
npm run compile
128165
pm2 restart prod-server
129166
```
167+
-->

docker-compose.prod.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
version: '3.2'
2+
3+
# This compose file demonstrates how Ilmomasiina could be run in a production environment.
4+
5+
# This compose file serves Ilmomasiina at http://localhost:8000.
6+
# Env variables from .env file (in the repository root) will be used by docker compose and passed to the container.
7+
# Remember to create your own .env file (see .env.example)!
8+
9+
services:
10+
database:
11+
image: postgres:latest
12+
restart: always
13+
environment:
14+
- POSTGRES_PASSWORD=$DB_PASSWORD
15+
- POSTGRES_USER=$DB_USER
16+
- POSTGRES_DB=$DB_DATABASE
17+
18+
ilmomasiina:
19+
build:
20+
context: .
21+
dockerfile: "Dockerfile"
22+
restart: always
23+
depends_on:
24+
- database
25+
environment:
26+
- DB_HOST=database
27+
- NODE_ENV=production
28+
- ENFORCE_HTTPS=false
29+
# When deploying to production, either set ENFORCE_HTTPS true (default) or
30+
# configure the reverse proxy to enforce HTTPS communication with the clients.
31+
env_file:
32+
- .env
33+
ports:
34+
- "8000:3000"
35+
volumes:
36+
- "frontend:/opt/ilmomasiina/frontend"
37+
38+
volumes:
39+
frontend:
40+
# Contains static files of the ilmomasiina-frontend.
41+
# To achieve better performance under high loads, these files
42+
# can be served using a more performant web server or even a CDN.

docker-compose.yml

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,56 @@
1-
version: '3'
1+
version: '3.2'
2+
3+
# This compose file is for development use only – Do not use this in production!
4+
5+
# This compose file builds Ilmomasiina and serves it at http://localhost:3000.
6+
# For easier development, it provides hot reloading by utilizing bind mounts.
7+
# You will still need to rebuild if you update the project's dependencies.
8+
9+
# Env vars from the .env file (in repository root) will be used by docker-compose and passed to the containers.
10+
# Remember to create your own .env file (see .env.example)!
11+
# The database will use the DB_USER, DB_PASSWORD and DB_DATABASE provided by you.
212

313
services:
4-
database:
5-
image: mysql:8
6-
command: --default-authentication-plugin=mysql_native_password
7-
env_file:
8-
- .env
9-
backend:
10-
# Use Dockerfile from root directory
11-
build: .
12-
environment:
13-
- MYSQL_HOST=database
14-
env_file:
15-
- .env
16-
ports:
17-
- "3000:3000"
14+
database:
15+
image: postgres:latest
16+
restart: always
17+
environment:
18+
- POSTGRES_PASSWORD=$DB_PASSWORD
19+
- POSTGRES_USER=$DB_USER
20+
- POSTGRES_DB=$DB_DATABASE
21+
22+
ilmomasiina-dev:
23+
build:
24+
context: .
25+
dockerfile: "Dockerfile.dev"
26+
command: "npm start"
27+
restart: always
28+
depends_on:
29+
- database
30+
environment:
31+
- DB_DIALECT=postgres
32+
- DB_HOST=database
33+
- NODE_ENV=development
34+
env_file:
35+
- .env
36+
volumes:
37+
- type: bind
38+
source: ./packages/ilmomasiina-models/src
39+
target: /opt/ilmomasiina/packages/ilmomasiina-models/src
40+
- type: bind
41+
source: ./packages/ilmomasiina-frontend/config
42+
target: /opt/ilmomasiina/packages/ilmomasiina-frontend/config
43+
- type: bind
44+
source: ./packages/ilmomasiina-frontend/public
45+
target: /opt/ilmomasiina/packages/ilmomasiina-frontend/public
46+
- type: bind
47+
source: ./packages/ilmomasiina-frontend/scripts
48+
target: /opt/ilmomasiina/packages/ilmomasiina-frontend/scripts
49+
- type: bind
50+
source: ./packages/ilmomasiina-frontend/src
51+
target: /opt/ilmomasiina/packages/ilmomasiina-frontend/src
52+
- type: bind
53+
source: ./packages/ilmomasiina-backend/src
54+
target: /opt/ilmomasiina/packages/ilmomasiina-backend/src
55+
ports:
56+
- "3000:3000"

docs/project-structure.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
The project uses [Lerna](https://lerna.js.org/) to manage the code in separate packages. This allows for cleaner code
44
and package files.
55

6-
To prepare for development, run `npx lerna bootstrap`.
7-
The project is configured to run development servers for the frontend and backend with `npm start`.
6+
To prepare for development, Lerna must bootstrap the cross-dependencies between projects. This is done automatically
7+
using a npm *postinstall* script. Running `npm install` or `npm ci` will automatically run `lerna bootstrap` once
8+
packages are installed. To re-run bootstrapping manually, run `npm run bootstrap`.
9+
10+
The project is configured to run development servers for the frontend and backend with `npm start`. In development, the
11+
Webpack development server serves the frontend and proxies API calls to the backend server.
12+
13+
In production, the backend server can optionally serve the compiled frontend bundle, but ideally a separate reverse
14+
proxy such as Nginx is used for this purpose.
815

916
## Packages
1017

0 commit comments

Comments
 (0)