32

So I'm trying to run the following shell script which requires the container id/name of the container (in which the script would be run) dynamically.

One way could be to do docker ps and then getting the Container Id, but that won't be dynamic.

So is there a way to do this dynamically?

#!/bin/bash docker exec <container id/name> /bin/bash -c "useradd -m <username> -p <password>" 
2
  • 2
    Possible duplicate of Docker, how to get container information from within the container? Commented Sep 4, 2017 at 6:47
  • I did not get the usecase. Do you want to know from inside container what is it's ID/name, or just know ID of a container created by docker run? Also, if you are inside container already, maybe there is no need to run docker exec? And if you are outside, you anyway need to know name/id to exec. Commented Jun 7, 2023 at 14:35

3 Answers 3

33

You can give your container a specific name when running it using --name option.

docker run --name mycontainer ... 

Then your exec command can use the specified name:

docker exec -it mycontainer ... 
Sign up to request clarification or add additional context in comments.

4 Comments

Then it would be restricted to that container only. But I could be running the script from any container.
@Nobita if you have 2 different containers running, how would you know (manually) which container you want the script to run in. Is it the container image, or something else?
Consider we have multiple containers for the same image and we don't know which container we are running the script from (as all of them are identical)
You're not answering the question. How would you know which one to connect to? A common trick is docker ps -l -q which gets you the id of the latest container you started but obviously that's not necessarily what you want here.
24

You can start your container and store the container id inside a variable like so:

container_id=$(docker run -it --rm --detach busybox) 

Then you can use the container id in your docker exec command like so:

docker exec $container_id ls -la 

or

docker stop $container_id 

Note: Not using a (unique) name for the container but using an ID instead is inspired by this article on how to treat your servers/containers as cattle and not pets

Comments

10

I just figured out a way to do this that works for this. I'm constantly going into my container in bash, but each time I do it I have to look up the id of the running container - which is a pain. I use the --filter command like so:

docker ps -q --filter="NAME={name of container}" 

Then the only thing that's output is the id of the container, which allows me to run:

docker exec -it $(docker ps -q --filter="NAME={name of container}") bash 

...which is what I really want to do in this case.

You can filter by

id, name, label, exited, status, ancestor, beforesince, volume, network, publishexpose, health,isolation, or is-task 

The documentation for filter is here.

1 Comment

This is the answer! I already know how to do it with a command, checking the id, giving another command. But this one gives me the constant one-liner I need! Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.