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

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/

Related

Cant connect to postgresql with docker-compose

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

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 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.

Asp.Net Core on Docker

I'm currently trying to launch a Docker component with a ASP.NET Core application.
I use the following repo for my test : https://github.com/aspnet/cli-samples
I ran the following commande without any issue :
git clone https://github.com/aspnet/cli-samples aspnet-Home
cd aspnet-Home/HelloWeb
cat Dockerfile
FROM microsoft/aspnetcore
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "helloweb.dll"]
sudo docker build –t helloweb .
sudo docker run -d helloweb
The image is visible using the sudo docker images, but the container doesn't launch and is not visible with sudo docker ps:
And if I browse my website, obsviously I do not see data.
Is the repo not good for my test ? Is there any mistake I do on the docker container creation ?
Running the -it command give me the following output:
The error you highlighted is because helloweb.dll you set as the ENTRYPOINT doesn't exist. There could be two reasons for it
1 You didn't build the project yet
In this case you should run dotnet restore from the project home directory, then navigate to HelloWeb directory and run dotnet publish. When I run this command, I see the following:
publish: Published to /code/HelloWeb/bin/Debug/netcoreapp1.0/publish
Published 1/1 projects successfully
2 You built the project, but the ENTRYPOINT path is wrong
COPY . . directive will copy everything from the current directory into your app directory. That means HelloWeb.dll will actually be in bin/Debug/netcoreapp1.0/publish/ (or bin/Release/... for release builds).
Option 1: Modify your entrypoint with the full path
ENTRYPOINT ["dotnet", "bin/Debug/netcoreapp1.0/publish/HelloWeb.dll"]
Your application should happily start and serve requests.
Option 2: Modify your COPY directive
Once your project has been published, everything you'll need to run it will be in the publish directory. You could copy the contents of that into the /app directory and your entrypoint will be correct. That would look like this
FROM microsoft/aspnetcore
WORKDIR /app
COPY ./bin/Debug/netcoreapp1.0/publish/ .
EXPOSE 80
ENTRYPOINT ["dotnet", "HelloWeb.dll"]
You will also probably want to add the EXPOSE directive to tell Docker that your container will be listening on port 80.
When that succeeds, you should see (if you run in interactive mode)
docker run -it helloweb
Hosting environment: Production
Content root path: /app
Now listening on: http://+:80
Application started. Press Ctrl+C to shut down.
You could also use the microsoft/dotnet:latest image instead. That image comes with the SDK installed and a very convenient run command. The Dockerfile would look like this
FROM microsoft/dotnet:latest
COPY . /app
WORKDIR /app
RUN dotnet restore
ENV ASPNETCORE_URLS http://*:5000
EXPOSE 5000
ENTRYPOINT ["dotnet", "run"]
and you should be able to modify your source and build and run your container.

unable to process Dockerfile: unable to parse repository info: repository name component must match

Hi I am following the guidance on this blog post, "Managing containerized ASP.NET Core apps with Kubernetes"
https://cloudplatform.googleblog.com/2016/10/managing-containerized-ASP.NET-Core-apps-with-Kubernetes.html
I am stuck at the stage where you get docker to build the image
docker build -t gcr.io/xxxx/hello-dotnet:v1 .
This is the error I am getting.
unable to process Dockerfile: unable to parse repository info: repository name component must match "[a-z0-9](?:-*[a-z0-9])*(?:[._][a-z0-9](?:-*[a-z0-9])*)*"
Contents of my Dockerfile are
FROM microsoft/dotnet:1:1.0.1-core
COPY . /app
WORKDIR /app
RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]
EXPOSE 8080/tcp
ENV ASPNETCORE_URLS http://*:8080
ENTRYPOINT ["dotnet", "run"]
The first line of your Dockerfile should be:
FROM microsoft/dotnet:1.0.1-core

Resources