Tengo la siguiente estructura de carpeta:
/backend (en golang)
/frontend (en next14)
compose.yml
Dentro de backend/
tengo su despectivo Dockerfile
:
FROM golang:1.22
WORKDIR /app
COPY go.mod .
COPY main.go .
RUN go get
RUN go build -o bin .
EXPOSE 4000
ENTRYPOINT ["/app/bin"]
Dentro de frontend/
el Dockerfile
es el siguiente:
FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]
En compose.yml
version: "3.9"
services:
frontend:
container_name: front-paperworksmanager
image: front-paperworksmanager:1.0.0
build: ./frontend
ports:
- "3000:3000"
depends_on:
- backend
networks:
- paper
backend:
container_name: back-paperworksmanager
image: back-paperworksmanager:1.0.0
build:
context: ./backend
dockerfile: Dockerfile
ports:
- "4000:4000"
networks:
- paper
networks:
paper:
external: true
Hice muchos intentos por lo cual en comopose.yml
opte por crear manualmente
la network
"paper"
Al momento de ejecutar docker compose up
El backend se genera pero next me arroja el error :
9.652 TypeError: fetch failed
9.652 at node:internal/deps/undici/undici:12500:13
9.652 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
9.652 at async globalThis.fetch (/app/.next/server/chunks/638.js:1:36484)
9.652 at async n (/app/.next/server/app/hello/page.js:1:2484)
9.652 at async a (/app/.next/server/app/hello/page.js:1:2560) {
9.652 [cause]: Error: connect ECONNREFUSED 127.0.0.1:4000
9.652 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1605:16)
9.652 at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
9.652 errno: -111,
9.652 code: 'ECONNREFUSED',
9.652 syscall: 'connect',
9.652 address: '127.0.0.1',
9.652 port: 4000
9.652 }
9.652 }
Por el lado de nextjs
estoy haciendo una peticion server side:
src/app/hello/page.tsx
const greetings = async (): Promise<any> => {
const response = await fetch("http://localhost:4000/hello");
return response.text();
};
export default async function hola() {
const x = await greetings();
return (
<>
<p>{x}</p>
</>
);
}
Ya intente poniendo en el fetch
: http://backend:4000/hello pero no funciono.