Skip to content

Commit ecff058

Browse files
author
oomsooms
committed
Added Dockerfile, changed default server port to 3000, added create env in action
1 parent 7883022 commit ecff058

File tree

7 files changed

+147
-23
lines changed

7 files changed

+147
-23
lines changed

.github/workflows/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,12 @@ jobs:
1212
- name: Checkout repository
1313
uses: actions/checkout@v4
1414

15+
- name: Create env file in /server
16+
run: |
17+
mkdir -p server
18+
touch server/.env
19+
echo DATABASE_URI=${{ vars.DATABASE_URI }} >> server/.env
20+
cat server/.env # Verify the file is created
21+
1522
- name: Build Docker image
1623
run: docker build -t my-app:latest -t my-app:${{ github.sha }} -f server/Dockerfile server

server/.dockerignore

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
1-
# Ignore node_modules folder
2-
node_modules
3-
# Ignore logs
4-
logs
5-
# Ignore environment files
6-
.env
7-
# Ignore git directory
8-
.git
1+
# Include any files or directories that you don't want to be copied to your
2+
# container here (e.g., local build artifacts, temporary files, etc.).
3+
#
4+
# For more help, visit the .dockerignore file reference guide at
5+
# https://docs.docker.com/go/build-context-dockerignore/
6+
7+
**/.classpath
8+
**/.dockerignore
9+
**/.env
10+
**/.git
11+
**/.gitignore
12+
**/.project
13+
**/.settings
14+
**/.toolstarget
15+
**/.vs
16+
**/.vscode
17+
**/.next
18+
**/.cache
19+
**/*.*proj.user
20+
**/*.dbmdl
21+
**/*.jfm
22+
**/charts
23+
**/docker-compose*
24+
**/compose.y*ml
25+
**/Dockerfile*
26+
**/node_modules
27+
**/npm-debug.log
28+
**/obj
29+
**/secrets.dev.yaml
30+
**/values.dev.yaml
31+
**/build
32+
**/dist
33+
LICENSE
34+
README.md

server/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Database Configuration
2-
DATABASE_URI=mongodb://localhost:27017/databasename
2+
DATABASE_URI=mongodb+srv://thomas:[email protected]/?retryWrites=true&w=majority&appName=Cluster0
33

44
# Security Keys
55
JWT_SECRET_KEY=SOME_SECRET_KEY

server/Dockerfile

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
1-
# Use the official Node.js image as the base image
2-
FROM node:22.2.0-alpine
1+
# syntax=docker/dockerfile:1
32

4-
# Set the working directory
5-
WORKDIR /app
3+
# Comments are provided throughout this file to help you get started.
4+
# If you need more help, visit the Dockerfile reference guide at
5+
# https://docs.docker.com/go/dockerfile-reference/
66

7-
# Copy package.json and package-lock.json
8-
COPY package.json package-lock.json ./
7+
# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7
98

10-
# Install dependencies
11-
RUN npm install --production
9+
ARG NODE_VERSION=22.2.0
1210

13-
# Copy the rest of the application code
11+
FROM node:${NODE_VERSION}-alpine
12+
13+
# Use production node environment by default.
14+
ENV NODE_ENV production
15+
16+
17+
WORKDIR /usr/src/app
18+
19+
# Download dependencies as a separate step to take advantage of Docker's caching.
20+
# Leverage a cache mount to /root/.npm to speed up subsequent builds.
21+
# Leverage a bind mounts to package.json and package-lock.json to avoid having to copy them into
22+
# into this layer.
23+
RUN --mount=type=bind,source=package.json,target=package.json \
24+
--mount=type=bind,source=package-lock.json,target=package-lock.json \
25+
--mount=type=cache,target=/root/.npm \
26+
npm ci --omit=dev
27+
28+
# Run the application as a non-root user.
29+
USER node
30+
31+
# Copy the rest of the source files into the image.
1432
COPY . .
1533

16-
# Expose the port the app runs on
17-
EXPOSE 3000
34+
# Expose the port that the application listens on.
35+
EXPOSE 8000
1836

19-
# Start the application, this uses cross-env to set the NODE_ENV to production
20-
CMD ["npm", "run", "prod"]
37+
# Run the application.
38+
CMD npm run prod

server/README.Docker.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### Building and running your application
2+
3+
When you're ready, start your application by running:
4+
`docker compose up --build`.
5+
6+
Your application will be available at http://localhost:8000.
7+
8+
### Deploying your application to the cloud
9+
10+
First, build your image, e.g.: `docker build -t myapp .`.
11+
If your cloud uses a different CPU architecture than your development
12+
machine (e.g., you are on a Mac M1 and your cloud provider is amd64),
13+
you'll want to build the image for that platform, e.g.:
14+
`docker build --platform=linux/amd64 -t myapp .`.
15+
16+
Then, push it to your registry, e.g. `docker push myregistry.com/myapp`.
17+
18+
Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/)
19+
docs for more detail on building and pushing.
20+
21+
### References
22+
* [Docker's Node.js guide](https://docs.docker.com/language/nodejs/)

server/compose.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Comments are provided throughout this file to help you get started.
2+
# If you need more help, visit the Docker Compose reference guide at
3+
# https://docs.docker.com/go/compose-spec-reference/
4+
5+
# Here the instructions define your application as a service called "server".
6+
# This service is built from the Dockerfile in the current directory.
7+
# You can add other services your application may depend on here, such as a
8+
# database or a cache. For examples, see the Awesome Compose repository:
9+
# https://github.com/docker/awesome-compose
10+
services:
11+
server:
12+
build:
13+
context: .
14+
environment:
15+
NODE_ENV: production
16+
ports:
17+
- 8000:8000
18+
19+
# The commented out section below is an example of how to define a PostgreSQL
20+
# database that your application can use. `depends_on` tells Docker Compose to
21+
# start the database before your application. The `db-data` volume persists the
22+
# database data between container restarts. The `db-password` secret is used
23+
# to set the database password. You must create `db/password.txt` and add
24+
# a password of your choosing to it before running `docker-compose up`.
25+
# depends_on:
26+
# db:
27+
# condition: service_healthy
28+
# db:
29+
# image: postgres
30+
# restart: always
31+
# user: postgres
32+
# secrets:
33+
# - db-password
34+
# volumes:
35+
# - db-data:/var/lib/postgresql/data
36+
# environment:
37+
# - POSTGRES_DB=example
38+
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
39+
# expose:
40+
# - 5432
41+
# healthcheck:
42+
# test: [ "CMD", "pg_isready" ]
43+
# interval: 10s
44+
# timeout: 5s
45+
# retries: 5
46+
# volumes:
47+
# db-data:
48+
# secrets:
49+
# db-password:
50+
# file: db/password.txt
51+

server/src/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const sessionConfig = require('./config/sessionConfig');
1313
const { errorHandler, rateLimiter } = require('./api/middlewares');
1414

1515
const app = express();
16-
const PORT = process.env.PORT || 8000;
16+
const PORT = process.env.PORT || 3000;
1717

1818
// trust first proxy
1919
app.set('trust proxy', 1);

0 commit comments

Comments
 (0)