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
megaphone

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.
megaphone

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.

2. Accessing the Ubuntu Container:

Use docker exec -it <container_id> bash to access the running Ubuntu container.

3. Navigating the File System:

Use cd, ls, and pwd to navigate through the file system in the Ubuntu container.

4. Creating and Editing Files:

Use touch to create a file and nano or vi to edit it.

5. Viewing File Content:

Use cat, less, or more to view the contents of a file.

6. Managing Directories:

Create, list, and remove directories using mkdir, ls, and rmdir.

7. Managing Processes:

Use ps, top, and htop to view running processes.
Learn to kill a process using kill or pkill.

8. Managing Users:

Use adduser to create a new user and su to switch users.

9. Managing Groups:

Use addgroup to create a new group and usermod to add a user to a group.

10. Managing Permissions:

Use chmod and chown to change file permissions and ownership.

11. Installing Software:

Use apt update and apt install to install new software in the container.

12. Exploring Network Configuration:

Use ifconfig or ip a to view network configuration.

13. Using grep Command:

Use grep to search for specific text in files.

14. Redirection and Piping:

Learn to redirect output using > and use piping | with commands.

15. Scheduling Tasks:

Use cron or at to schedule tasks to run at specific times.

16. Compressing and Decompressing Files:

Use tar, gzip, and bzip2 to compress and decompress files.

17. Exploring Environment Variables:

Use env, export, and unset to manage environment variables.

18. Writing and Executing Shell Scripts:

Write a basic shell script and execute it using bash <script_name>.

19. Exploring System Logs:

Use dmesg and tail to explore system logs in /var/log.

20. Exiting and Stopping Containers:

Learn to exit the container shell using exit and stop the container using docker stop <container_id>.

Note:

Ensure to follow best practices for working within a Linux environment.
Be cautious while executing commands, especially commands related to system configurations and file permissions.
Refer to the man pages (man <command>) in Ubuntu for detailed information and options for each command used in the activities above.

Conclusion:

This workbook provides a practical introduction to interacting with a Dockerized Ubuntu system. It covers fundamental activities that form the basis for more advanced tasks and operations in Linux. Students are encouraged to explore further and practice additional commands and operations beyond these activities to enhance their Linux skills.


Lecture Notes: Dockerized Ubuntu in DevOps

I. Introduction:

Objective: To understand the importance and applications of Dockerized Ubuntu in a DevOps environment.
What is Dockerized Ubuntu?: Ubuntu operating system running inside a Docker container.
Why Use Dockerized Ubuntu?: It enables a lightweight, portable, and consistent environment for developing, testing, and deploying applications.

II. What is Docker?

Docker is a platform for automating the deployment, scaling, and management of applications by using containerization.
Containers: Lightweight, standalone, and executable software packages that include everything needed to run a piece of software, including the code, runtime, libraries, and system tools.

III. Benefits of Using Dockerized Ubuntu:

1. Environment Consistency:

Ensures that the application runs the same way in development, testing, and production by maintaining a consistent environment.

2. Portability:

Docker containers can run anywhere – on a developer’s local machine, on physical or virtual machines in a data center, or in the cloud.

3. Isolation:

Docker containers isolate applications from each other and from the underlying system, enhancing security.

4. Version Control:

Docker images can be versioned, allowing easy rollback to previous versions and ensuring application consistency and reliability.

5. Resource Efficiency:

Docker containers share the host system’s OS kernel, rather than including their own OS, reducing overhead and improving performance.

IV. Applications of Dockerized Ubuntu in DevOps:

1. Continuous Integration/Continuous Deployment (CI/CD) Pipelines:

Dockerized Ubuntu can be used to create consistent and reliable CI/CD pipelines, automating the process from code commit to production deployment.

2. Microservices:

Dockerized Ubuntu is ideal for developing and deploying microservices, allowing each service to run in its own container, independently of the others.

3. Testing:

Quickly set up and tear down test environments, ensuring that tests are run in a consistent environment.

4. Deployment:

Simplify deployment by packaging the application and all its dependencies in a Docker container.

5. Scalability:

Easily scale applications by running multiple Docker containers across a cluster of host machines.

6. Development:

Developers can work in the same environment where the application will run, reducing the “it works on my machine” problem.

V. Conclusion:

In their new role as DevOps specialists, students will find Dockerized Ubuntu an invaluable tool for developing, testing, and deploying applications. The ability to create consistent environments from development to production reduces bugs and errors, increases development speed, and ultimately leads to faster, more reliable delivery of applications.
Note:
Encourage students to experiment with Dockerized Ubuntu to familiarize themselves with the Docker commands and concepts.
Emphasize the importance of understanding both Docker and Ubuntu for effective use in a DevOps context.

A basic course outline that covers fundamental Linux concepts and commands in a three-hour class for complete beginners.

Introduction to Linux - Course Outline

I. Introduction (30 mins)

Brief Overview of Linux
History and Development of Linux
Different Linux Distributions (Ubuntu, Fedora, CentOS, etc.)
Advantages of Using Linux
Overview of the Linux Filesystem

II. Getting Started with Linux (30 mins)

Exploring the Linux Desktop Environment (DE)
Overview of Linux Terminal
Basic Linux Terminology
Explanation of Root and Sudo

III. Basic Linux Commands (60 mins)

Opening the Terminal
pwd – Print Working Directory
cd – Change Directory
ls – List Files and Directories
touch – Create a New Empty File
mkdir – Make Directory
cp – Copy Files or Directories
mv – Move Files or Directories
rm – Remove Files or Directories
cat – Concatenate and Display File Content
man – Display Manual Pages

IV. File and Directory Permissions (15 mins)

Explanation of Linux File Permissions
chmod – Change File Permissions
chown – Change File Ownership

V. Installing Software (20 mins)

apt – Advanced Package Tool (For Debian-based distributions)
Installation, Update, and Removal of Software Packages

VI. Basic Text Editing (15 mins)

Introduction to nano Text Editor
Creating, Editing, and Saving Files using nano

VII. Conclusion (10 mins)

Review of Covered Material
Additional Resources for Learning Linux
Q&A Session

VIII. Hands-On Exercise (Optional) (20 mins)

Navigating the Filesystem using Terminal
Creating, Editing, and Moving Files and Directories
Practice with Installing a Software
Editing File Permissions

Notes:

Practical Demonstrations:
Ensure to provide live demonstrations of the commands and operations to help students understand better.
Hands-On Activities:
Incorporate hands-on activities for students to practice the commands and operations.
Provide Additional Resources:
Share additional resources, such as websites, books, and online courses, for students to learn more about Linux after the class.
Adjust Time Allocations:
Feel free to adjust the time allocated to each section based on the complexity of the topic and the pace of your students.
This basic course outline should provide a good start for beginners to get acquainted with Linux. Tailor the course content as per the specific requirements and the learning pace of your students.

* Overview of the Linux Filesystem: provide a lecture lab work book with detailed instructions on linux commands and a learning scenario to complete

Overview of the Linux Filesystem: Lecture Lab Workbook

Welcome to the lab workbook of the Linux Filesystem. In this workbook, we will walk you through some fundamental Linux commands and a learning scenario which will help you in understanding the Linux Filesystem. This hands-on exercise is aimed at familiarizing you with navigating through the filesystem using various Linux commands.

Section 1: Introduction to Linux Filesystem

Root Directory (/):
The topmost directory in Linux.
All other directories are contained within this directory.
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.