Anton Rand
Anton Rand

Reputation: 342

Running a Bash Script from (on Docker Container B) from Docker Container A

I have two Docker Containers configured through a Docker Compose file.

Both start up fine. But as part of the build process in TeamCity - I would like the Agent (Container A) to run a bash script which is on Docker Container B (Only B can run this script).

I tried to set this up using the SSH build step in Team City, but I get connection refused.

Further reading into it shows that SSH isn't enabled in containers and that I shouldn't really be trying to SSH into a container.

So how can I get Container A to run the script on Container B and see the output of the script on A?

What is the best practice for this?

Upvotes: 1

Views: 917

Answers (1)

Carlos Rafael Ramirez
Carlos Rafael Ramirez

Reputation: 6234

The only way without modifying the application itself is through SSH. It is completely false you cannot SSH to a container. I use SSH to a database container to run database export inside it.

First be sure openssh-server is installed on B. Then you must setup a passwordless connection between A and B.

Then be sure you link your containers in the docker-compose file so you won't need to expose the SSH port.

Snippet to add in Dockerfile for container B

RUN apt-get install -q -y openssh-server
ADD id_rsa.pub /home/ubuntu/.ssh/authorized_keys
RUN chown -R ubuntu:ubuntu /home/ubuntu/.ssh ; \
  chmod 700 /home/ubuntu/.ssh ; \
  chmod 600 /home/ubuntu/.ssh/authorized_keys

Also you can run the script outside the containers using docker exec in a crontab in the host. But I think you are not looking for this extreme solution.

I can help you via comments

Regards

Upvotes: 1

Related Questions