Can not run docker container from compose - docker

I can not run a Net Core application in docker-compose due to this error,while i can start the container from dockerfile.
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
My dockerfile uses the .dll directly (i am directly using the publish directory), so no build ,restore is required.
FROM microsoft/dotnet:aspnetcore
WORKDIR /app
COPY ./publish .
ENTRYPOINT ["dotnet", "Server.dll"]
EXPOSE 8202
I run:
docker build -t mp .
docker run --name mp0 -d myimg and it works.
However when i am running it from compose :
mp:
image: mp
container_name: mp0
build: ./mpbuild
ports:
- 8302:8202
I have tried using :aspnetcore,aspnetcore-build,aspnetcore-runtime to no avail .I keep getting asked about the SDK.
P.S: The app is version aspnetcore 2.1.

try changing microsoft/dotnet:aspnetcore to microsoft/dotnet:2.2-sdk

Related

Failed to compute cache key: ".csproj" not found

I am new to Docker. I created a Web API using ASP.Net Core using Visual Studio 2019 as well as in VS Code. It works fine. Then I added docker support and added Dockerfile with default values.
When I try to build the docker image, it fails in Visual Studio 2019 as well as in VS Code.
However, If I try to run the Docker image using the Visual Studio 2019 provided option (where I can select docker as run), then the image gets created.
But when I run the build command in Visual Studio 2019 or VS Code i.e.
docker build -f ./Dockerfile --force-rm -t mytestapp:dev ..
it throws following error<br>
=> ERROR [build 3/7] COPY [myTestApp.csproj, ./]
Content of my docker file is given below
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["myTestApp.csproj", "./"]
RUN dotnet restore "myTestApp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "myTestApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "myTestApp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "myTestApp.dll"]
The project structure picture is also attached:
A simple docker build command cannot work with the default Dockerfiles created by Visual Studio because the paths are specified relative to the root of the solution, and not the root of the project.
You can inspect the build output from VS to determine how it builds the image (simplified version):
docker build
-f "PROJECT_PATH\Dockerfile"
-t IMAGE_NAME:dev
"SOLUTION_PATH"
As you can see, it builds using the Dockerfile in the project folder (-f), but from the solution folder.
I guess they did it because it has the advantage of keeping each Dockerfile in its own project folder, while letting you reference resources outside that folder using more consistent solution-based paths. Apart from that, it's pretty annoying.
You can move the Dockefile to the solution folder and leave it unchanged, but then the Docker features in VS will stop working as expected. Or you can adopt the VS convention and adapt your scripts accordingly.
Try running the command from the parent folder, you can specify the path to the Dockerfile using the -f flag.
cd ..
docker build -t imagename:tag -f ProjectDir/Dockerfile .
Docker copy's the .csproj and other files from the current location on the host machine, so if you say:
COPY ["myTestApp.csproj", "./"]
Make sure you are in the right directory on the host machine. The Dockerfile created by Docker Support is not always ideal for building images if you use for example other project references but can be a good base.
Run this from your Solution root:
docker build . -f [ProjectDir]\Dockerfile
Answer from *#axtc*k worked for me. The only change required to make it work was to remove the slash:
cd ..
docker build -t imagename:tag -f ProjectDir/Dockerfile .
Use docker-compose to easily create and tear down your setup.
Step 1: Save code below as docker-compose.yml one directory higher than your Dockerfile (same path as your project's .sln file):
version: '3'
services:
web:
build:
context: .
dockerfile: [PROJECTNAME]\Dockerfile
ports:
- "5000:80"
networks:
- aspcore-network
sql-server:
image: mcr.microsoft.com/mssql/server
networks:
- aspcore-network
networks:
aspcore-network:
driver: bridge
Step 2: Add additional services (MYSQL/REDIS/ETC)
Step 3: Open terminal to docker-compose.yml location
Step 4: Run docker-compose build then docker-compose up -d
Step 5: When done run docker-compose down
Remove the .(dot) you included at WORKDIR "/src/."
I solved this issue by providing the absolute path to the docker command.
Instead, go to the parent directory, with the .sln file and use the docker -f option to specify the Dockerfile to use in the subfolder:
cd \CoreDockerAPI
docker build -f CoreDockerAPI\Dockerfile --force-rm -t myfirstimage .
docker run -it myfirstimage
Here are the steps I used to solve this problem :
I checked Enable Docker while creating my .NET 5 Web API Project.
For Docker OS, I chose Linux.
Then I opened a terminal, navigated to the directory where my project is and typed the following command : docker build -f Movie.WebAPI\Dockerfile --force-rm -t movie-api:v1 .
Which gave the following results :
for path
for result
continuation of the result
As the last step, I ran this command : docker run -it --rm -p 8080:80 movie-api:v1
Which created the container image.
Now movie-api appears when I type docker images.

.NET Core 3.0 Web API won't start in Docker container

I'm fresh to Docker, trying to get a proof of concept container running on my machine. I generated a .NET Core 3.0 Web API project using the command dotnet new webapi. I've added a controller endpoint that simply returns a string "Hello World."
using System;
using Microsoft.AspNetCore.Mvc;
namespace DockerHelloWorld.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
[HttpGet]
public string Get()
{
return "Hello World!";
}
}
}
I am able to successfully build and run this project using dotnet build and dotnet run respectively.
I am now trying to get this to run in a Docker container. Here is my Dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish --framework netcoreapp3 --configuration Release --output out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DockerHellWorld.dll"]
This is copied and slightly modified from here.
I also have the following .dockerignore, just in case it is relevant:
bin\
obj\
*.md
*.png
I run the following commands:
docker build -t dockerhelloworldimage .
docker create -p 3000:80 --name dockerhelloworldcontainer dockerhelloworldimage
docker start dockerhelloworldcontainer
It seems like each step is being ran based on the output of docker build. A hash is returned after docker create and "dockerhelloworldcontainer" is returned after docker run.
The container stops immediately. I see this by using the docker container ls -a command.
docker logs dockerhelloworldcontainer has the following output: "It was not possible to find any installed .NET Core SDKs. Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from: https://aka.ms/dotnet-download" but I don't really know how to make sense of that.
It's worth mentioning, I got something very similar to this working with a .NET Core 2.2 app.
Any ideas? What could I be missing?
The issue as #Zied mentioned in the comment, is a typo in the Assembly name you want to call from the entrypoint command.
If you fix that on the docker file:
# All other content remains the same
ENTRYPOINT ["dotnet", "DockerHelloWorld.dll"]
You'll get your container successfully started.
Listing running containers: docker ps shows:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7dcb5c122445 dockerhelloworldimage "dotnet DockerHelloW…" 5 minutes ago Up 4 minutes 0.0.0.0:3000->80/tcp dockerhelloworldcontainer
Now, this can look like a misleading error message, but if you try to actually run a wrong dotnet executable from the CLI (not from docker):
> dotnet MyNonExistingExeName.dll
You'll get the below message:
Could not execute because the specified command or file was not found.
Possible reasons for this include:
* You misspelled a built-in dotnet command.
* You intended to execute a .NET Core program, but dotnet-.\bin\Debug\netcoreapp3.0\DockerHellWorld.dll does not exist.
* You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
I believe that the second and third bullets apply to this case. What that means is that if you have a dotnet executable (or a global/local tool) you can run them with the dotnet command so the issue is that your command is being interpreted as a non-existent tool or command and hence the error.
Hope this helps!
Run the below command to deploy sample Dotnet web API code on docker
docker-compose build
docker-compose up
Dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
COPY ./SampleWebApi3.0.csproj /SampleWebApi3.0/
RUN dotnet restore ./SampleWebApi3.0/SampleWebApi3.0.csproj
COPY . ./SampleWebApi3.0/
WORKDIR /SampleWebApi3.0/
RUN dotnet build "SampleWebApi3.0.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "SampleWebApi3.0.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SampleWebApi3.0.dll"]
docker-compose.yml
version: '3.5'
services:
sample-web-api:
build: SampleWebApi3.0/
restart: always
ports:
- "8085:80"
Sample web API code 3.0 along with Dockerfile on Github

Generate test ASP.NET Core test Result inside a Docker Volume with Docker-Compose

I am developing on a Windows 10 Pro machine, trying to generate the Microsoft trx test result file from within a docker container running in Linux mode.
This is my docker-compose yml file:
version: '3.4'
services:
redis:
image: redis
Project.Test:
build:
context: .
dockerfile: ./Project.Test/Dockerfile
This is the Dockerfile executed with the up command
FROM microsoft/dotnet:2.0-sdk AS build
WORKDIR /src
COPY Project.Test/*.csproj Project.Test/
RUN dotnet restore -nowarn:msb3202 Project.Test/*.csproj
COPY . ./
WORKDIR /src/Project.Test
RUN dotnet build Project.Test.csproj -c Release -o /app
RUN dotnet test --logger:trx;LogFileName=/artifacts/Project.Test.trx
I need to copy the Project.Test.trx on a folder on my Windows 10 machine but I am not able to correctly mount the volume.
What is the right command to insert inside the docker-compose?
You probably need to enable Shared Drives in the Docker for Windows settings (accessed via the system tray).
There is a section describing the process in detail available in the Docker docs https://docs.docker.com/docker-for-windows/#shared-drives - which may require firewall rules and can be done either permanently (for all containers) or only when required when mounting a container.
There is also a useful troubleshooting section here, which will likely come in handy as there are a few related permission/firewall gotchas: https://docs.docker.com/docker-for-windows/troubleshoot/#volumes

How to access WebAPi after deploying on docker

I have my Dockerfile:
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /build
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o output
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build /build/output .
ENTRYPOINT ["dotnet","TestDockerApi.dll"]
I am creating an image using :
docker build -t testdocker/api .
and then running a container from image using :
docker run testdocker/api
I can see following message on my console:
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
I am trying to access using http://localhost/app/TestDockerApi/Values , but it does not work.
Do I need to use docker image IP to access that .
I can see few tutorials suggesting to do this in Entrypoint :
ENTRYPOINT ["dotnet","TestDockerApi.dll","--server.urls","http://0.0.0.0:5000"]
And then while running the container, mapping the port:
docker run -p 80:5000 testdocker/api
Is there any way I could access the API with out using portforwarding? I am just trying to get the basics right , why and what should I do.
The Dockerfile does not manage network configuration outside of the container at all. If you want docker to listen on your host port of 80, you need to bind it when you run your container.
docker run -80:80 testdocker/api
For more description about mapping and exposing ports, you can read here:
- https://www.ctl.io/developers/blog/post/docker-networking-rules/
Alternatively you can create your own service composition where you specify these details and specify this in a docker-compose.yml file
api:
image: testdocker/api
ports:
- "80:80"
And then you can simply run with
docker-compose up
More information is at:
https://docs.docker.com/compose/reference/overview/#command-options-overview-and-help

Asp.net 5 - Docker

I'm about to lose my head ;-)
I've tried all tutorials I can find on running ASP.NET 5 using Docker in Linux.
It won't work....
I'm using Ubuntu 14.04 no matter what I do I always get :
mountpoint for devices not ready.
For some Dockerfile setup I'll get this message in build for other configuration I'll get it on run.
There is no point to shave my Dockerfile because I had tens of them.
I'm using Windows 10 Hyper-V for the virualization
Dockerfile
FROM microsoft/aspnet:1.0.0-rc1-final
COPY . /app
WORKDIR /app
RUN ["dnu", "restore"]
EXPOSE 5000
ENTRYPOINT ["dnx","-p", "project.json", "kestrel"]
Build - docker build -t dev3 .
Run - docker run -t -d -p 87:5000 dev3
Docker Install - apt-get install docker.io
Im running ASP.NET 5 in docker on ubuntu 15.04, I noticed in my Dockerfile im using this image:
FROM microsoft/aspnet:1.0.0-rc1-final-coreclr
You could try using that image, but it sounds like its your Docker install.

Resources