0

Trying to get one docker-compose service to run a shell script that's in another container. Is there a way of doing this without having to install docker in docker?

In the example below, ideally I'd like to be able to run something like shell-svc /bin/bash helloworld.sh from helloworld.py in the mgr-svc python container.

I can run helloworld.sh independently using docker-compose like this:

docker-compose run shell-svc /bin/bash helloworld.sh

and helloworld.py using

docker-compose run mgr-svc python helloworld.py

Example files:

docker-compose.yml

services :

  shell-svc:
    build:
      context: .
      dockerfile: Dockerfile-shell
    image: shell-svc:1.0
    networks:
      - test-net
    expose:
      - "22"
      - "8088"
      - "50070"     

  mgr-svc:
    build:
       context: .
       dockerfile: Dockerfile-python
    image: mrg-svc:1.0
    networks:
      - test-net

networks:
 test-net:
version: "3.5"

Dockerfile-python

FROM python:3.7
RUN pip install paramiko

COPY helloworld.py helloworld.py

Dockerfile-shell

FROM debian:stretch-slim

USER root

RUN apt-get update && \
    apt-get install -y openssh-server


RUN eval `ssh-agent -s` && \
    mkdir ~/.ssh && \
    echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config && \
    cat /etc/ssh/ssh_config && \
    chmod go-w /root && \
    chmod 700 /root/.ssh 


EXPOSE 22 8088 50070

COPY helloworld.sh ./helloworld.sh

CMD service ssh start && while true; do sleep 3000; done

helloworld.py

import paramiko

if __name__ == '__main__':
    print ("hello from python")

    ssh = paramiko.SSHClient()
    ssh.connect("shell-svc", username="root", password="password")
    ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(helloworld.sh)

helloworld.sh

#!/bin/sh

echo "hello from shell"
yahop19531
  • 193
  • 1
  • 11
  • Or maybe: https://stackoverflow.com/questions/42352104/run-shell-script-inside-docker-container-from-another-docker-container – Don't Panic Nov 18 '20 at 12:28
  • The ssh access option looks reasonable, I thought I might be missing a more obvious solution. I just need to work out how to set the ssh password in `shell-svc` and to add 'shell-svc' to `known_hosts` on `mgr-svc` – yahop19531 Nov 18 '20 at 12:53
  • The linked question runs through most of the possible ways of doing this, but either using the Docker socket (and potentially being able to root the whole host) or running an sshd (with big questions around credential management) are both pretty complex setups. Especially if it's just a shell script, it'd be better to `COPY` it into the image that needs to run it. – David Maze Nov 18 '20 at 13:23

0 Answers0