Docker – ASP.CORE 2.2 application and SSH

I’m trying to configure my docker container so it’s possible to ssh into it (the container will be run on Azure). I managed to create an image that enables user to ssh into a container created from that image, the Dockerfile looks like that (it’s not mine, I found it on the internet):

FROM AS build
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
COPY sshd_config /etc/ssh
RUN echo 'root:Docker' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

RUN sed '[email protected]*requireds*[email protected] optional [email protected]' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

CMD ["/usr/sbin/sshd", "-D"]

I’m using because it’s what I need later on to run the application.

Having the Dockerfile above, I run docker build . -t ssh. I can confirm that it’s possible to ssh into a container created from ssh image with following instructions:

docker run -d -p --name ssh ssh
ssh [email protected] -p 2222

My application’s Dockerfile:

FROM AS build
COPY ["Application.WebAPI/Application.WebAPI.csproj", "Application.WebAPI/"]
COPY ["Processing.Dependency/Processing.Dependency.csproj", "Processing.Dependency/"]
COPY ["Processing.QueryHandling/Processing.QueryHandling.csproj", "Processing.QueryHandling/"]
COPY ["Model.ViewModels/Model.ViewModels.csproj", "Model.ViewModels/"]
COPY ["Core.Infrastructure/Core.Infrastructure.csproj", "Core.Infrastructure/"]
COPY ["Model.Values/Model.Values.csproj", "Model.Values/"]
COPY ["Sql.Business/Sql.Business.csproj", "Sql.Business/"]
COPY ["Model.Events/Model.Events.csproj", "Model.Events/"]
COPY ["Model.Messages/Model.Messages.csproj", "Model.Messages/"]
COPY ["Model.Commands/Model.Commands.csproj", "Model.Commands/"]
COPY ["Sql.Common/Sql.Common.csproj", "Sql.Common/"]
COPY ["Model.Business/Model.Business.csproj", "Model.Business/"]
COPY ["Processing.MessageBus/Processing.MessageBus.csproj", "Processing.MessageBus/"]
COPY [".Processing.CommandHandling/Processing.CommandHandling.csproj", "Processing.CommandHandling/"]
COPY ["Processing.EventHandling/Processing.EventHandling.csproj", "Processing.EventHandling/"]
COPY ["Sql.System/Sql.System.csproj", "Sql.System/"]
COPY ["Application.Common/Application.Common.csproj", "Application.Common/"]
RUN dotnet restore "Application.WebAPI/Application.WebAPI.csproj"
COPY . .
WORKDIR "/src/Application.WebAPI"
RUN dotnet build "Application.WebAPI.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "Application.WebAPI.csproj" -c Release -o /app

FROM ssh AS final
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Application.WebApi.dll"]

As you can see I’m using ssh image as a base image in the final stage. Even though I was able to sshe into the container created from ssh image, I’m unable to ssh into a container created from the latter Dockerfile. Here is the docker-compose.yml I’m using in order to ease starting the container:

version: '3.7'

    image: application.webapi
    container_name: webapi
      - ""
      - ""
      context: .
      dockerfile: Application.WebAPI/Dockerfile

When I run docker exec -it webapi bashand execute service ssh status, I’m getting [FAIL] sshd is not running ... failed! – but when I do service ssh start and try to ssh into that container, it works. Unfortunately this approach is not acceptable, ssh daemon should launch itself on startup.

I tried using cron and other stuff available on debian but it’s a slim version and systemd is not available there – I’m also not fond of installing hundreds of things on slim versions.

Do you have any ideas what could be wrong here?

Source: StackOverflow