Docker exec command without the container ID

Yes, you can do this by naming the container with --name. Note that your command with container/container is likely referencing an image name and not the container.

➜  ~ docker run --name my_nginx -p 80:80 -d nginx
d122acc37d5bc2a5e03bdb836ca7b9c69670de79063db995bfd6f66b9addfcac

➜  ~ docker exec my_nginx hostname
d122acc37d5b

Although it won't save any typing, you can do something like this if you want to use the image name instead of giving the container a name:

docker run debian
docker exec -it `docker ps -q --filter ancestor=debian` bash

This will only work if you're only running one instance of the debian image.

It does help if you're constantly amending the image when working on a new Dockerfile, and wanting to repeatedly run the same command in each new container to check your changes worked as expected.

Tags:

Docker