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"