1

I want to be able to execute a command from a jenkins server inside a build container. I imagine this would have to be done using ssh. Here is my attempt sofar:

Dockerfile for the build server:

FROM ubuntu:18.04

RUN apt-get update 
RUN apt-get install git -y
RUN apt-get install wget -y
RUN apt-get install socat -y
RUN apt-get install unzip -y
RUN apt-get install chrpath -y
RUN apt-get install build-essential -y
RUN apt-get install texinfo -y
RUN apt-get install xterm -y
RUN apt-get install python3 -y
RUN apt-get install python -y
RUN apt-get install locales -y
RUN apt-get install cpio -y
RUN apt-get install diffstat -y
RUN apt-get install gawk -y

RUN apt-get install -y openssh-server
RUN mkdir /var/run/sshd

RUN echo 'root:root' |chpasswd

RUN sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config

RUN mkdir /root/.ssh
EXPOSE 22

RUN echo "LC_ALL=en_US.UTF-8" >> /etc/environment
RUN echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
RUN echo "LANG=en_US.UTF-8" > /etc/locale.conf
RUN locale-gen en_US.UTF-8
ENV LC_ALL=en_US.UTF-8
ENV LANG=en_US.UTF-8

RUN useradd -ms /bin/bash builder
USER builder
WORKDIR /home/builder/

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

docker-compose file:

version: '3'
services:
    yocto-server:
        build: .
        container_name: yocto-server
        tty: true
        ports:
            - 22:22
        networks:
            - build-network

    jenkins-master:
        image: jenkins/jenkins
        container_name: jenkins-master
        privileged: true
        working_dir: /home/jenkins
        depends_on:
            - yocto-server
        ports:
            - 8080:8080
        networks:
            - build-network
        links:
            - yocto-server
networks:
    build-network:
        driver: bridge

Now I try to run a bash inside of the jenkins container and ssh into the yocto container:

sudo docker-compose run jenkins-master /bin/bash
Starting yocto-server ... done
jenkins@b79689ec8403:/home/jenkins$ ping yocto-server
PING yocto-server (172.18.0.2) 56(84) bytes of data.
64 bytes from yocto-server.yoctodocker_build-network (172.18.0.2): icmp_seq=1 ttl=64 time=0.076 ms
64 bytes from yocto-server.yoctodocker_build-network (172.18.0.2): icmp_seq=2 ttl=64 time=0.044 ms
^C
--- yocto-server ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1026ms
rtt min/avg/max/mdev = 0.044/0.060/0.076/0.016 ms
jenkins@b79689ec8403:/home/jenkins$ ssh root@yocto-server
ssh: connect to host yocto-server port 22: Connection refused

I suspect something is wrong with the port exposure. The error reads as if the ports not open at all, despite it being exposed in the docker file and mapped in the compose file.

8
  • I think Docker doesn't run daemonized services. So your first thing to inspect is if sshd is actually running, and if it's not, make sure it runs by running. Then everything should be fine. Commented May 27, 2020 at 8:02
  • Tip: use one RUN command to install libraries Commented May 27, 2020 at 8:32
  • @funnydman I know, but its a bit more readable imo. Commented May 27, 2020 at 9:13
  • @ChristopheDeTroyer you are right the service did not run - why? I started it last thing in the Dockerfile? Commented May 27, 2020 at 9:19
  • It's not only readability. Commented May 27, 2020 at 9:50

1 Answer 1

0

two things

  1. It could be because the yocto-server container doesn't have a blocking command if the command exits, the container will stop, this happens for run command you can confirm for up
  2. the command could be failing

you need to setup the sshd properly, this is what I get when I ran it

╰─➤  docker-compose run yocto-server
Creating network "jenkins_build-network" with driver "bridge"
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
Sign up to request clarification or add additional context in comments.

2 Comments

I tried the same command but for me I do not get that error. The session starts just fine. What things to I need to add in your oppinion to set it up properly?
If you run the container and immediatly start logging the container, what do you see? Does it exit instantly? You can log a container by executing docker log -f <containername>, which will follow the file and only return if the container stops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.