Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Docker common commands

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use Docker common commands". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Docker commands.

What is Docker?

Docker is an open source application container engine.

Docker allows you to package all applications and their origins into a standardized unit of software development.

The Docker container packages the software and all the files it needs to run and install (code, runtime, system tools, system libraries), which ensures that it always runs in the same way no matter what environment it is running in. Just like the Java virtual machine, "write once, run everywhere (Write once,run anywhere)", while Docker is "build once, run everywhere (Build once,run anywhere)".

Docker is a "Docker Containers as a Service as a service" (CaaS) that allows development and IT operations teams to be more agile and controllable in building, releasing, and running applications.

In a nutshell: Docker is an open platform for developers and system administrators to build, publish, and run distributed applications. The platform consists of the Docker engine (a portable, lightweight runtime and packaging tool) and Docker Hub (a cloud service that shares applications and automated workflows). Docker enables applications to be quickly assembled from components and eliminates friction between development, quality assurance, and production environments. This allows the IT department to release faster, and these applications run on laptops, virtual machines in the data center, or any cloud, with the same process and results.

Let's take a look at Docker's Logo again. Obviously, this is a whale, it holds a lot of containers. We can think of the host as the whale and the isolated containers as containers, each containing its own application. This Logo is so vivid!

Advantages of Docker

Lightweight: all containers share the same operating system kernel on the same machine so that they start immediately and use memory more efficiently. Image is built from hierarchical file systems so that they can share public files, making disk usage and Image downloads more efficient.

Open: Docker containers are based on development standards that allow containers to run on mainstream Linux distributions and Microsoft operating systems as all the infrastructure.

Security: containers isolate applications from each other, while the infrastructure provides an additional layer of protection for applications.

The difference between Docker and virtual machine

Containers and virtual machines have similar advantages of resource isolation and allocation, but different architecture methods make containers more portable and efficient.

Architecture of virtual machine

Each virtual machine includes applications, necessary binaries and libraries, and a complete guest operating system (Guest OS). Although they are separated, they share and utilize the host's hardware resources, requiring nearly a dozen GB sizes.

Architecture of the container

The container includes the application and all its dependencies, but shares the kernel with other containers. They run on the host operating system as separate user-space processes. They also do not rely on any particular infrastructure, and Docker containers can run on any computer, any infrastructure, and any cloud.

The container of Docker uses LXC, the management uses namespaces to control and isolate permissions, cgroups to configure resources, and aufs to further improve the resource utilization of the file system, but these technologies are not original by Docker.

LXC

LXC differs from a virtual machine in that it is an operating system-level virtualized environment, not a hardware virtualized environment. They all do the same thing, but LXC is an operating system-level virtualized environment with its own processes and cyberspace, rather than creating a full-fledged virtual machine. As a result, a LXC virtual operating system has minimal resource requirements and takes only a few seconds to boot.

As you can see in the following figure, on the left is the LXC virtual Ubuntu, and the default installation is 11 MB.

The relationship between Docker and Microservices

Microservices (micro services) depends on "infrastructure automation", and Docker is the weapon of "infrastructure automation". It can be said that the popularity of Docker has led to the rise of micro-service architecture to some extent, and the wide application of micro-services has also promoted the prosperity of Docker. It can be said that the two complement each other.

Why use Docker?

Development is more agile: Docker gives developers the freedom to define the environment, create and deploy applications faster and easier, and IT operators respond quickly and flexibly to changes.

More controllable: Docker enables developers to save code from infrastructure to applications, helping IT operators manage a standard, secure, and extensible operating environment.

High portability: Docker allows free choice, from laptops to a team, from private infrastructure to public cloud providers.

In this way, you can focus on developing applications and leave the rest of the tedious tasks to Docker.

How to use Docker

This is really a big topic, if a complete exposition is enough to write a book. Fortunately, our goal is to get started, so let's briefly talk about the installation, basic use, and common commands of Docker.

Docker installation

It is recommended that the installation of Docker,window environment under linux environment is complex and error-prone, so it is convenient to use Centos7+yum to install Docker environment.

The Docker package is already included in the default CentOS-Extras software source. So to install docker, simply run the following yum command:

Yum install docker

After the installation is complete, use the following command to start the docker service and set it to boot:

Service docker start

Chkconfig docker on

LCTT translation note: the old sysv syntax is used here, such as the new systemd syntax supported in CentOS 7, as follows:

Service docker start

Chkconfig docker on

test

Docker version

Enter the above command and return the information about the version of docker, which proves that docker is installed successfully.

Hello World

Next, let's take a look at Docker through the simplest image file "hello world".

Because the official warehouse connected to Docker in China is very slow, we use Docker China Accelerator in our daily use. Through the acceleration of Docker official images, users in China can quickly access the most popular Docker images. The image is hosted on Chinese mainland, and local users will now enjoy faster download speeds and greater stability, allowing them to develop and deliver Docker-based applications more agile.

Docker China's official image acceleration can be accessed through registry.docker-cn.com. The mirror library contains only popular public images, and private images still need to be pulled from the US mirror library.

Just modify the configuration file corresponding to docker in the system, as follows:

Vi / etc/docker/daemon.json

# after adding

{

"registry-mirrors": ["https://registry.docker-cn.com"],"

"live-restore": true

}

Run the following command to grab the image file from the repository to your local location.

Docker pull library/hello-world

In the above code, docker image pull is the command to grab the image file. Library/hello-world is the location of the image file in the repository, where library is the group where the image file is located, and hello-world is the name of the image file.

After the crawl is successful, you can see the image file locally.

Docker images

# display the results

REPOSITORY TAG IMAGE ID CREATED SIZE

Docker.io/hello-world latest f2a91732366c 3 months ago 1.848 kB

Now, run the image file.

Docker run hello-world

# display the results

Hello from Docker!

This message shows that your installation appears to be working correctly.

...

After you print this prompt, the hello world stops running and the container terminates automatically. Some containers do not terminate automatically because they provide services, such as Mysql images.

Common command

In addition to the Docker command we used above, Docker has some other commonly used commands.

Pull docker image

Docker pull image_name

View the image on the host. The Docker image is stored in the / var/lib/docker directory:

Docker images

Delete Mirror

Docker rmi docker.io/tomcat:7.0.77-jre7 or docker rmi b39c68b7af30

See which containers are currently running

Docker ps

View all containers

Docker ps-a

Start, stop, restart container commands:

Docker start container_name/container_id

Docker stop container_name/container_id

Docker restart container_name/container_id

After launching a container in the background, if you want to enter the container, you can use the attach command:

Docker attach container_name/container_id

Delete Container Command

Docker rm container_name/container_id

View current system Docker information

Docker info

Download an image from Docker hub:

Docker pull centos:latest

Docker pull centos:latest

Executing docker pull centos will download all the images under the Centos repository to the local repository.

At this point, I believe you have a deeper understanding of "how to use Docker commands". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report