Main Tutorials

How to access the Docker container’s shell?

This article shows you how to use docker exec -it <container ID or Name> <sh or bash> to access or get into the container’s shell.

Terminal

# docker exec -it <contaier ID or name> <command>

$ docker exec -it a5df11a07a0e bash
/opt/app #  

$ docker exec -it stoic_bhaskara sh
/opt/app #

The docker exec -it means run a command on a running Docker container, in interactive mode.

1. DockerFile

This docker container is using the openjdk11:alpine-jre image.

Dockerfile

FROM adoptopenjdk/openjdk11:alpine-jre

ARG JAR_FILE=target/markdown.jar
ARG SSL_FILE=cert.p12

WORKDIR /opt/app

COPY ${JAR_FILE} webapp.jar
COPY ${SSL_FILE} .

ENTRYPOINT ["java","-jar","webapp.jar"]

2. Access the Docker container’s shell

Build, start and run a webapp in a docker container

Terminal

$ cd project/webapp/

$ docker build -t webapp:0.1 .

$ docker run -d -p 80:8080 -p 443:8443 -t webapp:0.1  

List all running docker containers.

Terminal

$ docker ps

CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                                         NAMES
a5df11a07a0e        webapp:0.1    "java -jar app.jar"   8 seconds ago       Up 7 seconds        0.0.0.0:80->8080/tcp, 0.0.0.0:443->8443/tcp   stoic_bhaskara  

This command tries to access the above running docker container’s shell.

Terminal

# docker exec -it <contaier ID or name> <shell, sh|bash>

$ docker exec -it stoic_bhaskara sh
/opt/app #

$ docker exec -it a5df11a07a0e bash
/opt/app #                 

If we are trying to access something that doesn’t exist:

Terminal

$ docker exec -it 5c27d3cfdfa3 sh

Error response from daemon: Container 5c27d3cfdfa35c627c323996e5a092ee666cb07bd59285aead8a5ee02a89841c is not running

3. What is -it?

For reference.

Terminal

$ docker --help

exec        Run a command in a running container

$ docker exec --help

-i, --interactive          Keep STDIN open even if not attached
    --privileged           Give extended privileges to the command
-t, --tty                  Allocate a pseudo-TTY
Terminal

$ docker exec --help

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container

Options:
-d, --detach               Detached mode: run command in the background
    --detach-keys string   Override the key sequence for detaching a
                           container
-e, --env list             Set environment variables
-i, --interactive          Keep STDIN open even if not attached
    --privileged           Give extended privileges to the command
-t, --tty                  Allocate a pseudo-TTY
-u, --user string          Username or UID (format:
                           <name|uid>[:<group|gid>])
-w, --workdir string       Working directory inside the container

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments