Mount docker volume to host machine path - docker

I need to access test result files in the host from the container. I know that I need to create a volume which maps between host and container, like below, but I get nothing written to the host.
docker run --rm -it -v <host_directory_path>:<container_path> imagename
Dockerfile:
FROM microsoft/dotnet:2.1-sdk AS builder
WORKDIR /app
COPY ./src/MyApplication.Program/MyApplication.Program.csproj ./src/MyApplication.Program/MyApplication.Program.csproj
COPY nuget.config ./
WORKDIR ./src/MyApplication.Program/
RUN dotnet restore
WORKDIR /app
COPY ./src ./src
WORKDIR ./src/MyApplication.Program/
RUN dotnet build MyApplication.Program.csproj -c Release
FROM builder as tester
WORKDIR /app
COPY ./test/MyApplication.UnitTests/MyApplication.UnitTests.csproj ./test/MyApplication.UnitTests/MyApplication.UnitTests.csproj
WORKDIR ./test/MyApplication.UnitTests/
RUN dotnet restore
WORKDIR /app
COPY ./test ./test
WORKDIR ./test/MyApplication.UnitTests/
RUN dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura
ENTRYPOINT ["dotnet", "reportgenerator", "-reports:coverage.cobertura.xml", "-targetdir:codecoveragereports", "-reportTypes:htmlInline"]
The command at the entry point is working correctly. It is writing the output to the MyApplication.UnitTests/codecoveragereports directory, but not to the host directory.
My docker run looks as follows:
docker run --rm -it -v /codecoveragereports:/app/test/MyApplication.UnitTests/codecoveragereports routethink.tests:latest
What could I be doing wrong?

Looks like a permission issue.
-v /codecoveragereports:/app/***/codecoveragereports is mounting a directory under the root / which is dangerous and you may not have the permission.
It's better to mount locally, like -v $PWD/codecoveragereports:/app/***/codecoveragereports, where $PWD is an environment variable equal to the current working directory.

Related

dockerfile COPY does not copy all the files

I do
git clone https://github.com/openzipkin/zipkin.git
cd zipkin
The create a Dockerfile as below
FROM openjdk
RUN mkdir app
WORKDIR /app
COPY ./ .
ENTRYPOINT ["sleep", "1000000"]
then
docker build -t abc .
docker run abc
I then run docker exec -it CONTAINER_ID bash
pwd returns /app which is expected
but I ls and see that the files are not copied
only the directories and the xml file is copied into the /app directory
What is the reason? how to fix it?
Also I tried
FROM openjdk
RUN mkdir app
WORKDIR /app
COPY . /app
ENTRYPOINT ["sleep", "1000000"]
That repository contains a .dockerignore file which excludes everything except a set of things it selects.
That repository's docker directory also contains several build scripts for official images and you may find it easier to start your custom image FROM openzipkin/zipkin rather than trying to reinvent it.

Module Not found after attaching volume in docker

This is my dockerfile
FROM node:15
# sets the folder structure to /app directory
WORKDIR /app
# copy package.json to /app folder
COPY package.json .
RUN npm install
# Copy all files from current directory to current directory in docker(app)
COPY . ./
EXPOSE 3000
CMD ["node","index.js"]
I am using this command in my powershell to run the image in a container
docker run -v ${pwd}:/app -p 3000:3000 -d --name node-app node-app-image
${pwd}
returns the current directory.
But as soon as I hit enter, somehow node_modules isn't being installed in the container and I get "express not found" error in the log.
[![Docker log][1]][1]
I can't verify if node_modules isn't being installed because I can't get the container up to run the exec --it command.
I was following a freecodecamp tutorial and it seems to work in his pc and I've tried this command in command prompt too by replacing ${pwd} by %cd%.
This used to work fine before I added the volume flag in the command.
[1]: https://i.stack.imgur.com/4Fifu.png
Your problem was you build your image somewhere and then try to map another folder to it.
|_MyFolder/
|_ all-required-files
|_ all-required-folders
|_ Dockerfile
docker build -t node-app-image .
docker run -p 3000:3000 -d --name node-app node-app-image
Simplified Dockerfile
FROM node:15
# sets the folder structure to /app directory
WORKDIR /app
# Copy all files from current directory to current directory in docker(app)
COPY . ./
RUN npm install
EXPOSE 3000
CMD ["node","index.js"]

How can we create Dockerfile for Angular with dotnet core api project?

I have created the Dockerfile using visual studio for my dotnet api project but how can we integrate the client app in the docker file. I have created the docker file which looks like:
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["Locomote-e-Client.Service/Locomote-e-Client.Service.csproj", "Locomote-e-Client.Service/"]
COPY ["Locomote-e-Client.Core/Locomote-e-Client.Core.csproj", "Locomote-e-Client.Core/"]
COPY ["Locomote-e-Client.Common/Locomote-e-Client.Common.csproj", "Locomote-e-Client.Common/"]
COPY ["Locomote-e-Client.Data/Locomote-e-Client.Data.csproj", "Locomote-e-Client.Data/"]
RUN dotnet restore "Locomote-e-Client.Service/Locomote-e-Client.Service.csproj"
COPY . .
WORKDIR "/src/Locomote-e-Client.Service"
RUN dotnet build "Locomote-e-Client.Service.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Locomote-e-Client.Service.csproj" -c Release -o /app/publish
FROM node:10.21 as nodebuilder
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH
COPY Locomote-e-ClientApp/package.json /usr/src/app/package.json
RUN npm install
COPY Locomote-e-ClientApp/. /usr/src/app
RUN npm run build
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
RUN mkdir -p /app/ClientApp/dist
COPY --from=nodebuilder /usr/src/app/dist/. /app/ClientApp/dist/
ENTRYPOINT ["dotnet", "Locomote-e-Client.Service.dll"]
Whenever I have created the image by using this docker file then it will create the image successfully. But when I tried to run that image using container then it is not running in container.
Command to build the image
docker build -t ashishrajput194/locomate:v1 .
Command to run as container
docker run -p 80:80 -d ashishrajput194/locomate:v1
It is not showing any error but the container exits automatically. I am not able to run the application.

read application logs from logs folder after hosted using docker in linux

We have hosted .net core api in linux server using docker. so issue is reading application logs from Logs folder which we are not getting in container. our docker code is :
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 50147
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["Services/Services.csproj", "Services/"]
COPY ["Portal.Common/Portal.Common.csproj", "Portal.Common/"]
COPY ["Portal.Domains/Portal.Domains.csproj", "Portal.Domains/"]
COPY ["Portal.Business/Portal.Business.csproj", "Portal.Business/"]
COPY ["Portal.DataAccess/Portal.DataAccess.csproj", "Portal.DataAccess/"]
RUN dotnet restore "PortalServices/PortalServices.csproj"
COPY . .
WORKDIR "/src/PortalServices"
RUN dotnet build "PortalServices.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "PortalServices.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "PortalServices.dll"]
so inside app path only dll files are there. So how can we read logs from folder???
We implemented serilog to solution and saving to solutionpath/Logs/log.txt
In local it is storing in this path.
Please help!!
Change folder permission to 777 where you want to store logs.
chmod 777 .
docker run -d -t -i -p 80:80 -v /usr/Logs:/app/Logs --name
testContainer testImage

Can't start ASP.NET Core web API Docker on a specified port

Here is my Dockerfile:
FROM microsoft/aspnetcore-build:1.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 -c Release -o out
# build runtime image
FROM microsoft/aspnetcore:1.0
WORKDIR /app
COPY --from=build-env /app/out .
EXPOSE 58912
ENTRYPOINT ["dotnet", "SgaApi.dll"]
I used this command to build: docker build -f Dockerfile -t sga .
Run: docker run -e "ASPNETCORE_URLS=http://+:58912" -it --rm sga
Application starts successfully
I can't access it from the browser. When I run the application using "dotnet run" it works fine.
You have exposed the port, but you haven't published it. You can either use -P to publish all exposed ports or -p and then specify port mapping. For example:
docker run -p 58912:58912 -e "ASPNETCORE_URLS=http://+:58912" -it --rm sga

Resources