KostyaSha / yet-another-docker-plugin

Jenkins Yet Another Docker Plugin
https://plugins.jenkins.io/yet-another-docker-plugin
MIT License
83 stars 48 forks source link

Unable to connect a Windows container as an ephemeral slave/agent #233

Open dkumar95120 opened 6 years ago

dkumar95120 commented 6 years ago

I was able to launch a Linux container as an ephemeral Jenkins agent using SSH. However, I am unable to do the same with a Windows container. I can verify that the SSHD service is running in the Windows container by running it interactively and performing an ssh user>@<container_ip_address.

Only the following log information in the Jenkins Master:

May 28, 2018 2:06:54 PM INFO com.github.kostyasha.yad.DockerCloud provision Will provision 'jenkins/windows-ssh-slave:1.0', for label: 'docker-windows-slave2', in cloud: 'YADP-windows-cloud' May 28, 2018 2:06:54 PM INFO com.github.kostyasha.yad.DockerCloud addProvisionedSlave Provisioning 'jenkins/windows-ssh-slave:1.0' number '0' on 'YADP-windows-cloud'; Total containers: '0' May 28, 2018 2:07:53 PM INFO hudson.slaves.RetentionStrategy$Demand check

On the dockerhost I see following output with docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 44d92a7d90f8 jenkins/windows-ssh-slave:1.0 "cmd.exe cmd" About a minute ago Exited (0) About a minute ago vig

Dockerfile for the image ----------------------------------- FROM microsoft/windowsservercore

SHELL ["powershell.exe", "-ExecutionPolicy", "Bypass", "-Command"]

RUN iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) RUN choco install -y git --params='/NoShellIntegration' RUN choco install -y nuget.commandline

ENV JAVA_HOME c:/jre-8u171 ENV JENKINS_HOME c:/jenkins ENV JAVA_EXE jre-8u171-windows-x64.exe

RUN (new-object System.Net.WebClient).Downloadfile('http://javadl.oracle.com/webapps/download/AutoDL?BundleId=233172_512cd62ec5174c3487ac17c61aaa89e8', 'C:\$env:JAVA_EXE')

COPY jre-8u171-windows-x64.exe C:/ RUN start-process -filepath C:/jre-8u171-windows-x64.exe -passthru -wait -argumentlist "/s,INSTALLDIR=$env:JAVA_HOME,/L,install64.log" RUN del C:/jre-8u171-windows-x64.exe

RUN $env:PATH = $env:JAVA_HOME + '\bin;' + $env:PATH; \

RUN mkdir ~/.ssh COPY authorized_keys C:/ RUN copy c:/authorized_keys ~/.ssh/

Download openssh for windows

COPY OpenSSH-Win64.zip C:/

COPY agent.jar C:/ RUN Expand-Archive C:/OpenSSH-Win64.zip -DestinationPath c:/

RUN C:/OpenSSH-Win64/install-sshd.ps1

RUN del C:/OpenSSH-Win64.zip

RUN mkdir $env:JENKINS_HOME WORKDIR $JENKINS_HOME

RUN mkdir C:/data VOLUME C:/data

EXPOSE 22

RUN net start sshd

RUN Set-Service sshd -StartupType Automatic

RUN start-service sshd ENTRYPOINT ["cmd.exe"]

dkumar95120 commented 6 years ago

Saw following exception just now in the logs: Can't connect to ssh for 10.20.5.73:48877 java.net.ConnectException: Connection refused (Connection refused) at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at com.trilead.ssh2.transport.TransportManager.establishConnection(TransportManager.java:367) at com.trilead.ssh2.transport.TransportManager.initialize(TransportManager.java:480) at com.trilead.ssh2.Connection.connect(Connection.java:774) Caused: java.io.IOException: There was a problem while connecting to 10.20.5.73:48877 at com.trilead.ssh2.Connection.connect(Connection.java:834) at com.github.kostyasha.yad.utils.HostAndPortChecker.bySshWithEveryRetryWaitFor(HostAndPortChecker.java:89) at com.github.kostyasha.yad.launcher.DockerComputerSSHLauncher.waitUp(DockerComputerSSHLauncher.java:103) at com.github.kostyasha.yad.DockerCloud.provisionWithWait(DockerCloud.java:259) at com.github.kostyasha.yad.DockerCloud.lambda$provision$0(DockerCloud.java:135) at jenkins.util.ContextResettingExecutorService$2.call(ContextResettingExecutorService.java:46) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)

KostyaSha commented 6 years ago

RUN start-service sshd ENTRYPOINT ["cmd.exe"]

You should ensure that after contain start it will be alive and cmd.exe wouldn't exit, because container will stop then and connection will fail.