简体   繁体   中英

How to add service to docker container?

Is it possible to install an additional feature to a specific docker layer? In practice, I want to add the ability to handle ssh from a terminal for scripts launched by asp.net core applications. In a normal nondocker environment, this works fine.

MyDockerFile:

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["LinuxConsoleApp/LinuxConsoleApp.csproj", "LinuxConsoleApp/"]
COPY . ./
WORKDIR "/src/LinuxConsoleApp"
RUN dotnet build "LinuxConsoleApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "LinuxConsoleApp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app

##instaling additional service, in my case ssh-client
RUN apt-get update && apt-get install -y openssh-client
##changing permissions
RUN mkdir -p /root/.ssh && \
  chmod 0700 /root/.ssh 

###unsafe coping private key to container - i know 
COPY id_rsa /root/.ssh/id_rsa

WORKDIR /app/

###here ssh can connect from docker to localhost during build time
RUN ssh -o StrictHostKeyChecking=no troom@192.168.0.200

COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "LinuxConsoleApp.dll"]
COPY LinuxConsoleApp/data /app/scripts

When i try to run script from asp.net core container i get message:

/app/scripts/Import.sh: 1: /app/scripts/Import.sh: ssh: not found

Where Import.sh is my script containg:

ssh -o StrictHostKeyChecking=no troom@192.168.0.200

As you can see, it installs the ssh client, but I don't understand why it is not available from the asp.net core application

The solution to the problem was not obvious, at least to me.

Long story: UTF-8 BOM https://www.w3.org/International/questions/qa-byte-order-mark

Short story: My ssh file was in a format that had invisible characters. So it was enough to create a new file, copy the contents there (in my case ssh -o StrictHostKeyChecking=no troom@192.168.0.200) and everything started to work properly.

To sum up, the above dockerfile is correct and can be safely used. The problem was the script file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM