Docker Community Forums

Share and learn in the Docker community.

What is the best way to execute a shell script via network on docker-compose on an image without a network service?

Hi,

I’m looking for a suggestion for a good way to access a container that is based off an image without a network service, since i’m using docker-compose the containers only talk to one another via network and the container i need just runs a shell script.

Ideas? should i really be considering adding a webserver with my code to just to access a shell script between containers?

You can access the container from your host with

docker-compose exec SERVICE bash

You have to be in the folder where the compose file is located. Replace SERVICE with the name you gave the service in the compose file and bash with any command you want to run in it.

That’s not what i meant,

I want to execute a shell script from one container using another one only the one with the script doesn’t have a webserver or anything to communicate via network.

In other words:

Container A - Has a shell script and no exposed ports

Container B- Need to run the script on A

I don’t want to use the Host machine as a proxy between the two, i was thinking of adding something to container A so it can communicate over the network

Sorry for my misunderstanding.
If you start both containers from the same compose file they automatically share a private network and can connect to each other using the service names you gave them in the compose file. Next you need something on container A that listens for commands. This could be an application, or you could install a ssh server and call the script directly from container B. Like this all is completely isolated from the host.

It’s not your fault i’m bad at explaining things…
That’s pretty much what i thought\found, either app or ssh but since my main container is running from a webserver with php i don’t think ssh will work out well at that level.
right now i’ve added apache to the other container so it seems to be ok but i thought maybe there’s a better solution for this.

There is always a better solution. For example if you have Perl and Mojolicious installed on container A you can start a server with this one-liner:

perl -Mojo -E 'a("/run" => sub ($c) { system "/path/to/script"; $c->render(text => "Script started!") })->start' daemon

Now whenever you make a call to http://container_a:3000/run the script is executed.

Thanks!
This is similar to gunicorn in python etc.
I might just take this over python or node.js in production, any of them needs stuff to be installed on the container.
But for now i chose php since i need it to have a page rather than an api call.