Skip to content

Commit 6b520f0

Browse files
authored
Merge pull request #15 from cupofpython/local-compose-cleanup
cleanup local vs compose testing
2 parents f192422 + 9d657f6 commit 6b520f0

File tree

9 files changed

+60
-64
lines changed

9 files changed

+60
-64
lines changed

.env.compose

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
REACT_APP_NODE_ENV=development
2+
REACT_APP_LOCAL=localhost
3+
REACT_APP_MODEL_SERVICE=model
4+
REACT_APP_SERVER_PORT=5002

.env.dev

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
REACT_APP_NODE_ENV=development
2+
REACT_APP_LOCAL=localhost
3+
REACT_APP_MODEL_SERVICE=localhost
4+
REACT_APP_SERVER_PORT=5002

Dockerfile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ ARG NODE_VERSION=23.10.0
1010

1111
FROM node:${NODE_VERSION}-alpine
1212

13-
# Use production node environment by default.
14-
ENV NODE_ENV production
15-
16-
1713
WORKDIR /usr/src/app
1814

1915
# Download dependencies as a separate step to take advantage of Docker's caching.
@@ -37,5 +33,8 @@ EXPOSE 3000
3733
# To access server.js
3834
EXPOSE 5001
3935

36+
# Expose dev port
37+
EXPOSE 5002
38+
4039
# Run the application.
41-
CMD npm start
40+
CMD npm run start:prod

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ It is a NodeJS app that uses the llama3.2 model to service prompt requests. It u
1212
## How to Run
1313

1414
### Locally
15-
- Update the `App.js` and `server.js` to use 0.0.0.0 as hosts for API requests instead of K8s service names.
16-
- `docker run -p 11434:11434 ollama/ollama:0.6.2`
15+
- Run an LLM container `docker run -p 11434:11434 --name model ollama/ollama:0.6.2`
1716
- Exec into the container and run `ollama pull llama3.2`
18-
- `npm start`
17+
- `dotenv -e .env.dev -- npm run start:dev`
1918

2019
### Locally with Docker Compose
2120
I used compose to develop this locally.
2221

23-
- Update the `App.js` and `server.js` to use 0.0.0.0 as hosts for API requests instead of K8s service names.
2422
- `docker compose up --build`
2523
- When done, `docker compose down`
2624

compose.yaml

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,24 @@
99
# https://github.com/docker/awesome-compose
1010
services:
1111
server:
12+
container_name: server
13+
command: npm run start:dev
1214
depends_on:
1315
- model
1416
build:
1517
context: .
16-
environment:
17-
NODE_ENV: production
18+
volumes:
19+
- ./:/usr/src/app
20+
- /usr/src/app/node_modules
21+
env_file:
22+
- .env.compose
1823
ports:
1924
- 3000:3000
25+
- 5002:5002
2026
model:
27+
container_name: model
2128
image: ollama/ollama:0.6.2
2229
ports:
2330
- 11434:11434
24-
container_name: ollama
2531
post_start:
2632
- command: ollama pull llama3.2
27-
28-
29-
# The commented out section below is an example of how to define a PostgreSQL
30-
# database that your application can use. `depends_on` tells Docker Compose to
31-
# start the database before your application. The `db-data` volume persists the
32-
# database data between container restarts. The `db-password` secret is used
33-
# to set the database password. You must create `db/password.txt` and add
34-
# a password of your choosing to it before running `docker-compose up`.
35-
# depends_on:
36-
# db:
37-
# condition: service_healthy
38-
# db:
39-
# image: postgres
40-
# restart: always
41-
# user: postgres
42-
# secrets:
43-
# - db-password
44-
# volumes:
45-
# - db-data:/var/lib/postgresql/data
46-
# environment:
47-
# - POSTGRES_DB=example
48-
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
49-
# expose:
50-
# - 5432
51-
# healthcheck:
52-
# test: [ "CMD", "pg_isready" ]
53-
# interval: 10s
54-
# timeout: 5s
55-
# retries: 5
56-
# volumes:
57-
# db-data:
58-
# secrets:
59-
# db-password:
60-
# file: db/password.txt
61-

package-lock.json

Lines changed: 24 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
"concurrently": "^9.1.2",
1414
"cors": "^2.8.5",
1515
"dockerode": "^4.0.4",
16+
"dotenv": "^16.4.7",
1617
"node-polyfill-webpack-plugin": "^4.1.0",
18+
"os": "^0.1.2",
1719
"react": "^19.0.0",
1820
"react-dom": "^19.0.0",
1921
"react-scripts": "5.0.1",
2022
"web-vitals": "^2.1.4"
2123
},
2224
"scripts": {
23-
"start": "concurrently \"node server.js\" \"react-scripts start\"",
25+
"start:prod": "concurrently \"node server.js\" \"react-scripts start\"",
26+
"start:dev": "concurrently \"node server.js\" \"react-scripts start\"",
2427
"build": "react-scripts build",
2528
"test": "react-scripts test",
2629
"eject": "react-scripts eject"

server.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ const cors = require("cors"); // Import CORS middleware
33
const axios = require("axios")
44
const app = express(); // Initialize Express
55

6-
// Use env variable, or fallback to localhost
7-
// const LLM_API_HOST = process.env.LLM_API_HOST || 'http://0.0.0.0:11434';
8-
96
app.use(cors());
107
app.use(express.json());
118

@@ -26,9 +23,8 @@ app.post("/execute", async (req, res) => {
2623
});
2724

2825
function getResponse(model, prompt) {
29-
//console.log(`${LLM_API_HOST}`)
30-
// Use K8s service nae, switch back to 0.0.0.0 for local testing (npm start)
31-
return axios.post("http://model-published:11434/api/generate", {
26+
var host = ("REACT_APP_MODEL_SERVICE" in process.env) ? process.env.REACT_APP_MODEL_SERVICE : "model-published";
27+
return axios.post(`http://${host}:11434/api/generate`, {
3228
model: model,
3329
prompt: prompt,
3430
stream: false
@@ -43,8 +39,8 @@ function getResponse(model, prompt) {
4339
});
4440
}
4541

46-
// Start the server and listen on port 5001
47-
const PORT = 5001;
48-
app.listen(PORT, () => {
49-
console.log(`Server running on port ${PORT}`);
42+
// Start the server and listen on port specified in command line, ex. PORT=5001 node server.js
43+
var port = ("REACT_APP_SERVER_PORT" in process.env) ? process.env.REACT_APP_SERVER_PORT : 5001;
44+
app.listen(port, () => {
45+
console.log(`Server running on port ${port}`);
5046
});

src/App.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ function App() {
5353
const prompt = `Context: This is a cat named ${catName}. They have the following traits: ${catTraits}. Generate a response as the cat to the following message: ${newUserInput}`;
5454

5555
// Execute command and wait for the result
56-
// Switch K8s service name to 0.0.0.0 for local testing!
57-
const result = await fetch("http://a4c423481a99842669d9088bba7450ad-1853516926.us-east-2.elb.amazonaws.com:5001/execute", {
56+
var host = ("REACT_APP_LOCAL" in process.env) ? process.env.REACT_APP_LOCAL : "a4c423481a99842669d9088bba7450ad-1853516926.us-east-2.elb.amazonaws.com";
57+
var port = ("REACT_APP_SERVER_PORT" in process.env) ? process.env.REACT_APP_SERVER_PORT : 5001;
58+
const result = await fetch(`http://${host}:${port}/execute`, {
5859
method: "POST",
5960
headers: { "Content-Type": "application/json" },
6061
body: JSON.stringify({

0 commit comments

Comments
 (0)