docker-compose container not firing up - docker

New to docker and I have an empty .NET Core API project and an empty vuejs frontend project. I simply want both to be up and running before beginning development. I have a project that has two docker files (one for the backend and one for the frontend). I have a docker-compose.yml file in which I am running the docker-compose up command and my frontend launches fine while my backend does not. I am running in Linux containers.
My project structure is the following
docker-compose.yml
version: '3.4'
services:
backend:
image: backend
build:
context: ./backend
dockerfile: Dockerfile
frontend:
image: frontend
build:
context: ./frontend
dockerfile: Dockerfile
Backend Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal AS base
WORKDIR /app
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build
WORKDIR /src
COPY ["backend/backend.csproj", "backend/"]
RUN dotnet restore "backend/backend.csproj"
COPY . .
WORKDIR "/src/backend"
RUN dotnet build "backend.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "backend.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "backend.dll"]
Frontend Dockerfile
FROM node:lts-alpine
# install simple http server for serving static content
RUN npm install -g http-server
# make the 'app' folder the current working directory
WORKDIR /app
# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./
# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .
# install project dependencies
RUN npm install
# build app for production with minification
RUN npm run build
CMD [ "http-server", "dist" ]
Error when running docker-compose up
=> ERROR [backend build 7/7] RUN dotnet build "backend.csproj" -c Release -o /app/build 8.1s
=> [frontend 1/7] FROM docker.io/library/node:lts-alpine#sha256:dc92f36e7cd917816fa2df041d4e9081453366381a00f40398d99e9392e78664 0.0s
=> CANCELED [frontend internal] load build context 7.0s
=> => transferring context: 35.13MB 7.0s
------
> [backend build 7/7] RUN dotnet build "backend.csproj" -c Release -o /app/build:
#19 1.015 Microsoft (R) Build Engine version 16.11.1+3e40a09f8 for .NET
#19 1.015 Copyright (C) Microsoft Corporation. All rights reserved.
#19 1.015
#19 2.634 Determining projects to restore...
#19 3.501 All projects are up-to-date for restore.
#19 7.322 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/backend/backend.csproj]
#19 7.334
#19 7.334 Build FAILED.
#19 7.334
#19 7.334 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/backend/backend.csproj]
#19 7.334 0 Warning(s)
#19 7.334 1 Error(s)
#19 7.334

I'd guess you should look at the copy statements in your backend Dockerfile before the build step. Looks like youre copying your source files or your csproj file into the wrong directories.
Lets break down whats happening in the build stage of your backend Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build # Use the public dotnet/sdk image as our base build image
WORKDIR /src # Set the current working directory inside our build image to /src
COPY ["backend/backend.csproj", "backend/"] # Copy the file ./backend/backend.csproj from our host file system into the build image to the directory ./backend (i.e. /src/backend)
RUN dotnet restore "backend/backend.csproj" # Restore our dotnet dependencies
COPY . . # Copy all files from the current directory on our host system to the current directory inside our build image (i.e. /src)
WORKDIR "/src/backend" # Set our current directory inside the build image to /src/backend
RUN dotnet build "backend.csproj" -c Release -o /app/build # Build our dotnet application
You are copying the entire directory into your build image and the resulting file structure would look something like this:
/src/
|- frontend/
| |- ...
|- backend/
|- Program.cs
|- Startup.cs
|- WeatherForcast.cs
|- backend.csproj
|...
|- backend/
|- backend.csproj
You're then executing a dotnet build against the file /src/backend/ which based on the folder structure you shared above expects your source files to be in the same folder.
In your backend Dockerfile try:
...
COPY ["backend/", "./backend/"]
RUN dotnet restore "backend/backend.csproj"
WORKDIR "/src/backend"
RUN dotnet build "backend.csproj" -c Release -o /app/build
...

Related

Gitlab CI .NET6 docker build issue

I am working on a GitLab CI/CD project to build a NET6 application into a docker image, which is pushed to AWS ECR. My issue is manifested in the continuous failure of 'COPY . .' or any other command after 'restore' of my targeted project.
Repo structure:
repo root
- projectFolder
- projectAPI
- projectAPI.csproj
- DockerFile
- nuget.config
- referencedClassLibrary1
- referencedClassLibrary2
- .sln
- gitlab-ci.yml
- package.json
Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
EXPOSE 80
WORKDIR /app
ARG Nuget_CustomFeedUserName
ARG Nuget_CustomFeedPassword
COPY "TT.CS/TT.CS.API/nuget.config" ""
COPY "TT.CS/TT.CS.DAL/TT.CS.DAL.csproj" "TT.CS.DAL/"
COPY "TT.CS/TT.CS.BLL/TT.CS.BLL.csproj" "TT.CS.BLL/"
COPY "TT.CS/TT.CS.API/TT.CS.ConfigAPI.csproj" "TT.CS.API/"
RUN ls -al
RUN sed -i "s|</configuration>|<packageSourceCredentials><aws><add key=\"Username\" value=\"${Nuget_CustomFeedUserName}\" /><add key=\"ClearTextPassword\" value=\"${Nuget_CustomFeedPassword}\" /></aws></packageSourceCredentials></configuration>|" /app/nuget.config
RUN dotnet restore --configfile "/app/nuget.config" "TT.CS.API/TT.CS.ConfigAPI.csproj"
COPY . .
WORKDIR "/app/TT.CS.API"
RUN dotnet publish --no-restore -c Release -o /app/out "TT.CS.ConfigAPI.csproj"
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "TT.CS.ConfigAPI.dll"]
Outcome:
#17 3.891 Restored /app/TT.CS.BLL/TT.CS.BLL.csproj (in 1.58 sec).
#17 3.891 Restored /app/TT.CS.DAL/TT.CS.DAL.csproj (in 1.58 sec).
#17 3.941 Restored /app/TT.CS.API/TT.CS.ConfigAPI.csproj (in 1.67 sec).
#17 DONE 4.1s
#18 [build-env 11/14] COPY . .
#18 sha256:23c9d357832b3a7c1a8c5d607316f97c2fa09b51818f473a97f709f9cfeb981f
#18 ERROR: failed to prepare i2owcagafc3j7shg0slffonae: invalid argument
------
> [build-env 11/14] COPY . .:
------
failed to prepare i2owcagafc3j7shg0slffonae: invalid argument
I've created a solution with a single folder sln/csproj without referencing the same solution projects, which works as expected but I'm still having issues with more complex solutions that combine multiple referenced projects with public and private nuget repos.
Does anyone see some obvious mistake? Thanks

cannot run docker build when using docker setup from Visual Studio

I created a web-project named ProjectService and added docker-support for it using Visual Studio 2022. I can build and debug the image pretty well from within VS.
Now I try to build and run the image from the command-line in order to have it within my build-pipeline. So I execute this from the root-directory of my repo:
docker build -t myrep/demo:latest ./ProjectService
However when I do that I get the following error:
#11 ERROR: "/ProjectService/ProjectService.csproj" not found: not found
#12 [build 4/8] COPY [DatabaseManager/DatabaseManager.csproj, DatabaseManager/]
#12 sha256:b881c00e01ebb7ea687c2f8c5d5f585e237bf6151b63cd21110ed1a7bdf74af6
#12 ERROR: "/DatabaseManager/DatabaseManager.csproj" not found: not found
I think this is because within my docker-file paths are relative:
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 ["ProjectService/ProjectService.csproj", "ProjectService/"]
COPY ["DatabaseManager/DatabaseManager.csproj", "DatabaseManager/"]
RUN dotnet restore "ProjectService/ProjectService.csproj"
COPY . .
WORKDIR "/src/ProjectService"
RUN dotnet build "ProjectService.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ProjectService.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectService.dll"]
when I execute docker from within the ProjectService-drectory itself, I get the same error, though.
My folder-structure is this:
root
ProjectService
ProjectService.csproj
DockerFile
DatabaseManager
DatabaseManager.csproj
where ProjectService depends on DatabaseManager.
Your Dockerfile looks like it assumes that the build context is the root directory.
So to build it you can either do it from the root directory with
docker build -t myrep/demo:latest -f ProjectService/Dockerfile .
or from the ProjectService directory with
docker build -t myrep/demo:latest ..
A 3rd option is to move the Dockerfile to the root directory. Then your Dockerfile will be in the directory that's assumed to be the build context. That's how most projects are organized. Then you can build with
docker build -t myrep/demo:latest .

ASP.NET Core build docker image having shared libraries

I'm working in a microservice environemnt where there are shared libraries between these microservices.
These libraries are stored in a different location on the disk, a "shared" location, and are directly referenced by these microservices.
In a .csproj of such a microservice you would see something like this.
<ItemGroup>
<ProjectReference Include="..\..\..\Shared1.csproj" />
<ItemGropu>
Now in each of these microservices I have a dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY . .
COPY ../../../Users/boris/Desktop/shared/Shared/Shared/Shared.csproj .
RUN dotnet restore "WebUi.csproj"
WORKDIR /src/.
RUN dotnet build "WebUi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebUi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
RUN ls
ENTRYPOINT ["dotnet", "WebUi.dll"]
when building a image
docker build -f dockerfile .
I'm getting
ERROR [build 4/7] COPY ..\..\..\Users\boris\Desktop\shared\Shared\Shared\Shared.csproj .
[build 4/7] COPY
......\Users\boris\Desktop\shared\Shared\Shared\Shared.csproj .:
failed to solve with frontend dockerfile.v0: failed to build LLB:
failed to compute cache key:
"/..\..\..\Users\boris\Desktop\shared\Shared\Shared\Shared.csproj"
not found: not found PS C:.demos\hangfire-dashboard\WebUi> docker
build -f .\dockerfile -t web . [+] Building 0.2s (10/18)
I'm thinking it's because of the build context, but not sure.
Any ideas on how can I fix this ?
Ok first thing first rename the dockerfile to Dockerfile. Next up is the path correct in the first place? I assume your csproj exists in the same folder as your Dockerfile and in your csproj you use <ProjectReference Include="..\..\..\Shared1.csproj" /> whereas in the docker file you use ../../../Users/boris/Desktop/shared/Shared/Shared/Shared.csproj so if the csproj AND the Dockerfile exist in the same directory the path is simply incorrect.

VS 2017 PermissionError: [Errno 13] Permission denied: 'v15\\Server\\sqlite3\\db.lock'

While working on a ServiceFabric based solution and building a docker image I am getting the following because of my directory structure :
My directory structure is
D:
D1:
src:
Foo.Fabric:
Foo.Fabric: -----------------> This is context right now
.vs
Foo.Service:
DockerFile
Foo.Service.csproj
docker-compose.yml
.dockerignore
Foo.Lib:
Foo.Lib:
Foo.Lib.csproj
The content of my docker-compose.yml is like :
version: '3.4'
services:
foo:
image: foodockerimage
build:
context: .
dockerfile: Foo.Service/Dockerfile
And the content of my dockerfile is as follows
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY Foo.Service/Foo.Service.csproj Foo.Service/
COPY ../Foo.Lib/Foo.Lib/Foo.Lib.csproj ../Foo.Lib/Foo.Lib/
RUN dotnet restore Foo.Service/Foo.Service.csproj
COPY . .
WORKDIR /src/Foo.Service
RUN dotnet build Foo.Service.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish Foo.Service.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Foo.Service.dll"]
As we can see here clearly that I need to build this Lib.csproj along with my main Foo.Service.csproj The problem is the directory location of Lib project. Becuase this is used by some other solutions, I can not not move this Lib project inside the Foo.Fabric folder.
Now initially I got the error of "Service 'foo' failed to build: COPY failed: Forbidden path outside the build context: ../Foo.Lib/Foo.Lib/Foo.Lib.csproj", OK I got this, I have a wrong context so I updated in docker-compose.yml as
build: context: . to build: context: ..
to change the context to the parent directory of the current directory, so again I will have to change the paths of dockerfile resulting a docker-compose.yml as
version: '3.4'
services:
foo:
image: foodockerimage
build:
context: ..
dockerfile: Foo.Fabric/Foo.Service/Dockerfile
And also
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY Foo.Fabric/Foo.Service/Foo.Service.csproj Foo.Fabric/Foo.Service/ #Updated
COPY Foo.Lib/Foo.Lib/Foo.Lib.csproj Foo.Lib/Foo.Lib/ #Updated
RUN dotnet restore Foo.Service/Foo.Service.csproj
COPY . .
WORKDIR /src/Foo.Service
RUN dotnet build Foo.Service.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish Foo.Service.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Foo.Service.dll"]
Now I have this error "PermissionError: [Errno 13] Permission denied: '\\?\D:\D1\src\Foo.Fabric\.vs\Foo.Fabric\v15\Server\sqlite3\db.lock'"
I am really banging my haed with this and couldn't get up.
By the way I am using :
Docker Desktop Community Version 2.0.0.0-win81(29211) Engine: 18.09.0
Visual Studio Professional 2017 15.9.3
Windows 10 1809
Make sure to exclude the .vs folder from your build, by using a '.dockerignore' file next to the dockerfile. Usually they look like this, for VS projects:
.dockerignore
.env
.git
.gitignore
.vs
.vscode
docker-compose.yml
docker-compose.*.yml
*/bin
*/obj

DockerFile for dotnetcore2.0 with multiple cs project references

I am running a .netcore2.0 webapi using Visual studio 2017 for MAC.
I have a project structure as below.
src
--Solution.sln
-- Common
-- commonPrj
-- Common.csproj
--Data
-- QM
-- QM.csproj
--Repo
--Repo.csproj
--Ser
-- Host
-- Api
--api.csproj
I have the DOCKERFILE content as well. Which is throwing error while doing the dotnet build process as the build agent could not resolve the common.csproj/ I think my dockerfile definition is wrong. Can someone please assist in getting this work [i tried lot of option here with dockerfile]>?
#FROM microsoft/aspnetcore-build:2.0 AS build-env
FROM microsoft/aspnetcore
# copy csproj and restore as distinct layers
#COPY *.csproj ./
#RUN dotnet restore
# copy everything else and build
#COPY . ./ ./ ./
#RUN dotnet restore
#RUN dotnet publish -c Release -o out
# build runtime image
#FROM microsoft/aspnetcore:2.0
#WORKDIR /app
#COPY --from=build-env /app/out .
#EXPOSE 5000
COPY ./out /out
WORKDIR /out
#WORKDIR /publish
ENTRYPOINT ["dotnet", "Rando.MortgageCenter.WebAPI.dll"]
I am trying to build this docker image using the Docker cloud environment [cloud.docker.com/swarm]
Error Log from docker cloud build
Building in Docker Cloud's infrastructure...
Cloning into '.'...
Warning: Permanently added the RSA host key for IP address '104.192.143.1' to the list of known hosts.
Reset branch 'master'
Your branch is up-to-date with 'origin/master'.
KernelVersion: 4.4.0-93-generic
Arch: amd64
BuildTime: 2017-08-17T22:50:04.828747906+00:00
ApiVersion: 1.30
Version: 17.06.1-ce
MinAPIVersion: 1.12
GitCommit: 874a737
Os: linux
GoVersion: go1.8.3
Starting build of index.docker.io/systreeau/mortgagecenter:latest...
Step 1/4 : FROM microsoft/aspnetcore
---> f79840764591
Step 2/4 : COPY ./out /out
COPY failed: stat /var/lib/docker/tmp/docker-builder177013174/out: no such file or directory
ERROR: Build failed: COPY failed: stat /var/lib/docker/tmp/docker-builder177013174/out: no such file or directory
ERROR: Build failed with exit code 2
So what I found was you have to place your DOCKERFILE in the src folder which is the root of the solution. so that docker context becomes this path.
Now you have a Dockerfile as below
FROM microsoft/aspnetcore-build:2.0 as build
WORKDIR /app
COPY . .
#COPY Solution.sln .
# RUN ls -l
RUN dotnet restore ./Solution.sln
COPY . .
RUN dotnet publish ./Ser/Host/Api/api.csproj --output /out/ --configuration Release
# runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build /out .
ENTRYPOINT ["api.dll"]
Hope this help someone having the issue what i am facing

Resources