Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker exec command without the container ID

Tags:

docker

How can do something like:

docker exec -it 06a0076fb4c0  install-smt 

But use the name of the container instead

docker exec -it container/container  install-smt 

I am running a build on CI server so I can not manually input the container ID.

How can I achieve this?

like image 846
johnnyshrewd Avatar asked Dec 11 '17 15:12

johnnyshrewd


People also ask

How do I run a command outside the docker container?

To run a command as a different user inside your container, add the --user flag: docker exec --user guest container-name whoami.

What is Docker EXEC command?

Description. The docker exec command runs a new command in a running container. The command started using docker exec only runs while the container's primary process ( PID 1 ) is running, and it is not restarted if the container is restarted. COMMAND will run in the default directory of the container.


2 Answers

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 
like image 186
TJ Biddle Avatar answered Oct 06 '22 19:10

TJ Biddle


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.

like image 41
Malvineous Avatar answered Oct 06 '22 18:10

Malvineous