Skip to content

Instantly share code, notes, and snippets.

Last active February 2, 2023 15:35
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
Copy link


Copy link

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

Copy link

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

Copy link

sanbor commented Feb 22, 2017

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

Copy link

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

Copy link

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

Copy link

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

Copy link

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
npm ERR! remote: Invalid username or password.

Copy link

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.

Copy link

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)

Copy link

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

Copy link

docker run -it --entrypoint /bin/sh

Copy link

abhidp commented Oct 29, 2018

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

thank you.. are a savior

Copy link

gpetz commented Mar 6, 2019

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

Copy link

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

Copy link

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

Copy link

stokito commented Nov 22, 2020

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

Copy link

montaro commented Jan 13, 2021


Copy link

RafaelWO commented Feb 7, 2021

For me none of the above commands worked. I ran docker inspect <image> and got

"Cmd": [
"Entrypoint": [

I figured out that docker run -it --entrypoint "" <image> sh works in this case.

Copy link

mitchwongho commented Feb 7, 2021 via email

Copy link

I have this in my Dockerfile to run java 16 (watch out for the modified part)

FROM adoptopenjdk/openjdk16:jre-16.0.1_9-ubi-minimal

RUN microdnf install curl ca-certificates \
    && microdnf update \
    && microdnf clean all \
    && mkdir /deployments \
    && chown 1001 /deployments \
    && chmod "g+rwX" /deployments \
    && chown 1001:root /deployments \
    && curl${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION} -o /deployments/ \
    && chown 1001 /deployments/ \
    && chmod 540 /deployments/ \
    && echo "securerandom.source=file:/dev/urandom" >> /opt/java/openjdk/conf/security/

Copy link

pere000 commented Feb 1, 2022

sudo docker run -it --entrypoint /bin/bash node:latest #Thanks to @jghaines
...........9e0d1dcbc703 node:latest "/bin/bash" 12 minutes ago Up 12 minutes trusting_yonath #Thanks to Solomon Hykes
sudo docker trusting_yonath bash -c "echo 'Hello World, again!' >> file.txt" #Thanks to Stackoverflow
...........bin boot dev etc file.txt home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var #Thanks to Stallman&Torvalds

Thanks to Github

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment