简体   繁体   English

docker-compose ssh 从一个容器到另一个容器

[英]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两件事情

  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这可能是因为 yocto-server 容器没有阻塞命令,如果命令退出,容器将停止, run命令会发生这种情况,您可以确认up
  2. the command could be failing该命令可能失败

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.

相关问题 如何在docker-compose中从一个容器连接到另一个容器? - How to connect from one container to another in docker-compose? 无法从 docker-compose 的另一个容器访问 api 的 docker 容器 - docker container of api not accessible from another container of docker-compose Docker-compose 从另一个容器访问 MySQL 容器 - Docker-compose MySQL container access from another container 另一个docker-compose的docker-compose链接容器 - docker-compose link container of another docker-compose 从一个容器内部运行docker-compose堆栈中的另一个容器运行mongoexport - running mongoexport from inside one container to another within a docker-compose stack docker-compose api 在同一网络中从一个容器调用到另一个容器 - docker-compose api call from one container to another in the same network 从 Docker 容器运行 docker-compose - Run docker-compose from Docker container 在另一个Dockerized容器中运行docker-compose - Running docker-compose in another Dockerized Container Docker-compose和节点容器不是主要的容器 - Docker-compose and node container as not the primary one 如何从另一个 docker 容器中运行的应用程序启动 Docker-Compose 中的 docker 容器 - How do I start a docker container in Docker-Compose from an app running in another docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM