Surfs Up
Surfs Up

Reputation: 63

Connecting to container using ansible

I have build a docker image and started the container using ansible. I'm running into an issue trying to create a dynamic connection to the container from the docker host to set some environment variable and execute some script. I know ansible does not use ssh to connect to the container where I can can use the expect module to run this command "ssh root@localhost -p 12345". How do I add and maintain a connection to the container using ansible docker connection plugin or pointing directly to the docker host? This is all running in AWS EC2 instance.

I think I need to run ansible as an equivalent to this command use by ansible to connect to the container host "docker exec -i -t container_host_server /bin/bash".


Thanks in Advance, DT

Upvotes: 1

Views: 4425

Answers (1)

gile
gile

Reputation: 5976

To set environment variables you can use parameter "env" in your docker_container task.

In the docker_container task you can add the parameter "command" to override the command defined as CMD in the Dockerfile of your docker image, somethning like

command: PathToYourScript && sleep infinity

In your example you expose container port 22, so it seems you want run sshd inside container. Although it's not a best practice in Docker, if you want sshd running you have to start that using command parameter in the docker_container task:

command: ['/usr/sbin/sshd', '-D']

Doing it (and having defined a user in the container), you'll be able to connect your container with

ssh -p 12345 user@dockerServer

or, as for your example, "ssh -p 12345 root@localhost" if your image already defined root user and you are working on localhost.

Upvotes: 2

Related Questions