containers-roadmap icon indicating copy to clipboard operation
containers-roadmap copied to clipboard

How to run "docker exec... " command in ECS

Open shoaib-tarams opened this issue 10 years ago • 160 comments

Hi I have three containers that are running in ECS. But the website comes up only when we run "docker exec..." command. I can do this by login into the server and running this command. But this shouldn't be used. So my question is how to run "docker exec..." command without logging into the server. You can give solution in AMAZON ECS console or using ecs-cli or any other which you know. Since using ecs-cli command, we can make a cluster, tasks etc from our local machine. So how to run docker exec command from local machine into the containers.

shoaib-tarams avatar Aug 03 '16 06:08 shoaib-tarams

+1

panga avatar Sep 06 '16 14:09 panga

+1

stongo avatar Sep 23 '16 19:09 stongo

can't you add your exec command to the dockerfile?

I'd like to run my rake db migrate task and not sure what is the most elegant way to go about it. It should runs only the first time when creating the cluster to create the database and seed it with test data

Vince-Cercury avatar Oct 13 '16 09:10 Vince-Cercury

+1

andreykats avatar Oct 13 '16 15:10 andreykats

+1

r1w1s1 avatar Jan 02 '17 20:01 r1w1s1

@VinceMD docker exec could be a valid use case when you want to update some code(git pull) in the container after image is built. Writing exec in dockerfile would require re-build the image, push the image, restart ECS task...

WillLiu360 avatar Jan 19 '17 17:01 WillLiu360

+1

royroque avatar Feb 14 '17 14:02 royroque

+1

calvinte avatar Feb 22 '17 02:02 calvinte

I would like this functionality. Researching various secrets injection solutions where the container wouldn't have to be modified to include aws tools

r631269 avatar Feb 28 '17 23:02 r631269

I built a tool ecsctl to do this. However, you will need to customize docker daemon configuration on container instances to listen on a port.

cxmcc avatar May 26 '17 16:05 cxmcc

@cxmcc what about security? How do you secure tcp port of Docker daemon?

panga avatar May 29 '17 11:05 panga

@panga Currently with networking configurations. externally only open this port to trusted network (vpn/bastion, etc) internally run a iptables rule to drop traffic going to that port from containers: iptables --insert INPUT 1 --in-interface docker+ --protocol tcp --destination-port MYDOCKERPORT --jump DROP Alternatively I believe using a tls cert may be possible, but I have not tried out.

cxmcc avatar May 30 '17 15:05 cxmcc

So...,if I am not wrong, right now it is not possible to run commands over running tasks, like "docker exec" way, isn´t it?

destebanm avatar Jun 21 '17 08:06 destebanm

@destebanm It's certainly possible to docker exec into a container that's running as part of an ECS task, but you currently need to identify the specific instance and container manually using ECS and Docker tooling and log in to the appropriate instance. This is clearly suboptimal, and we're tracking this as a feature request.

I'd be interested in hearing ideas for how this might work. What would be the ideal workflow around docker exec? Would people prefer that it be integrated into the web console, such that you can identify a task with the UI and get an interactive docker exec environment in the browser? Or would CLI integration be better?

nmeyerhans avatar Jun 21 '17 16:06 nmeyerhans

On the ECS Agent, you could marshall the unix socket to a TCP endpoint. This endpoint would need to be authed with a IAM token so that the console that is connecting to the socket is authenticated for a short period of time.

The best way of getting a quick shell would be from within the ECS Console. You could right click on the task itself, then you could open up a sh to the container. Otherwise you're using the cli to list the containers, get statistics, yada yada. It just seems more simple to look at the metrics of a service, then go into a container that way. Docker-cloud did this integration awhile back, but it works great and it's a simple way to get into your container to do a quick ls or curl to a database.

But you could also do a CLI integration that would behave in much of the same way, by connecting your docker cli to that specific socket via an AWS API. I might be missing something so please fill in the gaps if you have ideas!

ktruckenmiller avatar Jun 21 '17 21:06 ktruckenmiller

I just want to say that I would love to be able to exec into a running ecs container from my Macbook terminal.

It would make debugging so much quicker and easier.

dvizzini avatar Aug 09 '17 20:08 dvizzini

+1

rayjanoka avatar Dec 31 '17 05:12 rayjanoka

I am working in a development ECS cluster with EC2 instances that another developer built using his own key pair. Therefore I can't ssh into the instance to run 'docker exec...'. It would be great if something was made available to do this.

prcorcoran avatar Feb 02 '18 16:02 prcorcoran

@nmeyerhans Not sure if there's a better issue/repo to discuss being able to exec into an ECS task container but this seems to be the best I can find for now.

I was considering spending some time writing an ECS executor for Gitlab Runner that would allow people to run CI jobs as one off ECS tasks but the Gitlab Runner model for both Docker and Kubernetes is to run a container and then exec into it so it can receive the output easily.

I was thinking about seeing if I could hack something together where it overrides the command each time with the script lines concatenated together and then try to read the logs out of Cloudwatch log but it's horribly ugly and the delay on fetching the logs is probably going to be impractical, let alone not being able to support things like after_script (although that's less needed for my use cases right now).

If being able to exec into an ECS task container was possible then I think an ECS executor for Gitlab should be easy enough to write and would be a real benefit for my company. Coupled with Fargate that would be a really, really interesting way of running our CI workloads. That said, I'm also considering just waiting for EKS access and then moving to Kubernetes executors as that's the least work to get this off the Docker-Machine runners I'm using. I expect that will probably be the thing that moves me from ECS to Kubernetes for production services as well although I do prefer the relative simplicity of ECS to k8s.

tomelliff avatar Feb 17 '18 11:02 tomelliff

@nmeyerhans when you say:

It's certainly possible to docker exec into a container that's running as part of an ECS task, but you currently need to identify the specific instance and container manually using ECS and Docker tooling and log in to the appropriate instance.

Can you explain how to do that? That would suffice for me as a workaround...

harlantwood avatar Mar 17 '18 00:03 harlantwood