Error on Debugging docker-compose on VS2019 - asp.net-core

I have following docker file and docker-compose, it's build and created container images fine through CLI without any problem, however when I try to build it through VS2019 for debugging purpose, it gives an error and I tried to rebuild the project to check where it goes wrong but I could not figure it out. I hope someone point me to the right direction for troubleshooting:-
Docker file:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
# Copy necessary files and restore as distinct layer
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
COPY --from=build-env /app/out .
# Expose ports
EXPOSE 5000/tcp
ENV ASPNETCORE_URLS http://*:5000
HEALTHCHECK --interval=30s --timeout=3s --retries=1 CMD curl --silent --fail http://localhost:5000/hc || exit 1
# Start
ENTRYPOINT ["dotnet", "VehicleManagementAPI.dll"]
Docker-compose file:
services:
vehiclemanagementapi:
image: urgen/vehiclemanagementapi:1.0
build: src/VehiclemanagementAPI
depends_on:
- rabbitmq
- sqlserver
ports:
- "5000"
environment:
- ASPNETCORE_ENVIRONMENT=Production
It is not a particular project has a issue building/compiling docker-compose file rather it happens with all of the projects. As I said it works fine through CLI without any problem, but there has a issue with one particular project, which I wanted to debug in VS2019.
All of the projects build/work fine without dockerize, which means my projects is Okay but issue is with docker-compose or docker file.
Error :
Severity Code Description Project File Line Suppression State
Error CTC1000 (Line: 45, Col: 12, Idx: 954) - (Line: 45, Col: 36, Idx: 978): Exception during deserialization docker-compose C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Sdks\Microsoft.Docker.Sdk\build\Microsoft.VisualStudio.Docker.Compose.targets 304

I have resolved the problem by making changes in following line in docker-compose file, however I am confused why VS 2019 failed to build the project where it works fine in CLI mode, therefore my understanding is if it has a issue with the docker-compose file structure it would not have been build in CLI mode at the first place:-
build: src/VehicleManagementAPI to build: .
Even the error does not tell anything about the problem, error seems pointless/useless for troubleshooting.

Related

How to build container serving Vue SPA using Cloud Native Buildpacks

Currently I'm trying to build container serving VueJS application via Cloud Native Buildpacks.
I already have working Docker file that builds VueJS in production mode and then copy results to nginx image, but I would like to try to use CNB.
So I just have created empty VueJS project for test via vue create vue-tutorial and trying to do with CNB somehting like described there https://cli.vuejs.org/guide/deployment.html#heroku but using CNB.
Does anyone know working recipe how to do that with CNB?
P.S. Currently I'm trying to build that with
pack build spa --path . \  SIGINT(2) ↵  17:22:41
--buildpack gcr.io/paketo-buildpacks/nodejs \
--buildpack gcr.io/paketo-buildpacks/nginx
but getting next error (and I'm not sure that I'm on right way):
===> DETECTING
ERROR: No buildpack groups passed detection.
ERROR: Please check that you are running against the correct path.
ERROR: failed to detect: no buildpacks participating
ERROR: failed to build: executing lifecycle: failed with status code: 100
UPD
My current dockerfile
# build stage
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# production stage
FROM nginx:1.19-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
We chatted about this in Slack, but I wanted to capture it here too:
pack build --buildpack heroku/nodejs --buildpack https://cnb-shim.herokuapp.com/v1/heroku-community/static yourimage
This command may do what you want. The static buildpack used in that example is not yet converted to a cloud native buildpack, but the shim may allow you to build a workable artifact. Then run your image with something like docker run -it -e PORT=5000 -p 5000:5000 yourimagename

Docker build for C# core need the internet connection

I am writing in C# .NET core (VS 2017), using the build for linux containers, using docker-compose.
When I build the image (or publish), there is always reference to the internet, because of nuget usage.
Last error encountered (which I persume of downtime on the nuget, but no matter
- any downtime should not lead to exception in build/publish).
/usr/share/dotnet/sdk/2.1.503/NuGet.targets(114,5): error : Unable to
load the service index for source https://api.nuget.org/v3/index.json. ...
I want to build the image, even the nuget is down, or even there is no interet connection
The yml file look like this:
services:
myProj:
image: my_proj
build:
context: ./all_projects/base_solution/
dockerfile: myProj/Dockerfile
It seems that "dotnet publish..." command call restore from the internet.
If I run publish with --no-restore, the code not compiled, but I want to restore the nuget packages from my own pre-build of my computer.
How can I do it? With no internet connection? Why should I depend on the internet to restore the nuget at each build?
Why cannot I restore nuget package from my own pre-build image, and not always?! get from nuget (actually nuget packages not change occasionally in my code).
May I just copy the folder from the nuget, and not using "COPY" command?
I did the following:
Build an image from a 'common' project, that uses all the nuget package.
Add a reference in the built-image for the new other image, like this:
Create a new common image:
# there is no runtime.
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY TestDock/TestDock.csproj TestDock/
FROM build AS publish
RUN dotnet publish TestDock.csproj -c Release -o /app
And at the original image:
FROM microsoft/dotnet:2.1-runtime AS base
WORKDIR /app
# FROM microsoft/dotnet:2.1-sdk AS build do:
FROM my_common_image AS build
WORKDIR /src
COPY TestDock/TestDock.csproj TestDock/
# Added the following. I tried to copy all, but this doesn't help.
# I persume I can copy part of the common build.
COPY --from=build /app /app
COPY --from=build /src /src
COPY --from=build /usr /usr
FROM build AS publish
RUN dotnet publish TestDock.csproj --no-restore --no-dependecies -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TestDock.dll"]
I have tried to add an intermediate image, with Dockerfile as following:
# This image is base image for all dockers (no need runtime)
# docker build . -f DockerFile_Common -t docker_common
FROM microsoft/dotnet:2.1-sdk AS build
COPY . .
WORKDIR /src/myProj
FROM build AS publish
RUN dotnet publish myProj.csproj -c Release -o /app
RUN dotnet pack /src/myProj.csproj -c Release -o /app
and use it in my image (that I don't want to build using the internet), instead of:
FROM microsoft/dotnet:2.1-sdk AS build
I did:
FROM docker_common AS build
I also tried to add the line: "RUN dotnet publish ..." when setting the workdir as the solution folder, and mark the "Microsoft Visual Studio Offline Packages" (unmark default "nuget.org").
Still, the above doesn't build correctly.
For not using the internet, you can follow the steps.
What is needed is to publish in visual studio, as following:
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-2.2
(Alternative publish in command line).
dotnet publish myProj.csproj -c Release /p:PublishProfile=Properties\PublishProfiles\
<myprofile>.pubxml /p:PublishDir=<proj_folder>\bin\Release\netcoreapp2.1\publish
or put in the Post-build event a line like:
dotnet publish $(ProjectDir)$(ProjectName).csproj -c $(Configuration)
/p:PublishProfile=Properties\PublishProfiles\PublishProfile.pubxml
/p:PublishDir=$(TargetDir)publish --no-build
... and in the Dockerfile I just did with need to do "COPY ..." from the relevant publish directory!
FROM microsoft/dotnet:2.1-runtime
COPY MyProjectFolder/bin/Release/netcoreapp2.1/publish /app
WORKDIR /app
ENTRYPOINT ["dotnet", "myproject.dll"]

Docker for Windows, AspNetCore, and aspnet-webpack Hot Module

I've recently converted my AspNetCore web application to use docker containers for local development and have run into trouble getting the npm module "aspnet-webpack" to work.
When I start the container, I get the following error:
Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: Webpack dev middleware failed because of an error while loading 'aspnet-webpack'. Error was: Error: ENOENT: no such file or directory, lstat 'C:\ContainerMappedDirectories'
Of course, if I comment out the below snippet of code, the error goes away, but I'd appreciate it if anyone has some advice on getting my webpack hot module to work:
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true
});
Here's a simplified snippet of my DockerFile (hope I'm not missing anything):
FROM microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-sac2016 AS base
# Pretend I install nodejs here or the image above already has it
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk-nanoserver-1803 AS build
WORKDIR /src
COPY ["WebApp/WebApp.csproj", "WebApp/"]
RUN dotnet restore "WebApp/WebApp.csproj"
COPY . .
WORKDIR "/src/WebApp"
RUN dotnet build "WebApp.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "WebApp.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApp.dll"]
And a simplified snippet of my docker-compose.yml:
services:
webapp:
image: ${DOCKER_REGISTRY-}webapp
build:
context: .
dockerfile: WebApp\Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://+:80
ports:
- "53760:80"
volumes:
- ${APPDATA}/ASP.NET/Https:C:\Users\ContainerUser\AppData\Roaming\ASP.NET\Https:ro
- ${APPDATA}/Microsoft/UserSecrets:C:\Users\ContainerUser\AppData\Roaming\Microsoft\UserSecrets:ro
- .\WebApp\node_modules:C:/app/node_modules
Notice that I tried mapping my node_modules from my local machine to the container to see if that'd help the hot module to find "aspnet-webpack."

How to publish docker images to docker hub from gitlab-ci

Gitlab provides a .gitlab-ci.yml template for building and publishing images to its own registry (click "new file" in one of your project, select .gitlab-ci.yml and docker). The file looks like this and it works out of the box :)
# This file is a template, and might need editing before it works on your project.
# Official docker image.
image: docker:latest
services:
- docker:dind
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
build-master:
stage: build
script:
- docker build --pull -t "$CI_REGISTRY_IMAGE" .
- docker push "$CI_REGISTRY_IMAGE"
only:
- master
build:
stage: build
script:
- docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" .
- docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
except:
- master
But by default, this will publish to gitlab's registry. How can we publish to docker hub instead?
No need to change that .gitlab-ci.yml at all, we only need to add/replace the environment variables in project's pipeline settings.
1. Find the desired registry url
Using hub.docker.com won't work, you'll get the following error:
Error response from daemon: login attempt to https://hub.docker.com/v2/ failed with status: 404 Not Found
Default docker hub registry url can be found like this:
docker info | grep Registry
Registry: https://index.docker.io/v1/
index.docker.io is what I was looking for.
2. Set the environment variables in gitlab settings
I wanted to publish gableroux/unity3d images using gitlab-ci, here's what I used in Gitlab's project > Settings > CI/CD > Variables
CI_REGISTRY_USER=gableroux
CI_REGISTRY_PASSWORD=********
CI_REGISTRY=docker.io
CI_REGISTRY_IMAGE=index.docker.io/gableroux/unity3d
CI_REGISTRY_IMAGE is important to set.
It defaults to registry.gitlab.com/<username>/<project>
regsitry url needs to be updated so use index.docker.io/<username>/<project>
Since docker hub is the default registry when using docker, you can also use <username>/<project> instead. I personally prefer when it's verbose so I kept the full registry url.
This answer should also cover other registries, just update environment variables accordingly. 🙌
To expand on the GabLeRoux's answer,
I had issues on the pushing stage of the GitLab CI build:
denied: requested access to the resource is denied
By changing my CI_REGISTRY to docker.io (remove the index.) I was able to successfully push.

Nuget CA certificates error on a docker container of Asp.net core

I am trying to build a docker container of an asp.net code application and i get errors while trying to retrieve nuget packages
docker build -t my:container .
Sending build context to Docker daemon 9.58 MB
Step 1 : FROM microsoft/dotnet:latest
---> 3693707d4f7f
Step 2 : COPY . /app
---> Using cache
---> 22a461236738
Step 3 : WORKDIR /app
---> Using cache
---> 8bea2af489ad
Step 4 : RUN dotnet restore
---> Running in 5fbfe078c820
log : Restoring packages for /app/project.json...
error: Unable to load the service index for source https://api.nuget.org/v3/index.json.
error: An error occurred while sending the request.
error: Peer certificate cannot be authenticated with given CA certificates
The command 'dotnet restore' returned a non-zero code: 1
The dockerfile i am using is a pretty standard one and based on microsoft/dotnet:latest container.
FROM microsoft/dotnet:latest
COPY . /app
WORKDIR /app
RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]
EXPOSE 9881/tcp
ENV ASPNETCORE_URLS http://*:9881
ENTRYPOINT ["dotnet", "run"]
This used to work a while ago, something seems to have broken but i have no idea what would that be.
The problem was that i was been using an experimental version 1.12.3 of docker.
Everything works great now that i installed the latest official version on windows (still 1.12.3 but not marked as experimental).