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

Case Analysis of Docker Container usage

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge points of Docker container use case analysis. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Docker and virtual machine

If you are running your software virtually, why do you need docker instead?

The main difference between them is that docker is a separate process running in your native operating system, while the virtual machine is a complete isolated operating system that runs on top of the host operating system and takes more time to load. So docker has more advantages than virtual machines, such as:

The loading speed is different from that of virtual machines and requires very few hardware resources.

Multiple docker containers are running simultaneously on the same operating system.

You can modify the container and deploy it, or provide the docker file definition to a friend to work in the same environment.

In fact, docker is not a substitute for virtual machines, but rather solves specific problems.

Assuming that your application requires three or more services running on different operating systems, you can run three containers smoothly on the same host instead of three virtual machines on the same host. That sounds great!

Run your container

Before you begin, make sure that docker is installed correctly and is ready to accept commands. Type the following command in a new terminal window:

$docker-v

The above command outputs the version of docker installed on pc:

Docker version 17.12.0-ce-rc2, build f9cde63

It's time to start running the container:

$docker container run alpine echo "hello world"

When you run the above command for the first time, you should see output similar to this in the terminal window:

It's easy, isn't it? Try running the same command again:

$docker container run alpine echo "hello world"

For the second, third, or nth time you run the above command, you should only see this output in the terminal:

Hello world

Now that you have successfully run the container, it is time to analyze what happened. View the following commands:

$docker container run alpine echo "hello world"

The command contains multiple parts. First, you have the word "docker". This is the name of the docker command line interface (cli) and is used to interact with the docker engine responsible for running the container.

Next, you have the word "container", which indicates the context you are using.

The next step is the actual command run to be executed.

Now, you also need to tell docker which container to run. In this case, the alpine container is running.

Finally, you need to define the types of processes or tasks that should be executed within the container while the container is running. This is the last part of the command, echo "hello world".

Running a process within a container

Now that you know the various parts of the command that runs the container, try running a different process in another container:

$docker container run centos ping-c 5 127.0.0.1

The output is as follows:

In the previous example, the container image used is centos, and the process executed within the centos container is ping-c 5 127.0.0.1, which loops back to the address ping five times until it stops.

The first line is as follows:

Unable to find image 'centos:latest' locally

This tells you that docker did not find a mirror named centos:latest in the system's local cache. Therefore, docker knows that it must be lifted from some mirror source in the storage container.

By default, the docker environment is configured to extract images from the docker hub of hub.docker.com. This is represented by the second line as follows:

Latest: pulling from library/centos

The next three lines of output are as follows:

85432449fd0f: pull completedigest: sha256:3b1a65e9a05...status:

This tells you that docker has successfully extracted the mirror centos:latest from docker hub.

The subsequent output is generated by processes running in the container, where the ping tool is run.

You may also notice that the keyword latest appears several times. Each image has a version (also known as a tag), and if no version is explicitly specified, docker automatically treats it as the latest version.

If you run this container again on the system, the previous five lines will not be output, because docker will cache the container image locally, so you don't have to download it first. See if that's the case.

Run a random reference container

In order to run the random statement container, you need an algorithm to generate random statements. The api that generated these random statements can be found here [1].

The goal now is to run a process in the container, generate a random statement every 5 seconds, and output it to stdout:

Press ctrl + c to stop the script. This is the output:

Each response is a string in json format, containing quotation marks, author and its category.

Now, let the container run in the background. To do this, you need to reduce the previous script to one line and use / bin/sh-c "…" To execute. The expression for docker is as follows:

$docker container run-d-name quotes alpine\ / bin/sh-c "while:; do wget-qo- https://talaikis.com/api/quotes/random; printf'\ n; sleep 5; done"

In the above expression, you used two command line arguments,-d and-- name. -d tells docker to run the container as a linux daemon. The-name parameter is used to specify an explicit name for the container.

If you do not specify an explicit container name, docker automatically assigns a random but unique name to the container. The name will consist of the name of a famous scientist and an adjective.

Such as "boring_borg" or "angry_goldberg". Quite humorous, isn't it?

An important aspect is that the container name must be unique. Make sure the quote container is up and running:

$docker container ls-l

The important part of the previous output is the status column, which in this case shows up 16 seconds. This means that the container has been up and running for 16 seconds.

List container

As you continue to run containers over time, your system may produce a lot of containers. To find the containers that are currently running on the host, use the container ls command, as follows:

$docker container ls

This lists all currently running containers.

By default, docker outputs seven columns, meaning as follows:

If you want to list all the containers defined on the system, you can use the command line parameter-an or-all, as follows:

$docker container ls-a

This will list containers in any state, whether they are created, run, or exit.

Sometimes, you may just want to list the id of all containers. To do this, you have the-Q parameter:

$docker container ls-Q

You might want to know what this is for. Here's an example:

$docker container rm-f $(docker container ls-a-Q)

The above command deletes all containers currently defined on the system, including stopped containers. The rm command represents deletion, which will be further explained in this tutorial.

In the previous section, you used the-l argument in the list command. Try to use docker to help find out what the-l parameter stands for. You can invoke help for the list command, as follows:

$docker container ls-h

Stop and start the container

Sometimes, you may need to temporarily stop a running container. Try the following container:

$docker container run-d-name quotes alpine\ / bin/sh-c "while:; do wget-qo- https://talaikis.com/api/quotes/random; printf'\ n; sleep 5; done"

Now, you can stop this container using the following command:

$docker container stop quotes

When you try to pause the container, you may notice that it takes a while (about 10 seconds) to complete execution. Why is this? Docker sends linux sigterm signals to the main process running in the container.

In the above command, the name of the container is used to specify the container to stop. You can also use the container id.

How do you get the container id?

There are several ways to do this. The manual method is to list all the running containers and find the container you are looking for in the list. Just copy its id from there.

A more automated approach is to use shell scripts and environment variables. For example, if you want to get the id of the quote container, this is an example:

$export container_id = $(docker container ls | grep quotes | awk'{print $1}')

Here we use awk to get the first field, the container id. Instead of using the container name, you can now use the $container_id variable in the expression:

$docker container stop $container_id

Once the container is stopped, its status changes to "exited".

You can restart the stopped container using the docker container start command.

Remove Container

When you run the docker container ls-a command, you can see many containers in the "exited" state.

If you no longer need these containers, it is best to remove them from memory; otherwise, they will take up valuable resources. The command to delete the container is as follows:

$docker container rm

Alternatively, you can use this command:

$docker container rm

Sometimes you cannot delete a running container; if you want to force deletion, you can use the command line parameter-f or-force.

Containerization has changed the way the industry operates, reducing maintenance costs by more than 50% and reducing time to market by about 90%. In addition, containers make applications more secure than running outside the container.

These are all the contents of the article "Docker Container usage case Analysis". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report