Use name of Docker (Flask) service inside package.json of React app - reactjs

I'm trying to have my React front end application interact with a Flask API, both Dockerized and built together with docker-compose. Here is the docker-compose.yml file:
version: "3.9"
services:
server:
build: ./server
ports:
- "80:5000"
volumes:
- ./server:/app
environment:
FLASK_ENV: development
env_file:
- ./.env
web:
build: ./app
ports:
- "3000:3000"
volumes:
- ./app:/user/src/app
depends_on:
- server
The package.json looks like this:
{
"name": "housing",
"version": "0.1.0",
"private": true,
...
"proxy":"http://server:80"
}
And then in App.js file trying to call the API with:
callAPI( some_arg ) {
var h = new Headers();
h.append("Content-Type", "application/json");
h.append("Access-Control-Allow-Origin", "*");
var raw = JSON.stringify({"some_arg":some_arg});
var requestOptions = {
method: 'POST',
headers: h,
body: raw,
redirect: 'follow'
};
const url = '/api/some_service'
fetch(url, requestOptions).then(res => res.json()).then(data => {
this.setState({some_component_data: data});
});
}
Unfortunately doing this results in an error:
Proxy error: Could not proxy request /api/some_service from localhost:3000 to http://server:80.
It works fine if I replace server with 0.0.0.0 but I'd quite like to use the actual container name in package.json. How can I do this?

My use case is a little different (django + redis), but I would try some combination of these 2 things:
Remove the http:// and just use server:80
Specify container_name in your docker-compose file. I don't know if this is actually necessary or if it uses the service name to connect, but worth a shot if the first thing doesn't work alone.
For my use case, the connection string is just redis://redis and the docker-compose section for that service looks like this:
redis:
image: redis
container_name: redis
restart: always
command: redis-server --requirepass <password>
volumes:
- redis_data:/data
ports:
- "6379:6379"

Related

SocketIo behind traefik and reverse proxy. Unable to connect

I am unable to connect to socketio in production.
This is my traefik file. my backend gets all calls which go to /api....
backend:
build:
context: ./backend
dockerfile: Dockerfile
command: ["npm", "run", "start"]
labels:
- "traefik.enable=true"
- "traefik.http.routers.backend.rule=Host(`mydomain.com`) && PathPrefix(`/api`)"
networks:
- web
My express server:
export const io = new Server(server, {
cors: {
origin: [
"http://localhost:5173",
"https://mysecretdomain.com",
],
},
});
My react frontend:
let socket: any;
if (import.meta.env.MODE === 'development') {
socket = io(API_CONFIGS.SOCKET_IO_URL);
} else {
// the app runs with traefik and is available under the prefix /api
socket = io(API_CONFIGS.SOCKET_IO_URL, { path: '/api/socket.io' });
}
In Development, everything works fine but in prod im getting a 404 error
example call from my domain
https://mysecretdomain/api/socket.io/?EIO=4&transport=polling&t=OKSmUX8
Status:404
I am completly out of ideas. Can someone help me out?

HTTP requests between docker containers

I have created two very simple containers to understand/test HTTP requests between containers, but for some reason, I just can't get my containers communicating. I keep getting GET http://backend:5000/ net::ERR_NAME_NOT_RESOLVED
My first container, a simple react app with no functionality, just gets container name from process.env.REACT_APP_URL, and makes a get request by fetch(http://${url}:5000/).
import { useState } from "react";
const MyComponent = () => {
const [message, setmessage] = useState("Hello");
async function buttonClick() {
let url = process.env.REACT_APP_URL;
try {
let response = await fetch(`http://${url}:5000/`);
console.log("This is response", response);
setmessage(response.data);
} catch (error) {
console.log("error occured:", error);
}
}
return (
<>
<p>{message}</p>
<button onClick={buttonClick}>Click Me!</button>
</>
);
};
export default MyComponent;
My second container, again incredibly simple Flask app, with Hello World served in the homepage route, and nothing else.
from flask import Flask, jsonify
app = Flask(__name__)
#app.route("/")
def hello_world():
return jsonify("Hello World"), 200
if __name__ == '__main__':
app.run(host="0.0.0.0")
And their corresponding docker files,
FROM node:17.4.0-alpine
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install --silent
RUN npm install react-scripts#3.4.1 -g --silent
# add app
COPY . ./
# start app
CMD ["npm", "start"]
FROM python:3.6.5-alpine
RUN apk update && apk upgrade && apk add gcc musl-dev libc-dev libc6-compat linux-headers build-base git libffi-dev openssl-dev
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "./myfile.py"]
Finally, I am using docker-compose to orchestrate these containers:
version: "3"
services:
backend:
build:
context: ./server
container_name: backend
expose:
- 5000
ports:
- 5000:5000
frontend:
build:
context: ./web
container_name: frontend
expose:
- 3000
ports:
- 3000:3000
environment:
- REACT_APP_URL=backend
depends_on:
- "backend"
links:
- "backend:backend"
My file system is as follows:
/sample-app
|_ server
|_ web
|_ docker-compose.yml
I have been trying to understand what I am doing wrong and I just can't find it. I appreciate any help. 🙏 🙏
Your frontend communicates with the backend with the exposed port on the host machine (not a container-container communication that needs to happen if the backend container wanted to connect with a DB container). Therefore, the hostname should be localhost not backend.
Try with the following change
frontend:
...
...
environment:
- REACT_APP_URL=localhost
...
...
You need to convert the response to JSON as well.
let response = await fetch(`http://${url}:5000/`);
response = await response.json();
console.log("This is response", response);
setmessage(response.data);

React app doesn't receive environment variables defined in Kubernetes pod

The app is pretty simple: its job is to display three types of variables:
A variable defined locally in the code,
An environment variable defined in Dockerfile and overwritten on pod creation,
An environment variable defined only on pod creation.
The code is as follows:
import './App.css';
function App() {
let something = "3Wzc3mgdlIFwc4"
return (
<div className="App">
<header className="App-header">
<p>
<code>ENV1: </code> <em>{process.env.REACT_APP_ENV_VARIABLE}</em>
</p>
<p>
<code>ENV2: </code> <em>{process.env.REACT_APP_ENV_VARIABLE_TWO}</em>
</p>
<p>
Hash: <code>{something || "not set"}</code>
</p>
</header>
</div>
);
}
export default App;
Dockerfile used when building an image got the following values:
FROM node:17.1.0-buster-slim as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
RUN npm config set unsafe-perm true
RUN npm install -g npm#8.1.3
RUN npm install --force
RUN npm install react-scripts#4.0.3 -g
COPY . ./
RUN chown -R node /app/node_modules
ENV REACT_APP_ENV_VARIABLE "It works!"
RUN npm run build
FROM nginx:1.21.4-alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
As I want to deploy the app on Kubernetes, the following pod and service are defined:
apiVersion: v1
kind: Pod
metadata:
name: playground-pod
labels:
name: playground-pod
app: playground-app
spec:
containers:
- name: playground
image: localhost:5000/playground:1.0
ports:
- containerPort: 80
env:
- name: REACT_APP_ENV_VARIABLE
value: "Variable from Kube!"
- name: REACT_APP_ENV_VARIABLE_TWO
value: "192.168.0.120"
apiVersion: v1
kind: Service
metadata:
name: playground-service
labels:
name: playground-service
app: playground-app
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30003
protocol: TCP
selector:
name: playground-pod
app: playground-app
After successful creation of the pod and the service, the variables in the pod container look this way:
/ # printenv | grep REACT_APP
REACT_APP_ENV_VARIABLE=Variable from Kube!
REACT_APP_ENV_VARIABLE_TWO=192.168.0.120
Yet, when I display the page in the browser, I can see something like this:
ENV1: It works!
ENV2:
Hash: 3Wzc3mgdlIFwc4
So, as you see, the local variable is displayed as expected, the variable that was intended to be overwritten is not overwritten and keeps the value from the Dockerfile, and the second variable that was only present in pod definition is not displayed at all.
Why is that? What should I do to make it work as expected?

React app not loading with docker-compose

The Dockerfile for my React client:
FROM node:10
WORKDIR /app/client
COPY ["package.json", "package-lock.json", "./"]
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
The Dockerfile for my Express backend:
FROM node:10
WORKDIR /app/server
COPY ["package.json", "package-lock.json", "./"]
RUN ls
RUN npm install --production
COPY . .
EXPOSE 5000
CMD ["node", "server.js"]
My docker-compose.yml file in my project's root:
version: '3'
services:
backend:
build:
context: ./backend
dockerfile: ./Dockerfile
image: "isaacasante/mcm-backend"
ports:
- "5000:5000"
frontend:
build:
context: ./client
dockerfile: ./Dockerfile
image: "isaacasante/mcm-client"
ports:
- "3000:3000"
links:
- "backend"
My server.js file under my backend folder:
var express = require("express");
var cors = require("cors");
var app = express();
var path = require("path");
// Enable CORS and handle JSON requests
app.use(cors());
app.use(express.json());
app.post("/", function (req, res, next) {
// console.log(req.body);
res.json({ msg: "This is CORS-enabled for all origins!" });
});
// Set router for email notifications
const mailRouter = require("./routers/mail");
const readerRouter = require("./routers/reader");
const notificationsRouter = require("./routers/booking-notifications");
app.use("/email", mailRouter);
app.use("/reader", readerRouter);
app.use("/notifications", notificationsRouter);
if (process.env.NODE_ENV === "production") {
app.use(express.static("mcm-app/build"));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "mcm-app", "build", "index.html"));
});
}
app.listen(5000, function () {
console.log("server starting...");
});
When I run:
docker-compose up
I get the following output in my terminal:
$ docker-compose up
Starting mcm_fyp_backend_1 ... done
Starting mcm_fyp_frontend_1 ... done
Attaching to mcm_fyp_backend_1, mcm_fyp_frontend_1
backend_1 | server starting...
frontend_1 |
frontend_1 | > mcm-app#0.1.0 start /app/client
frontend_1 | > react-scripts start
frontend_1 |
frontend_1 | ? ?wds?: Project is running at http://172.18.0.3/
frontend_1 | ? ?wds?: webpack output is served from
frontend_1 | ? ?wds?: Content not from webpack is served from /app/client/public
frontend_1 | ? ?wds?: 404s will fallback to /
frontend_1 | Starting the development server...
frontend_1 |
mcm_fyp_frontend_1 exited with code 0
My backend exits with code 0, and I can't load my app. My backend is running though.
What am I doing wrong, and how can I get my React-Express-Node app running with Docker Compose?
I found the solution to prevent my frontend service from exiting with 0. I had to add tty: true for it in my docker-compose.yml file. Also, to make the frontend-backend interaction work as expected in the app, I had to change my proxy command in my client's package.json to the following:
"proxy": "http://backend:5000"
And I changed my links command in my docker-compose.yml to this:
links:
- "backend:be"
After rebuilding, everything is working as intended.

nginx reverse proxy : js file not loading when running apps on the same server_name

I have django and angular app both as a container.
it works fine when I run the two apps via nginx in different ports like below.
worker_processes 1;
events { worker_connections 1024; }
http {
# Django config
upstream django-api {
ip_hash;
server django-api:8000;
}
# Angular config
upstream angular-apache2 {
ip_hash;
server angular-apache2:80;
}
#Angular config
server {
location / {
proxy_pass http://angular-apache2/;
}
listen 80;
server_name localhost;
}
#Django config
server {
location / {
proxy_pass http://django-api/;
}
listen 8000;
server_name localhost;
}
but when I try to run in on the same port, Im getting this error message http://localhost/runtime-es2015.js net::ERR_ABORTED 404 (Not Found)
"/etc/nginx/html/runtime-es2015.js" failed (2: No such file or directory), client: 192.168.16.1, server: localhost, request: "GET /runtime-es2015.js HTTP/1.1", host: "localhost", referrer: "http://localhost/web/
worker_processes 1;
events { worker_connections 1024; }
http {
# Django config
upstream django-api {
ip_hash;
server django-api:8000;
}
# Angular config
upstream angular-apache2 {
ip_hash;
server angular-apache2:80;
}
server {
location /web/ {
proxy_pass http://angular-apache2/;
}
location /api {
proxy_pass http://django-api/api/v1;
}
listen 80;
server_name localhost;
}
here is my docker-compose
version: "3"
services:
reverse-proxy:
build:
context: ./nginx
container_name: ${PROJECT_NAME}--nginx
ports:
- '8000:8000'
- '80:80'
# volumes:
# - ./nginx/django.conf:/etc/nginx/conf.d
depends_on:
- django-api
- angular-apache2
angular-apache2:
build:
context: ./angular
container_name: ${PROJECT_NAME}--angular
ports:
- '80'
volumes:
- ./angular/dist/ng7:/var/www/html
- ./config/apache/angular-000-default.conf:/etc/apache2/sites-available/000-default.conf
django-api:
build:
context: ./django
command: gunicorn data_tracker.wsgi:application --bind 0.0.0.0:8000
#command: systemctl restart apache2
container_name: ${PROJECT_NAME}--django
ports:
- '8000'
volumes:
- ./django:/django
- ./config/apache/django-000-default.conf:/etc/apache2/sites-available/000-default.conf

Resources