27

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>"
Nobita
  • 598
  • 3
  • 6
  • 16
  • 2
    Possible duplicate of [Docker, how to get container information from within the container?](https://stackoverflow.com/questions/20995351/docker-how-to-get-container-information-from-within-the-container) – arco444 Sep 04 '17 at 06: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. – Kuchara Jun 07 '23 at 14:35

3 Answers3

29

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 ...
yamenk
  • 46,736
  • 10
  • 93
  • 87
  • Then it would be restricted to that container only. But I could be running the script from any container. – Nobita Sep 04 '17 at 10:49
  • @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? – yamenk Sep 04 '17 at 13:47
  • 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) – Nobita Sep 04 '17 at 17:05
  • 6
    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. – tripleee Sep 06 '17 at 09:40
21

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

QNimbus
  • 355
  • 2
  • 6
9

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.

dgo
  • 3,877
  • 5
  • 34
  • 47