Docker Community Forums

Share and learn in the Docker community.

How do I run a script in a container from the outside

I’ have been given a task which I am failing miserable to crack. I’ve been given a container called 46e which has to be started with a docker start 46e. No docker run ... or anything like that.

Once it is started I’ve been told I need to shell into it and run:

export GENESIS_FILE=/home/indy/ledger/clear/pool_transactions_genesis
start_indy_node Node1 0.0.0.0 9701 0.0.0.0 9702 &

Then everything is up and running. The challenge I’ve been given is to automate it, perhaps with a ExecPost script in a systemd unit file, but I am stuck at first base and have not even found a way of running the commands manually.

I have tried:

/usr/bin/docker exec -it 46e bash -c "export GENESIS_FILE=/home/indy/ledger/clear/pool_transactions_genesis; start_indy_node Node1 0.0.0.0 9701 0.0.0.0 9702 &"
/usr/bin/docker exec -it 46e bash -c "export GENESIS_FILE=/home/indy/ledger/clear/pool_transactions_genesis && start_indy_node Node1 0.0.0.0 9701 0.0.0.0 9702 &"
/usr/bin/docker exec -it 46e 'GENESIS_FILE=/home/indy/ledger/clear/pool_transactions_genesis ; start_indy_node Node1 0.0.0.0 9701 0.0.0.0 9702 &'
 /usr/bin/docker exec -it 46e 'export GENESIS_FILE=/home/indy/ledger/clear/pool_transactions_genesis ; start_indy_node Node1 0.0.0.0 9701 0.0.0.0 9702 &'

The first two fail silently the third and fourth fail with a message like:

stat export GENESIS_FILE=/home/indy/ledger/clear/pool_transactions_genesis ; start_indy_node Node1 0.0.0.0 9701 0.0.0.0 9702 &: no such file or directory: unknown

I’ve tried various different forms of the commands with different quoting and using && instead of ; and with and without the export word but everything has failed.

How can I achieve what I have been tasked with?

The container may have originally been created with a docker-compose.yml file with a command: file but that does not seem to execute (I added an echo njh >> somefile at the end of it and no file got created) so that is also a non-starter.

The main secret here is not to use the t in docker exec -it 46e bash -c ..... when scripting. I also added a nohup and ended up with

docker exec -i 46e bash -c 'GENESIS_FILE=/home/indy/ledger/clear/pool_transactions_genesis nohup start_indy_node Node1 0.0.0.0 9701 0.0.0.0 9702 &>/dev/null &'
1 Like

I tried to solve it for you but I failed, so thanks for coming back to share your solution :slight_smile: