Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Docker 'run' command to start an interactive BaSH session
# Assuming an Ubuntu Docker image
$ docker run -it <image> /bin/bash
@boyney123

This comment has been minimized.

Copy link

@boyney123 boyney123 commented Jan 4, 2017

Thanks

@arjabbar

This comment has been minimized.

Copy link

@arjabbar arjabbar commented Jan 28, 2017

You can also do it without the /bin/ part
docker run -it ubuntu bash
or if you literally need nothing else but bash...
docker run -it bash

@jghaines

This comment has been minimized.

Copy link

@jghaines jghaines commented Feb 13, 2017

This won't work if your image has a defined ENTRYPOINT. For these cases use:
docker run -it --entrypoint /bin/bash <image>

@sanbor

This comment has been minimized.

Copy link

@sanbor sanbor commented Feb 22, 2017

docker run -it stands for docker run --interactive --tty. Found this info it in man docker-run.

@augnustin

This comment has been minimized.

Copy link

@augnustin augnustin commented Mar 24, 2017

👍
It could be also nice to share current folder between contexts.

@augnustin

This comment has been minimized.

Copy link

@augnustin augnustin commented Mar 24, 2017

docker run -v $(pwd):/root -it node /bin/bash

@dkowsley

This comment has been minimized.

Copy link

@dkowsley dkowsley commented Jan 4, 2018

@arjabbar /bin/bash and bash are not always the same thing. The former is a direct reference to an executable and the latter is detected by searching $PATH for an executable with the name bash.

As for the second line: docker run -it bash does not run bash inside of your image; it downloads the bash:latest image and runs it. It is syntactically equivalent to running docker run -it --entrypoint /usr/local/bin/bash bash

@johngrabner

This comment has been minimized.

Copy link

@johngrabner johngrabner commented Jan 19, 2018

Is there a form of this I can use to log into github as part of the "docker build ."?
My node project uses private npm packages stored in githib. So my build dies with
npm ERR! /usr/bin/git ls-remote -h -t https://github.com/johngrabner/purple_shared_enums_cloud_and_charger.git
npm ERR! remote: Invalid username or password.

@fusir

This comment has been minimized.

Copy link

@fusir fusir commented Mar 31, 2018

I find that when I run docker run -it --entrypoint bash <image> that it does not save any of my changes.

@PRElias

This comment has been minimized.

Copy link

@PRElias PRElias commented Jun 7, 2018

I think it never saves your changes because they are made to the container and not the image itself.
You can save it as a new image like this:
$ docker commit <container_id> new_image_name:tag_name(optional)

As you are on bash, you have to skip it to root or use another terminal (take a note of your container ID)

@berezovskyi

This comment has been minimized.

Copy link

@berezovskyi berezovskyi commented Jul 17, 2018

docker run -it --entrypoint /bin/sh for the images w/o bash.

@Andres-Hack

This comment has been minimized.

Copy link

@Andres-Hack Andres-Hack commented Oct 10, 2018

docker run -it --entrypoint /bin/sh

@abhidp

This comment has been minimized.

Copy link

@abhidp abhidp commented Oct 29, 2018

docker run -it --entrypoint /bin/sh for the images w/o bash.

thank you.. ..you are a savior

@gpetz

This comment has been minimized.

Copy link

@gpetz gpetz commented Mar 6, 2019

if you add --rm the container gets deleted if you exit it ;-)

@teebu

This comment has been minimized.

Copy link

@teebu teebu commented May 28, 2020

If you want to interact with an existing container, do:

docker exec -it <name> bash

(alpine has sh)
docker exec -it <name> sh

@sdrycroft

This comment has been minimized.

Copy link

@sdrycroft sdrycroft commented Nov 16, 2020

This won't work if your image has a defined ENTRYPOINT. For these cases use:
docker run -it --entrypoint /bin/bash <image>

🙌 Works for all, just use this!

docker run -it stands for docker run --interactive --tty. Found this info it in man docker-run.

Good to know

@stokito

This comment has been minimized.

Copy link

@stokito stokito commented Nov 22, 2020

I proposed to add a new command docker shell. Please leave your thought and vote

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.