Chapter 2 Docker and command line

Posted by Randy on Sun, 23 Jan 2022 20:30:01 +0100

Docker and operating system

Linux distribution

Common are Ubuntu, Alpine, CentOS, Debian, etc. You can download the Linux distribution image with the following command:

docker pull ubuntu
docker pull alpine
docker pull centos
docker pull debian

Using docker images, you can view the image information and compare the image sizes of different distributions through the information.

As can be seen from the above figure, the largest footprint is 231MB centos and the smallest footprint is 5.59MB alpine.

Container interaction mode

Linux distributions do not have a GUI. bash and sh are generally provided. bash is an enhanced version of the shell.

There are two ways to access the Ubuntu command line:

  • docker run
  • docker exec

The complete command is:

docker run -it --rm --name tke-ubuntu ubuntu

Parameter Description:

  • -i is an interactive mode operation container
  • -t is the allocation pseudo terminal
  • -rm is used to automatically clean up the file system inside the container when the container exits
  • -Name is a name for the container

The command line in front of the cursor can be used to determine whether it is the Docker container terminal pseudo terminal or the command line terminal of the local computer.

In the command line window of the local computer, enter docker ps to view the normal container information.

In the pseudo terminal, enter exit and press enter to exit the Docker pseudo terminal, and the container will be deleted.

You can also create a terminal by using docker exec, but you need a pre operation, that is, start the container with the previous docker run, run the image, and then create a new terminal on the local computer. Enter the command:

docker exec -it tke-ubuntu /bin/bash

In this way, you can successfully create a running container terminal.

I misunderstood it before and thought it was to enter the exec command in the interactive mode of running the container.

Linux file structure

ls -a # View all files and folders in the current directory

Linux common commands

Basic command

ls         #Displays non hidden files and folders in the current directory   
ls -a      #Display all files and folders in the current directory (including hidden)
ls --help  #Use heLp to learn more about the command flag and its meaning
ls /bin    #Displays all files and folders in the bin folder in the current directory
cd bin     #Enter the bin folder
pwd        #Displays the absolute path of the current terminal
cd ..      #Enter the parent directory of the current directory
cd ./var   #Enter the var directory, here Indicates the current directory   

Folder operation

mkdir tke          #Create a folder named tke
mkdir tke/dir1     #Create a folder named dir1 in the tke folder
mkdir tke2 tke3    #Create two folders named tke2 and tke3
ls                 #Displays whether a folder has been created
ls tke             #Displays whether a dir1 folder has been created in the tke folder
rmdir tke2 tke3    #Delete the folder. Because tke2 and tke3 are empty files, the TKE folder cannot be deleted
rm -r tke          #remove folders

It should be noted that rmdir cannot delete non empty folders. To delete a non empty folder, use the rm -r command.

Command memory:

  • mkdir is the abbreviation of make directory, which means to create a directory
  • rmdir is the abbreviation of remove directory, which means to delete a directory

File operation

touch test.txt         #Create a blank txt file
cat test.txt           #Check the contents of the txt file and find that it is blank
cat > test.txt         #Enter the editing mode, enter the content, press Ctrl+D to save and exit
touch hello            #Create a blank file named hello
rm hello               #Delete the file named hello
​

Note that cat > test Txt command, after entering the content, be sure to enter first, and then press Ctrl+D, otherwise Ctrl+D will fail. In addition, each time cat > test Txt will empty the previous content and need to add content again.

In addition to the above basic usage, cat has more complex usage:

touch test2.txt             #New test2 txt
cat test.txt > test2.txt    #Text Txt to test2 txt,
cat test2.txt               #View test2 Text content of TXT file
cat test.txt > test3.txt    #When test3.0 does not exist Txt will create this file
cat test.txt >> test3.txt   #Test Txt is appended to test3 Txt file
cat test.txt >> test3.txt 
cat test3.txt               #In test3 Txt and you can see test Txt

>The operator is to empty and copy, and the > > operator is to retain the content and append it later.

Linux Basics

Executable file

echo $PATH                   #Understand the directory of environment variable settings. Linux is separated by colon:
which ls                     #Understand the path where the ls command is located
which pwd rmdir mkdir cp cd  #Know the path of other commands
which docker                 #Return empty
ls /bin                      #View the executable files, mainly the executable files of prerequisites and applications
ls /sbin                     #View the executable files, which are mainly used for system management and network management

Windows is separated by semicolons, and the command is echo% path%.

Basic information command

uptime   #Obtain the host running time and query the linux system load
top      #Continuously view the running process status of the current system, which is somewhat similar to the task manager of windows. To exit the view window, press "Ctrl+C"
ps       #View the running processes of the current system, often using the combination of PS and ef
free     #Used to display memory status
uname -a #Understand the version information of the operating system
whoami   #Displays the current user name
env      #Display system environment variables

Enter the date command, and you will find that the Linux time is 8 hours later than the UTC+8 time zone.

Linux application management

Modify the package image to Tencent cloud

Use the cp command to back up sources List file, rename to sources_bak.list

Then use the cat command to override sources Contents in the list:

# cat /etc/apt/sources.list can get the contents inside and back up the files. The original contents here can also be found on the Internet
cat > /etc/apt/sources.list     
# cat >> /etc/apt/sources. List can also use cat > > to append image resources

Copy the content and paste it into the shell:

deb http://mirrors.cloud.tencent.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.cloud.tencent.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.cloud.tencent.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.cloud.tencent.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.cloud.tencent.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.cloud.tencent.com/ubuntu/ focal-updates main restricted universe multiverse

If the warning can be ignored, select Paste still. Press enter and Ctrl+D to save successfully.

apt update   #Update the package list file to ensure that the latest package is downloaded. You can also use apt get update
apt --help      #Find out what other flag s the apt package manager has
apt list        #Find out what apt packages are

apt installation software

apt install vim        #vim is a very important and general editor for Linux
apt install wget       #Install the download file tool wget
apt install curl       #Install file transfer tool curl
apt install -y git     #Install git tools,
apt install -y unzip      #Install the unzip tool to unzip the zip package

tips: Do you want to continue? [Y/n], if you don't want to see it every time, enter the - Y parameter in the command, you can select confirm by default, skip this step and install directly. The installed software can be accessed through ls/ Bin view the corresponding command.

Try the tool you just installed:

cd /     #Switch to the following directory
mkdir temp
cd temp
wget https://hackweek-1251009918.cos.ap-shanghai.myqcloud.com/tke/d/getting-started-master.zip
unzip getting-started-master.zip

It should be noted that the unzip decompression operation is to unzip to the current folder by default, which usually disturbs the original directory structure. As shown below, you can only manually delete the extracted files one by one and unzip them to the specified folder.

To unzip to the specified directory and create a folder, you can use the following command:

unzip -d /temp getting-started-master.zip

reference resources: Unzip: unzip unzip the file to the specified directory - night Traveler - blog Park

curl can also download files and rename them:

curl -o tke-start.zip https://hackweek-1251009918.cos.ap-shanghai.myqcloud.com/tke/d/getting-started-master.zip

It can also be used to test the website and return the html page structure:

curl -L https://baidu.com

apt can also install the development environment of programming language, such as python, nodejs, etc.

apt install python3    #Install Python's compilation environment, python3

After entering the interactive mode of python, you can use Python syntax to execute statements and do some operations.

Note that sometimes the input is fast, there are more characters after the python 3 Command, and a single > is in front of the cursor. At this point, you only need to enter again to enter the python interaction mode of > > >.

summary

Today, I mainly learned about Linux and four common distributions. I mainly learned the commands of Ubuntu system, including container interaction mode, viewing directories, folders and files. Later, I installed some common tools and software, and became more proficient in the command line operation of Linux system.

Topics: Python Linux Docker bash