Docker

Run Bash in Ubuntu Docker Container With a Single Command

Docker is one of the widely used solutions to build and run programs, applications, and software under different environments or OS systems. This can be done through Docker images that run the containers of different platforms such as Linux containers by executing “ubuntu” or “alpine” images, Windows containers, and MacOS. This feature of Docker helps us to execute the application of different platforms like the Linux application on Windows OS.

In this article, we will demonstrate how to run bash in Ubuntu Docker container with a single command.

How to Run Bash in Ubuntu Docker Container With a Single Command?

Bash is a shell and command line language that is mostly used as the default login shell in the Linux distribution.

To run the bash shell in an Ubuntu container using a single command, simply use the given command:

docker run -it ubuntu:latest /bin/bash

 

In the above command:

  • docker run” is an official Docker command used to create and start the container from the provided image. To run Ubuntu or Linux containers, we have utilized the “ubuntu:latest” Docker image.
  • -it” option is utilized to execute the container interactively and allocate the TTY pseudo-terminal to the container.
  • /bin/bash” are the executables of the container to run bash in the Ubuntu Docker container:

Alternatively, In order to automatically remove the container after using the Ubuntu bash shell, use the “–rm” flag:

docker run --rm -it ubuntu:latest /bin/bash

 

Now, the user can use the container to do any Linux-based activity or run any command. For demonstration, follow the listed instructions.

Step 1: List all Ubuntu Container Directories

List down all Ubuntu container directories or files using the “ls” command:

ls

 

The output shows all the container directories.

Step 2: Open “home” Directory

Next, navigate to any of the listed directories. For instance, we have opened the “home” directory using the “cd” command:

cd home

 

To view the currently opened directory, utilize the “pwd” command:

pwd

 

Step 3: Create New File Using “Touch” Command

Next, create a new file using the Linux command line utility “touch” command:

touch file1.txt

 

For confirmation, list down all the files and directories of the currently open directory:

ls

 

Here, you can see we have successfully created the new file in a Linux container using Bash shell:

Step 4: Exit the Bash Shell

To exit the container bash shell, utilize the “exit” command:

exit

 

That’s all about running bash in Ubuntu Docker container with a single command.

Conclusion

To run the bash shell in the Ubuntu Docker container using a single command, simply execute the “docker run -it ubuntu:latest /bin/bash” command. This command will automatically run the container and open the bash shell, where the user can execute any Linux command. This blog has demonstrated how to run bash in Ubuntu containers.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.