Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Run a container from an image and shell into it in one step? (i.e. docker run and docker exec in single command)

I often do the following:

docker run -dt myimage
docker ps # This step gives the container id necessary for next step
docker exec -it <container-id> bash

Ideally I’d like to do it all in one line

docker run -dt myimage && docker exec -it <id> bash

but I don’t know how to get the container id to docker exec without looking it up in a separate step.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Question

Is there a one-liner to run an image and shell into its container?

>Solution :

You can specify a name for the running container and reference it that way.

docker run --name mycontainer -d myimage
docker exec -it mycontainer bash

You can also spawn a container and jump right into a shell.

docker run --name mycontainer --rm --entrypoint="" -it myimage bash

Or, you can run a single command inside the container and then exit.

docker run --name mycontainer --rm --entrypoint="" myimage echo "Hello, World!"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading