I’m trying to run all my projects as containers on Docker. My react app tries calling the server using axios in chrome it fails:
export const fetchTreeSection = async (
sectionId: number,
): Promise<TreeSection> => {
const response = await axios.get<TreeSection>(
`${API_BASE_URL}/api/ImageNet/treeSection/${sectionId}`,
);
return response.data;
};
Chrome inspector error:
http://localhost:5105/api/ImageNet/treeSection/0
message: ‘Network Error’, name: ‘AxiosError’, code: ‘ERR_NETWORK’
But if I paste http://localhost:5105/api/ImageNet/treeSection/0 into another tab it does return data correctly.
I do not want to use my machine IP address instead of localhost because I want to send this assignment off to be executed on other machines.
Here is the docker file that I run locally:
version: '3.8'
services:
mssql:
image: mcr.microsoft.com/mssql/server:2019-latest
container_name: mssql
environment:
SA_PASSWORD: "StrongPassword123"
ACCEPT_EULA: "Y"
ports:
- "1433:1433"
volumes:
- mssql-data:/var/opt/mssql
healthcheck:
test: /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "StrongPassword123" -Q "SELECT 1" || exit 1
interval: 10s
retries: 10
start_period: 10s
timeout: 3s
importer:
build:
context: ./FinvizDataManager
dockerfile: Dockerfile.importer
depends_on:
mssql:
condition: service_healthy
environment:
ConnectionStrings__DefaultConnection: "Server=mssql,1433;Database=Finviz;Uid=sa;Pwd=StrongPassword123;TrustServerCertificate=True;MultipleActiveResultSets=true;"
server:
build:
context: ./FinvizDataManager
dockerfile: Dockerfile.server
depends_on:
- importer
environment:
ConnectionStrings__DefaultConnection: "Server=mssql,1433;Database=Finviz;Uid=sa;Pwd=StrongPassword123;TrustServerCertificate=True;MultipleActiveResultSets=true;"
ports:
- "5105:8080"
react-app:
build:
context: ./finviz
dockerfile: Dockerfile
ports:
- "5173:5173"
environment:
- VITE_API_BASE_URL=http://localhost:5105
depends_on:
- server
volumes:
mssql-data:
driver: local
And the server config parts:
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowReactApp",
policyBuilder => policyBuilder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
});
...
app.UseHttpsRedirection();
app.UseCors("AllowReactApp");
app.MapControllers();