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.
Related
I Have following structure in my project
I'm trying to run
following in cmd
docker build -t counter-bal-image '.\docker.'
My docker file has following line
COPY cicd/scripts/* /App/scripts/
When i run i get COPY failed: no source files were specified
How to copy relative folders?
If you build in ./docker then that's where everything must live. Instead specify the path to the Dockerfile but build in the current directory:
docker build -f docker/Dockerfile -t counter-bal-image .
Since you're building in . then ./cicd becomes accessible.
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
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/api/values",
"httpPort": 52706,
"useSSL": true,
"sslPort": 44344
}
This gives the output when it is run through visual studio
But on build, it throws error
DockerFile:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 83
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["testdocker/testdocker.csproj", "testdocker/"]
RUN dotnet restore "testdocker/testdocker.csproj"
COPY . .
WORKDIR "/src/testdocker"
RUN dotnet build "testdocker.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "testdocker.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENV ASPNETCORE_URLS http://+:83
ENTRYPOINT ["dotnet", "testdocker.dll"]
to build the docker image
docker build -t testdock .
but it gives
COPY failed: stat /var/lib/docker/tmp/docker-builder666564019/testdocker/testdocker.csproj,: no such file or directory
Please help to get the dockerfile rewritten so as to complete this build and run the app
If you look at the Container Tools output in Visual Studio, you'll see a line like:
docker build -f "C:\Users\foo\source\MySolution\TestDocker\Dockerfile" -t testdocker:dev --target base --label "com.microsoft.created-by=visual-studio" "C:\Users\foo\source\MySolution"
When building an image for a Linux container on Windows, Docker lifts the contents of the active directory into the MobyLinux VM and all the copy commands and such are run against that path in the MobyLinux VM, not your local filesystem. Because projects very often need access to other projects in the same solution in order to build, the Dockerfiles created by Visual Studio are relative to your solution directory, such that the entire solution directory is lifted in MobyLinux.
Very likely, what you've done is navigate directly into your project directory and run the Dockerfile from there, without passing a directory to use as the "root". As such, Docker simply lifts the current, i.e your project, directory and the resulting paths in the MobyLinux VM no longer match what's in the Dockerfile.
Long and short, if you want to manually do a build of the image, then you need to ensure that the active directory that's lifted is your solution directory, not your project directory. You can achieve that simply by passing that last string of the command above to your own command, which will make it relative to your solution.
The fix for me appeared to be:
docker build -f PROJECT_DIR\Dockerfile .
When ran from the solution directory.
Andrew's sample works for me.
My command:
docker build -f helloworld\dockerfile -t sr-app .
Conceptually COPY takes path as specified in COPY command, just an example
COPY server/nginx/nginx.conf /etc/nginx/conf.d
so its necessary to run docker file from parents of server say path is like this.
xyz-app/server/nginx/nginx.conf
then you need to go to xyz-app directory and then run (And to specify docker file path use -f)
docker build -f server/nginx/Dockerfile -t app.v1.0 .
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
I have made images ubuntu 14:04 on dockerfile
I am running the syntax
$ sudo docker build -t mypostgres .
but I am still confused as to build the dockerfile
how to build it?
sudo docker build -t mypostgres . means:
process the file named 'Dockerfile' (default name)
located in the current folder (that is the final .)
and build as a result the image named mypostgres
So if you have a Dockerfile starting with FROM postgres, you can execute your command and have your own postgres image in no time.
Dockerfile is not as complex as it looks. here's a good start article that could help you to build your first docker file easily - http://rominirani.com/2015/08/02/docker-tutorial-series-writing-a-dockerfile/
You may want to read the doc of Dockerfile best practice by Docker, better than any article IMHO.
You can build a docker file direct from git repository or from a director.
to build a docker file first create a docker file inside your project and name it just Docker without any extension. Now inside that file write necessary command for building an image. For example
FROM node:alpine
WORKDIR /app
COPY package.json ./
RUN npm install
COPY ./ ./
CMD ["npm", "start"]
->Build from git:
sudo docker build https://github.com/lordash/mswpw.git#fecomments:comments
in here:
fecomments is branch name and comments is the folder name.
->building from git with tag and version:
sudo docker build https://github.com/lordash/mswpw.git#fecomments:comments -t lordash/comments:v1.0
->Now if you want to build from a directory: first go to comments directory the run command sudo docker build .
->if you want to add tag you can use -t or -tag flag to do that:
sudo docker build -t lordash . or sudo docker build -t lordash/comments .
-> Now you can version your image with the help of tag:
sudo docker build -t lordash/comments:v1.0 .
->you can also apply multiple tag to an image:
sudo docker build -t lordash/comments:latest -t lordash/comments:v1.0 .