Cant connect to postgresql with docker-compose - asp.net

I am facing the problem the host is not found or an error connecting to this hosting
I have tried many different options but have not found a solution to the problem.
My docker-compose file
version: '3.4'
services:
backend:
image: webapiwithdockerpostgre
build:
context: .
dockerfile: WebApiWithDockerPostgre/Dockerfile
database:
image: postgres
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: superpassworddocker
POSGRES_DB: TestDb
volumes:
- database-data:/var/lib/postgresql/data/
volumes:
database-data:
pgadmin:
My Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["WebApiWithDockerPostgre/WebApiWithDockerPostgre.csproj", "WebApiWithDockerPostgre/"]
RUN dotnet restore "WebApiWithDockerPostgre/WebApiWithDockerPostgre.csproj"
COPY . .
WORKDIR "/src/WebApiWithDockerPostgre"
RUN dotnet build "WebApiWithDockerPostgre.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApiWithDockerPostgre.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApiWithDockerPostgre.dll"]
Connection string -
"Host=database;Database=DbTest;Username=admin;Password=superpassworddocker;Port=5432"

Should the database name be TestDb instead of DbTest in your connection string?

I solved my problem, just change Host=database in the connection string to Host=host.docker.internal and it works fine

Related

Why is the image contains the source files when build through docker compose?

I have this docker file -
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS publish
WORKDIR /src
COPY . .
RUN dotnet publish -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "HrManagement.dll"]
placed in
When building the image through the windows cmd, and running the image subsequently, i see that the app folder lists the below files -
However, when i build via docker-compose file in Visual Studio, I see the below ones-
Additionally, i see the src folder as well in the container -
Why is this difference seen when building the SAME docker file through the above methods?
The below is the docker-compose.yml file-
version: '3.4'
services:
hrmanagement:
image: ${DOCKER_REGISTRY-}hrmanager
build:
context: ./HrManagement
dockerfile: ./Dockerfile
sqldata:
image: mcr.microsoft.com/mssql/server:latest
environment:
- SA_PASSWORD=mypass
- ACCEPT_EULA=Y
ports:
- "1450:1433"
volumes:
- sql-server-vol:/var/opt/mssql
volumes:
sql-server-vol:
external: true
placed in

Docker build and Github actions unable to prepare context: path "..."

I've a weird issue with docker build in combination with GitHub actions.
The error I'm receiving is: unable to prepare context: path "..." not found Error: Process completed with exit code 1.
This error only occurs on the GitHub Actions, because when I run the same command locally on my laptop, it works fine.
cd services/Gateway/OcelotGateway
docker build -f Dockerfile ... -t stanpanman/ocelotgateway:1.0.0
Are three dots not a thing inside the RUN command, i'm so confused?
My GitHub actions:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: docker login
env:
DOCKER_USER: '${{ secrets.DOCKER_USER }}'
DOCKER_PASSWORD: '${{ secrets.DOCKER_PASSWORD }}'
run: |
docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
- name: docker build ocelotgateway
run: |
cd services/Gateway/OcelotGateway
docker build ... -t stanpanman/ocelotgateway:1.0.0
My Docker image:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Gateway/OcelotGateway/OcelotGateway.csproj", "Gateway/OcelotGateway/"]
RUN dotnet restore "Gateway/OcelotGateway/OcelotGateway.csproj"
COPY . .
WORKDIR "/src/Gateway/OcelotGateway"
RUN dotnet build "OcelotGateway.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "OcelotGateway.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "OcelotGateway.dll"]
How my file structure looks like
I've also tried to run the command:
cd services/Gateway/OcelotGateway
docker build . -t stanpanman/ocelotgateway:1.0.0
But then I'm receiving this error:
---> Running in d12aaca83aeb
COPY failed: file not found in build context or excluded by .dockerignore: stat Gateway/OcelotGateway/OcelotGateway.csproj: file does not exist
Removing intermediate container d12aaca83aeb
---> cd903f3ffb15
Step 7/17 : COPY ["Gateway/OcelotGateway/OcelotGateway.csproj", "Gateway/OcelotGateway/"]
Error: Process completed with exit code 1.
I'm quite new with Docker, I expect that something isn't correct in my file structure. Help is really, really, really appreciated!

Docker doesn't copy new images

I have a problem regarding Docker.
When I'm deploying a new version of my app image, the images i have added to the images folder in my wwwroot folder aren't copied..
My Dockerfile looks like this:
FROM microsoft/aspnetcore-build:1.0-projectjson
WORKDIR /app-src
COPY . .
RUN dotnet restore
RUN dotnet publish src/Test -o /app
EXPOSE 5000
WORKDIR /app
ENTRYPOINT ["dotnet", "Test.dll"]
And my docker-compose:
version: '3.8'
services:
app:
image: <dockeruser>/<imagename>:<tag>
links:
- db
environment:
ConnectionStrings__Dataconnection: "Host=db;Username=Username;Password=Password;Database=db"
ports:
- "5000:5000"
volumes:
- ~/data/images:/app/wwwroot/images
db:
image: postgres:9.5
ports:
- "31337:5432"
volumes:
- ~/data/db:/var/lib/postgresql/data/pgdata
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: db
PGDATA: /var/lib/postgresql/data/pgdata
My current version of docker is:
Docker version 18.09.7, build 2d0083d and docker-compose docker-compose version 1.26.2, build eefe0d31
The exact same files (except for the docker-compose version was set to 2 in docker-compose.yml) worked previously on docker version Docker version 17.03.0-ce, build 60ccb22 and docker-compose version docker-compose version 1.9.0, build 2585387
I store my new images in my repos wwwroot/images folder, and then push them to the repo, and then dockerhub automatically builds an image from the new commit. On the server i then pull the new docker-image and run the docker-compose down -v command followed by docker-compose up -d but the images is not available in the app afterwards.
Disclaimer: This is a project I have overtaken and I'm aware of some of the very old software versions.
Your images may be in your container image, but since you are doing a bind mount whatever is in your server’s “~/data/images” directory will basically “override/replace” what’s in your image when the container is created.
Try removing the volume from the app service, basically remove this:
volumes:
- ~/data/images:/app/wwwroot/images
The other thing you can try is to manually copy the images to the “~/data/images” directory on the server.

C# app runs fine on VM, but not on docker

Have a small ftp server with http in a app, which runs fine in a vm.
I'm trying to dockerize it, which when I do, works ok If I ssh in the container (I can telnet both localhost and the container VM), but I can't access from the machine.
Here is my dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["proj.csproj", "proj"]
RUN dotnet restore "proj/proj.csproj"
COPY . .
WORKDIR "/src/proj"
RUN dotnet build "proj.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "proj.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
EXPOSE 21
EXPOSE 80
ENV ASPNETCORE_ENVIRONMENT=development
ENTRYPOINT ["dotnet", "proj.dll", "--urls", "http://0.0.0.0:80"]
Docker inspect:
"Ports": {
"21/tcp": null,
"80/tcp": null
PS: I changed the paths manually to not expose my project, so may have some mistakes with folders name

How do I go about installing a certificate on a nanoserver-1709 based image on a docker container?

I've been looking around this image for any cert managers but I can't find anything that will help me install a .cer certificate. The 1709 nanoserver image doesn't come with powershell so in order to use that I would have to do a multi-staged build with the microsoft/windowsservercore image but I'm not quite sure how I'd go about doing this, I can't seem to find anything through google that will help.
If anybody knows a way to install the cert with or withour a multi-stage build that would be very appreciated.
For those interested, here's my docker-compose.yml and Dockerfile
docker-compose.yml
version: '3'
services:
myapp:
image: myapp
ports:
- "5000:80"
build:
context: .
dockerfile: MyApp\Dockerfile
container_name: "myapp"
hostname: "myapp"
depends_on:
- db
db:
image: "microsoft/mssql-server-windows-express"
environment:
SA_PASSWORD: ""
ACCEPT_EULA: "Y"
container_name: "myapp"
hostname: "myapp"
Dockerfile
FROM microsoft/aspnetcore:2.0-nanoserver-1709 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0-nanoserver-1709 AS build
WORKDIR /src
COPY MyApp.sln ./
COPY MyApp/MyApp.csproj MyApp/
RUN dotnet restore -nowarn:msb3202,nu1503
COPY . .
WORKDIR /src/MyApp
RUN dotnet restore
RUN dotnet ef database update
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyApp.dll"]
Thanks!
On some images, there is a tool called certoc.exe that allows you to import a certificate (usage: certoc.exe -addstore root my_root_certificate.cer).
This tool is not present on the 1709 images, but is present on images such as microsoft/nanoserver:sac2016.
To sum-up, the best way I found to deal with that is to change the Dockerfile to contain something like:
FROM microsoft/nanoserver:sac2016 as tool
COPY --from=tool /Windows/System32/certoc.exe .
USER ContainerAdministrator
RUN certoc.exe -addstore root my_root_certificate.cer
Complete example available here: https://pvlerick.github.io/2018/11/how-to-run-an-https-asp.net-core-app-using-test-certificates-in-nanoserver-1709-1803-with-docker
A huge thanks to Joshua Chini here: https://joshuachini.com/2018/02/08/how-to-import-an-enterprise-certificate-into-a-windows-container/

Resources