2

In the cloud, I have multiple instances, each running a container with a different random name, e.g.:

CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS                        NAMES
5dc97950d924        aws_beanstalk/my-app:latest   "/bin/sh -c 'python    3 hours ago         Up 3 hours          80/tcp, 5000/tcp, 8080/tcp   jolly_galileo    

To enter them, I type:

sudo docker exec -it jolly_galileo /bin/bash

Is there a command or can you write a bash script to automatically execute the exec to enter the correct container?

1
  • You want to enter the correct container based on what (image name, ...) ? Commented Oct 8, 2015 at 9:05

3 Answers 3

3

"the correct container"?

To determine what is the "correct" container, your bash script would still need either the id or the name of that container.

For example, I have a function in my .bashrc:

deb() { docker exec -u git -it $1 bash; }

That way, I would type:

deb jolly_galileo

(it uses the account git, but you don't have to)

Sign up to request clarification or add additional context in comments.

2 Comments

you will enter the last container started with docker exec -it $(docker ps -lq) bash but there is no magic, as VonC said.
This worked for me! sudo docker exec -it $(sudo docker ps -lq) bash
1

Here's my final solution. It edits the instance's .bashrc if it hasn't been edited yet, prints out docker ps, defines the dock function, and enters the container. A user can then type "exit" if they want to access the raw instances, and "exit" again to quit ssh.

commands:
  bashrc:
    command:  if ! grep -Fxq "sudo docker ps" /home/ec2-user/.bashrc; then echo -e "dock() { sudo docker exec -it $(sudo docker ps -lq) bash; } \nsudo docker ps\ndock" >> /home/ec2-user/.bashrc; fi

1 Comment

Nice feedback in addition of my answer, thank you. +1
1

As VonC indicated, usually you have to make some shell scripting of your own if you find yourself doing something repetitive. I made a tool myself here which works if you have Bash 4+.

Install

wget -qO- https://raw.githubusercontent.com/Pithikos/dockerint/master/docker_autoenter >> ~/.bashrc

Then you can enter a container by simply typing the first letters of the container.

$> docker ps
CONTAINER ID        IMAGE               ..
807b1e7eab7e        ubuntu              ..
18e953015fa9        ubuntu              ..
19bd96389d54        ubuntu              ..

$> 18
root@18e953015fa9:/#

This works by taking advantage of the function command_not_found_handle introduced in Bash 4. If a command is not found, the script will try and see if what you typed is a container and if it is, it will run docker exec <container> bash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.