[英]docker-compose ssh from one container into another
I want to be able to execute a command from a jenkins server inside a build container.我希望能够从构建容器内的 jenkins 服务器执行命令。 I imagine this would have to be done using ssh.
我想这必须使用 ssh 来完成。 Here is my attempt sofar:
到目前为止,这是我的尝试:
Dockerfile for the build server: Dockerfile 用于构建服务器:
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: docker-compose 文件:
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:现在我尝试在 jenkins 容器和 ssh 容器中运行 bash 到 yocto 容器中:
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.
尽管该错误已在 docker 文件中公开并在 compose 文件中映射,但该错误看起来好像端口根本没有打开。
two things两件事情
run
command you can confirm for up
run
命令会发生这种情况,您可以确认up
you need to setup the sshd properly, this is what I get when I ran it您需要正确设置 sshd,这是我运行它时得到的
╰─➤ 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.