Home > Back-end >  Run a container from an image and shell into it in one step? (i.e. docker run and docker exec in sin
Run a container from an image and shell into it in one step? (i.e. docker run and docker exec in sin

Time:12-01

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.

Question

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

CodePudding user response:

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!"
  • Related