Share
Explore

How to run Linux in Docker to get to a linux command line

Devops people people love Docker because:
If you need to quick provision desktop computer images for software testing or configurtion: You CAN do this with VMWare or Oracle Virtual Box: But Devops people love using Docker Container for this purpose.
How to work with DOCKER:
Download and install Docker Desktop.
We download Docker CONTAINERS which contain the software to be installed PLUS ALL the connectors, drivers, other configuration elements.
The process of doing this is called: DOCKERIZATION or containerization of your software.
image.png

How does the command line interface of your operating system have connection to docker image:

image.png
The connection between your operating system's command line interface (CLI) and a Docker image is facilitated by the Docker software installed on your system.
Here’s a step-by-step explanation of how the process works:

1. Installation of Docker:

When you install Docker on your system (be it Windows, macOS, or Linux), it includes the Docker Engine, which is responsible for creating and managing containers.
Docker also provides a command-line tool called docker, which you can use to interact with the Docker Engine.

2. Using Docker Command-Line Tool:

You can use the docker command in your system's CLI to give instructions to the Docker Engine.
The docker command-line tool can communicate with the Docker Engine, which is running in the background as a service (daemon).

3. Pulling a Docker Image:

When you run a command like docker pull ubuntu, the docker CLI tool sends a request to the Docker Engine to pull the specified image (in this case, ubuntu) from Docker Hub (a cloud-based registry service for sharing Docker images).
The Docker Engine then handles the process of downloading the image to your local machine.

4. Running a Docker Container:

After downloading an image, you can create a new container from that image using a command like docker run ubuntu. Again, the docker CLI tool sends this command to the Docker Engine.
The Docker Engine creates a new container from the specified image and runs it. It also sets up the necessary networking and storage interfaces for the container to operate.

5. Interaction with the Container:

You can interact with the running container using the docker CLI tool, which you get to by typing docker at the command prompt of the terminal windows. For example, you can execute commands inside the container using docker exec, stop the container using docker stop, and so on.
The docker CLI tool sends all these commands to the Docker Engine, which then interacts with the appropriate container to perform the requested actions.

6. Docker Daemon:

A Daemon is a headless process running in the process table of your Operating System.
The Docker Engine (or daemon) continuously runs in the background on your system, waiting for commands from the docker CLI tool. It manages all aspects of Docker containers, including creating, starting, stopping, and destroying containers, as well as building images and managing networks and volumes (storage: directories on the hard drive).

Summary:

In essence, the CLI on your operating system is used to send commands to the Docker daemon, which performs actions related to Docker images and containers.
The Docker daemon handles the low-level tasks of container management, while you use the docker CLI tool to easily issue commands to the daemon.
The Docker daemon communicates with external services like Docker Hub to download images and performs various operations on containers based on the commands it receives from the docker CLI tool.
Running a Linux distribution inside a Docker container is a straightforward task.
Below are the steps you can follow to run a Linux distribution (for example, Ubuntu) and get to a Linux command line using Docker.
Note that you must have Docker installed on your system. If it is not installed, you can download and install it from the .

1. Pull a Linux Docker Image:

Start by pulling a Linux Docker image from Docker Hub. Open a terminal or command prompt (on Windows) and type the following command to pull the latest Ubuntu image:

docker pull ubuntu

2. Run a Container from the Image:

After pulling the image, run a container from the image with an interactive terminal.

docker run -it ubuntu

This command does the following:
docker run: This is the command used to start a new container.
-it: The -it flag attaches an interactive terminal to the container. The -i flag keeps STDIN open (i.e., allows you to input commands), and -t allocates a pseudo-terminal.
ubuntu: This specifies the image to use to create the container.

3. Using the Linux Command Line:

After running the above command, you should find yourself inside the container with a command prompt, and you can start running Linux commands. For example, try running:
bashCopy code
echo "Hello, World!"

4. Exiting the Container:

To exit the container, you can type exit at the command prompt.

5. Managing Docker Containers:

If you need to manage Docker containers, use these additional commands:
To list all running containers:
bashCopy code
docker ps
To list all containers (including stopped ones):
bashCopy code
docker ps -a
To stop a container:
bashCopy code
docker stop <container_id_or_name>
To remove a container:
bashCopy code
docker rm <container_id_or_name>

6. Accessing a Running Container:

If you have a running container and want to access its command line, use the docker exec command:
bashCopy code
docker exec -it <container_id_or_name> /bin/bash

This will allow you to run a Linux environment inside a Docker container and access the command line to execute Linux commands.
Remember that the changes you make inside the container will not be saved after you exit the container unless you commit the changes to a new image. To save the state of a container as a new Docker image, you can use the docker commit command.

Pull a Linux Docker Image:
Start by pulling a Linux Docker image from Docker Hub. Open a terminal or command prompt (on Windows) and type the following command to pull the latest Ubuntu image:

bash
Copy code
docker pull ubuntu
Do I do this inside Docker desktop ? where do I do this?
ChatGPT
You perform these actions in your system's command line interface, not inside the Docker Desktop application (though Docker Desktop should be running). Here are the steps for different operating systems:

Windows:

Make Sure Docker is Running:
Open Docker Desktop to ensure it's running. If it’s not, start it.
Open Command Prompt or PowerShell:
You can search for cmd or powershell in the Windows start menu and open it.
Run the Docker Command:
Type the command docker pull ubuntu and press enter. This will pull the latest Ubuntu image from Docker Hub.

macOS:

Make Sure Docker is Running:
Open Docker Desktop to ensure it's running. If it’s not, start it.
Open Terminal:
You can open the Terminal application by searching for it using Spotlight (press Cmd + Space and type "Terminal").
Run the Docker Command:
Type the command docker pull ubuntu and press enter.

Linux:

Make Sure Docker is Running:
Run the command sudo systemctl start docker to ensure Docker is running.
Open Terminal:
Simply open your terminal.
Run the Docker Command:
Type the command docker pull ubuntu and press enter.
In all cases, make sure you have the necessary permissions to run Docker commands. On Linux, you might have to use sudo to run Docker commands, or you can add your user to the docker group to run Docker commands without sudo.

When you got it going:(base) PS C:\> cd .\lab_docker\
(base) PS C:\lab_docker> docker pull ubuntu
Using default tag: latest
aabed3296a3d: Already exists
b492494d8e01: Already exists
c6b84b685f35: Download complete
445a6a12be2b: Download complete
docker.io/library/ubuntu:latest

What's Next?
View a summary of image vulnerabilities and recommendations → docker scout quickview ubuntu
(base) PS C:\lab_docker>
ChatGPT
Great! You have successfully pulled the Ubuntu image to your local machine. Now, you can run a container based on this image and interact with the Ubuntu command line. Follow the next steps to run an Ubuntu container and start using the Linux command line:

Running the Ubuntu Container:

Run the following command in your PowerShell:
bashCopy code
docker run -it ubuntu
docker run: This command creates and starts a new container from the image.
-it: These options allow you to interact with the container.
-i keeps the standard input open for commands.
-t allocates a pseudo-TTY, which makes it behave like a terminal.
ubuntu: This is the name of the image you pulled.
Wait for Docker to start the container.
Once the container starts, you should see a command prompt that looks something like this:
bashCopy code
root@1234567890ab:/#
This is the command prompt of the Ubuntu container, and you can enter Linux commands here.

Some Basic Linux Commands You Can Try:

Check the Ubuntu Version:
bashCopy code
lsb_release -a
This will show you the version of Ubuntu running inside the container.
List Files in the Current Directory:
bashCopy code
ls
This will list the files and directories in the current directory.
Change Directory:
bashCopy code
cd /path/to/directory
This will change the current directory to the specified path.
Create a New File:
bashCopy code
touch filename
This will create a new empty file named filename.
View the Contents of a File:
bashCopy code
cat filename
This will display the contents of filename.

Exiting the Ubuntu Container:

When you are done exploring, you can exit the Ubuntu container by typing:
bashCopy code
exit

This command will stop the container and return you to your local command prompt.
Feel free to ask if you have more questions or need further assistance with Docker and Linux commands!


now:Wait for Docker to start the container.
Once the container starts, you should see a command prompt that looks something like this:

bash
Copy code
root@1234567890ab:/#
This is the command prompt of the Ubuntu container, and you can enter Linux commands here.
ChatGPT
Great to hear that you've successfully accessed the Ubuntu command line inside the Docker container!
Now you are inside an Ubuntu Linux environment and you can start running Linux commands to navigate through directories, manage files, install software, and much more. Below are a few more basic Linux commands that you might find helpful:

Navigation Commands:

pwd (Print Working Directory):
Use this command to display the current directory.
bashCopy code
pwd
cd (Change Directory):
Use this command to navigate to a different directory.
bashCopy code
cd /path/to/directory

File Management Commands:

ls (List Files):
Use this command to list all files and directories in the current directory.
bashCopy code
ls
touch (Create Empty File):
Use this command to create a new empty file.
bashCopy code
touch filename
mkdir (Make Directory):
Use this command to create a new directory.
bashCopy code
mkdir directoryname
rm (Remove Files or Directories):
Use this command to delete files or directories.
bashCopy code
rm filename rm -r directoryname

System Information Commands:

uname -a (Display System Information):
Use this command to view system information.
bashCopy code
uname -a
free -m (Display Memory Information):
Use this command to view memory usage in MB.
bashCopy code
free -m

Software Management Commands:

apt update (Update Package List):
Use this command to update the list of available packages from the repositories.
bashCopy code
apt update
apt install packagename (Install Software):
Use this command to install a software package.
bashCopy code
apt install packagename

Exiting the Docker Container:

To exit the container and return to your local machine’s command prompt, just type:
bashCopy code
exit
These are just some basic commands to get you started. The Linux command line is very powerful, and there are many more commands and options to explore.

lsb_release -a

When you run the command lsb_release -a inside the Ubuntu container, it should return information about the Ubuntu distribution running in the Docker container.
Here's a breakdown of what this command does:
lsb_release: This command is used to provide certain LSB (Linux Standard Base) and distribution-specific information.
-a: This option stands for "all," and it shows all the available information including the distribution ID, description, release number, and codename.
For example, the output might look something like this:
plaintextCopy code
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.2 LTS
Release: 20.04
Codename: focal

This output indicates that the Docker container is running Ubuntu 20.04.2 LTS with the codename "focal."
If the lsb_release command is not found, it means that the lsb-release package is not installed in the Docker container by default. You can install it by running:
bashCopy code
apt update
apt install -y lsb-release

After installing, you can run the lsb_release -a command again to view the distribution information.

error: root@7330cbb1e3c5:/# lsb_release -a
bash: lsb_release: command not found
root@7330cbb1e3c5:/#
ChatGPT
The error message bash: lsb_release: command not found means that the lsb_release command is not available in the container by default. You can install the lsb-release package to use this command.
Here are the steps to install lsb-release in the container:
Update the package list:
First, run the following command to update the list of available packages:
bashCopy code
apt update
Install the lsb-release package:
Next, run the following command to install the lsb-release package:
bashCopy code
apt install -y lsb-release
The -y flag is used to automatically answer yes to the prompts (for example, confirming that you want to use additional disk space).
Run lsb_release -a again:
After the installation is complete, try running the lsb_release -a command again:
bashCopy code
lsb_release -a
This should now work and display the LSB and distribution information.

root@7330cbb1e3c5:/# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
root@7330cbb1e3c5:/# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
root@7330cbb1e3c5:/#
ChatGPT
You have successfully executed the lsb_release -a command, and as per the output, your Docker container is running Ubuntu 22.04.3 LTS with the codename "jammy."
You can now proceed to use other Linux commands and perform operations as per your requirements within this Ubuntu environment.

Student Lab Workbook: Exploring Dockerized Ubuntu

Lab Objective:

This lab is designed to provide hands-on activities for students to explore and learn about Ubuntu running within a Docker container. This is a basic introduction and the activities are fundamental to understanding how to interact with a Dockerized Linux system.

Prerequisites:

Docker installed on your system
Basic understanding of Docker commands
A Docker container running Ubuntu

Activity List:

1. Exploring Docker Commands:

Use docker ps to list running containers.
Use docker images to view available images on your system.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.