C
C#7mo ago
Rowin ツ

Docker container not port forwarding

Hello, yesterday I was in here as well regarding some problems with my port forwarding. Im trying to get my docker container to run on localhost:8080 and also make use of some certificates because I want to use another local project to test out the endpoints on my api which I am trying to deploy. I have the following code Dockerfile:
# Stage 1: Base image with HTTPS setup
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
ENV HOME=/root

USER root
WORKDIR /app
ENV DOTNET_URLS=https://+:8080
EXPOSE 8080

# Ensure the /app/certs directory exists and copy the HTTPS certificate
RUN mkdir -p /app/certs
COPY "Certs/aspnetapp.pfx" "/app/certs/aspnetapp.pfx"
RUN chown -R app:app /app/certs
USER app


# Stage 2: Build the application
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["authapi.csproj", "."]
RUN dotnet restore "./authapi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./authapi.csproj" -c $BUILD_CONFIGURATION -o /app/build

# Stage 3: Publish the application
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./authapi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# Stage 4: Final stage to run the application
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "authapi.dll"]
# Stage 1: Base image with HTTPS setup
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
ENV HOME=/root

USER root
WORKDIR /app
ENV DOTNET_URLS=https://+:8080
EXPOSE 8080

# Ensure the /app/certs directory exists and copy the HTTPS certificate
RUN mkdir -p /app/certs
COPY "Certs/aspnetapp.pfx" "/app/certs/aspnetapp.pfx"
RUN chown -R app:app /app/certs
USER app


# Stage 2: Build the application
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["authapi.csproj", "."]
RUN dotnet restore "./authapi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./authapi.csproj" -c $BUILD_CONFIGURATION -o /app/build

# Stage 3: Publish the application
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./authapi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# Stage 4: Final stage to run the application
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "authapi.dll"]
Docker-compose:
services:
authapi:
image: authapi:latest
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
- ASPNETCORE_ENVIRONMENT=Development
networks:
- authapi-network
volumes:
- /certs:/Certs

networks:
authapi-network:
driver: bridge
services:
authapi:
image: authapi:latest
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
- ASPNETCORE_ENVIRONMENT=Development
networks:
- authapi-network
volumes:
- /certs:/Certs

networks:
authapi-network:
driver: bridge
Appsettings:
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://localhost:8080",
"Certificate": {
"Path": "./certs/aspnetapp.pfx",
"Password": "<My valid password is here>"
}
}
}
},
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://localhost:8080",
"Certificate": {
"Path": "./certs/aspnetapp.pfx",
"Password": "<My valid password is here>"
}
}
}
},
Launchsettings:
"profiles": {
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/public",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:8080"
},
"certificates": {
"Default": {
"path": "Certs/aspnetapp.pfx",
"password": "<My valid password>"
}
},
"profiles": {
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/public",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:8080"
},
"certificates": {
"Default": {
"path": "Certs/aspnetapp.pfx",
"password": "<My valid password>"
}
},
55 Replies
Rowin ツ
Rowin ツOP7mo ago
When i run docker compose up --build i get the following result
Rowin ツ
Rowin ツOP7mo ago
No description
Rowin ツ
Rowin ツOP7mo ago
No description
Rowin ツ
Rowin ツOP7mo ago
Though when I am trying to connect to localhost:8080 it doesnt give me a response from the website nor do i get any response inside the logs It also doesnt seem to not properly copy over the certs like ive requested in my dockerfile
Rowin ツ
Rowin ツOP7mo ago
No description
Rowin ツ
Rowin ツOP7mo ago
No description
Rowin ツ
Rowin ツOP7mo ago
# Ensure the /app/certs directory exists and copy the HTTPS certificate
RUN mkdir -p /app/certs
COPY "Certs/aspnetapp.pfx" "/app/certs/aspnetapp.pfx"
RUN chown -R app:app /app/certs
USER app
# Ensure the /app/certs directory exists and copy the HTTPS certificate
RUN mkdir -p /app/certs
COPY "Certs/aspnetapp.pfx" "/app/certs/aspnetapp.pfx"
RUN chown -R app:app /app/certs
USER app
canton7
canton77mo ago
Did you get output indicating the docker image was actually being rebuilt?
Rowin ツ
Rowin ツOP7mo ago
No description
Rowin ツ
Rowin ツOP7mo ago
Id assume so? Im quite new when it comes to certificates but I supposedly need them to have my local react application talk to my local api
canton7
canton77mo ago
Just for interest, try docker-compose build --no-cache --progress=plain. See the actual output from the build process
canton7
canton77mo ago
Oh. You put your certs in your base image? Same with EXPOSE, DOTNET_URLS, etc
Rowin ツ
Rowin ツOP7mo ago
Honestly Im just following what people on the internet are doing
canton7
canton77mo ago
Ah, no. You do FROM base AS final
Rowin ツ
Rowin ツOP7mo ago
Im not instantly seeing anything thats problematic
canton7
canton77mo ago
What makes you think it isn't copying over the keys?
Rowin ツ
Rowin ツOP7mo ago
as the 2 screenshots I send above It seems like it did create a mount folder called Certs But it is empty
canton7
canton77mo ago
I'm not sure what those screenshots are from, and the second one does show the certs?
Rowin ツ
Rowin ツOP7mo ago
No description
canton7
canton77mo ago
/certs:/Certs -- your certs are in /app/certs no?
Rowin ツ
Rowin ツOP7mo ago
Inside the app folder is my certs folder which holds the certificate
Rowin ツ
Rowin ツOP7mo ago
No description
canton7
canton77mo ago
So what is the /certs:/Certs line trying to do?
Rowin ツ
Rowin ツOP7mo ago
It should change the ownership of the directory and its ocntents to the user "app" and the group "app" afaik This is yet again just something I see everyone doing online
canton7
canton77mo ago
Do you know what the line /certs:/Certs does? It creates a bind mount from /certs in the container, to /Certs on your local filesystem. But /certs in the container doesn't contain your certificates -- you put them in /app/certs. So it's no surprise that /certs and /Certs are empty! I'm also not sure what you're trying to do with that bind mount. You've put the certs inside the docker image already. Why are you then trying to bind that folder back to your local filesytem?
Rowin ツ
Rowin ツOP7mo ago
🥲 So I dont even need that piece of code? As that cert file is already being copied because its inside my project? I just need to reference everything to that folder?
canton7
canton77mo ago
It's really hard to help someone whose question is "I'm not going to tell you what I'm trying to do, but I've copied this random bit of code from the internet, and it doesn't do what I expect. But I'm not going to tell you what I want it to do" What are you trying to do?
Rowin ツ
Rowin ツOP7mo ago
Im sorry, ill rephrase my question.
canton7
canton77mo ago
Do you want your certs to be in /certs or /app/certs? Do you want them to even be in the docker image, or do you want them to be on your local filesystem, and you bind mount them into your container?
Rowin ツ
Rowin ツOP7mo ago
So as I mentioned before I am trying to talk from my local ReactJs project to my API which I want to run in docker. Initially I just had it run like anything else and it worked just fine. But after calling one of my endpoints using "localhost:8080" I've gotten shown a log in which I was required to add a certificate to trust "dotnet dev-certs https --trust" though it did create the pfx file it didnt seem to be present in my docker container which remained giving me that same problem. So I guess I want my API to accept that certificate inside my docker container so these problems can be resolved.
canton7
canton77mo ago
Could you answer my two questions above please?
Rowin ツ
Rowin ツOP7mo ago
in /cerstI suppose
canton7
canton77mo ago
You have a mix of "Path": "./certs/aspnetapp.pfx", and "path": "Certs/aspnetapp.pfx", currently
Rowin ツ
Rowin ツOP7mo ago
Im not quite sure which one is beneficial, but I reckon just in my docker image
canton7
canton77mo ago
It depends on whether you want them to be present in the image itself -- so anyone who gets a copy of the image also gets a copy of the certs -- or whether you want to keep them a bit more private and only provide them when you actually run the container
Rowin ツ
Rowin ツOP7mo ago
The latter I'd prefer
canton7
canton77mo ago
OK, so: 1. Remove the code to copy the key into the docker image (the COPY "Certs/aspnetapp.pfx" "/app/certs/aspnetapp.pfx" etc) 2. If you want them in /certs, keep the bind mount the same, but change your "Path": "./certs/aspnetapp.pfx" to /Certs/aspnetapp.pfx (I think?) 3. Manually copy the certs into the Certs mount folder after the container is created
Rowin ツ
Rowin ツOP7mo ago
authapi-1 | Unhandled exception. System.IO.FileNotFoundException: Could not find file '/Certs/aspnetapp.pfx'.
"Certificate": {
"Path": "/Certs/aspnetapp.pfx",
"Certificate": {
"Path": "/Certs/aspnetapp.pfx",
Ive removed the copy line
canton7
canton77mo ago
So yep, copy the certs into the Certs folder manually
Rowin ツ
Rowin ツOP7mo ago
No description
canton7
canton77mo ago
Oh sorry, it's /certs inside the container, not /Certs You mount /Certs to /certs
Rowin ツ
Rowin ツOP7mo ago
Im a bit lost now, Did you want me to also remove the other code after that COPY inside my dockerfile or just that line?
canton7
canton77mo ago
"Path": "./certs/aspnetapp.pfx" needs to be "Path": "/certs/aspnetapp.pfx", not "Path": "/Certs/aspnetapp.pfx"
Rowin ツ
Rowin ツOP7mo ago
Yup I have that now
canton7
canton77mo ago
And?
Rowin ツ
Rowin ツOP7mo ago
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://localhost:8080",
"Certificate": {
"Path": "/certs/aspnetapp.pfx",
"Password": ""
}
}
}
},
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://localhost:8080",
"Certificate": {
"Path": "/certs/aspnetapp.pfx",
"Password": ""
}
}
}
},
authapi-1 | Unhandled exception. System.IO.FileNotFoundException: Could not find file '/certs/aspnetapp.pfx'. C:\Users\r\source\repos\authapi\Certs\aspnetapp.pfx
canton7
canton77mo ago
Can you look here again?
Rowin ツ
Rowin ツOP7mo ago
It doesnt even get that far to show me the files inside the container authapi-1 | Hosting failed to start authapi-1 | System.IO.FileNotFoundException: Could not find file '/certs/aspnetapp.pfx'. authapi-1 | File name: '/certs/aspnetapp.pfx'
Rowin ツ
Rowin ツOP7mo ago
No description
Rowin ツ
Rowin ツOP7mo ago
docker compose build goes fine
canton7
canton77mo ago
Hmm, that's irritating. Open a shell directly in the container, something like docker run --rm -it --entrypoint bash authapi:latest Ah, no, that won't do the bind mount Change your ENTRYPOINT to /bin/bash for the moment, start in docker-compose, then docker exec -it authapi-authapi-1 bash
Rowin ツ
Rowin ツOP7mo ago
ENTRYPOINT ["/bin/bash"]
ENTRYPOINT ["/bin/bash"]
\authapi> docker exec -it authapi-authapi-1 bash What's next: Try Docker Debug for seamless, persistent debugging tools in any container or image → docker debug authapi-authapi-1 Learn more at https://docs.docker.com/go/debug-cli/ Error response from daemon: container bd6f4730420cd78b766435487f0e4deb4c127e59c624261b4b98370f803546fa is not running
canton7
canton77mo ago
You need to start it with docker-compose
Rowin ツ
Rowin ツOP7mo ago
https://paste.ofcode.org/4SXvtJTJAKsAFs7cqHpgD7 thats what I did no? or am i overlooking something Well I think ive managed to solve the problem I redid everything, even generated a new certificate And now it works
Want results from more Discord servers?
Add your server