- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I get into a Docker container's shell?
Once you have your Docker container up and running, you can work with the environment of the Docker container in the same way you would do with an Ubuntu machine. You can access the bash or shell of the container and execute commands inside it and play around with the file system. You can build, test, and deploy your applications inside the container itself.
Predominantly, there are 3 ways to access the shell of a running container. These are -
- Using the Docker run command to run a container and access its shell.
- Using the Docker exec command to run commands in an active container.
- Using the Docker start command and attach a shell to a stopped container.
In this article, we will discuss all these approaches one by one with practical examples. Let’s see how to do so.
Docker Run Command
If you don’t have a container running, you can use the Docker run command to create and run a container associated with an image and access the bash of the container. Let’s try to run an ubuntu container and access it’s bash.
$ docker run -it --name=myubuntu ubuntu:latest bash
In the above command, we have used the -i (interactive) and -t (pseudo-TTY) options which will allow us to interact with the container using a terminal driver. The bash command at the end is run as soon as the container is started. Hence, after executing this command, the daemon will automatically create the container, run it, and provide us access to its shell.
Please note that if you don’t have an Ubuntu image on your system, the daemon will pull the image and create and run a container automatically.
Once you have access to the bash of the container, you can start interacting with the Ubuntu environment and play with its file system.
Docker Exec Command
The Docker exec command is used to run a command inside a container that is already running. You can use this command only if the primary process of the container is running. Any command executed inside the container will run in the default working directory unless you specify the desired directory using the -w option. Moreover, you won’t be able to run any quoted or chained command. Only commands that are executable can be run.
The syntax of the Docker exec command is -
$ docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
You can use this command to start a new bash session of a container that is already running elsewhere.
$ docker exec -it myubuntu bash
This command uses the interactive and pseudo-TTY options to start the bash of the ubuntu container that is already running.
Docker Start Command
If you have a container in the exited state and you want to start a bash associated with that container, you can use the Docker start command along with the --attach and --interactive options. This will attach a new terminal and will allow you to interact with the container easily. Let’s see how to do so.
The original syntax of the Docker start command is -
$ docker start [OPTIONS] CONTAINER [CONTAINER...]
If you want to check the status of the container, you can try to list all the containers.
$ docker container ps -a
You can use the following command to create a bash session of a stopped container -
$ docker start -ai myubuntu
To sum up, in this article, we discussed how to get into a Docker container’s shell for a running, stopped, or by creating a new container. We have used the Docker run, exec, and start commands to do so. According to your own use case, you can use any of the above-discussed methods to achieve your goal.
Kickstart Your Career
Get certified by completing the courseGet Started