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 Container Container in Docker

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

Share

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

How is the Container container used in Docker? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

A container is an application or a group of applications that run independently and their running environment. Correspondingly, a virtual machine can be understood as a complete set of operating systems that simulate running (providing a running environment and other system environments) and applications running on it.

The main operations about the container are:

Create

Start

Stop it

Import and export

Delete

Wait

Start the container

There are two ways, one is to create a new container based on the image and start it, and the other is to restart the container in the stopped state.

Because Docker containers are so lightweight, users often delete and create new containers at any time.

Create and start

The main command required is docker run.

This is almost indistinguishable from executing / bin/echo 'hello world' directly locally.

The following command starts a bash terminal that allows the user to interact, such as:

Docker run-t-I ubuntu:18.04 / bin/bash

The-t option lets Docker assign a pseudo terminal (pseudo-tty) and bind it to the standard input of the container, and-I keeps the standard input of the container open.

So what happens when we use the docker run command to create and start the container?

Check whether the specified image exists locally. Download it from the public warehouse if it does not exist.

Create and start a container using an image

Assign a file system and mount a read-write layer outside the read-only mirror layer

Bridge a virtual interface to the container from the bridge interface configured by the host host

Configure an ip address from the address pool to the container

Execute a user-specified application

The container is terminated after execution

So when we enter the terminal through bash, we are actually entering another system.

Start the terminated container

You can use the docker container start command to start running a terminated container directly.

The core of the container is the executed application, and the resources required are necessary for the application to run. Besides, there are no other resources. You can use ps or top to view process information in a pseudo terminal.

It can be seen that only the specified bash application is running in the container. This feature makes Docker extremely high utilization of resources, and is a genuine lightweight virtualization.

Background operation

More often, we will run the container in the background, which can be achieved by adding the-d parameter. Here is to print the hello world every 1 second.

Docker run ubuntu:18.04 / bin/sh-c "while true; do echo hello world;sleep 1 done"

If you run the container with the-d parameter, it runs in the background:

Note: whether the container will run for a long time is related to the command specified by docker run and has nothing to do with the-d parameter. Starting with the-d parameter returns a unique id, or you can view the container information through the docker container ls command.

To get the output of the container, you can use the docker container logs command.

Docker container logs [container ID or NAMES]

Hello world

Hello world

Hello world

. . .

Termination container

You can use docker container stop or docker stop container id to terminate a running container. In addition, the container automatically terminates when the application specified in the Docker container terminates.

The container that terminates the state can be seen with the docker container ls-a command. For example:

Containers in the terminated state can be restarted with the docker container start command.

In addition, the docker container restart command terminates a running container and then restarts it.

Enter the container

When using the-d parameter, the container will enter the background after startup.

In some cases, you need to enter the container to operate, including using the docker attach command or the docker exec command. It is recommended to use the docker exec command. The reasons are described below.

Attach command

Note: if you exit from this stdin, it will cause the container to stop.

Exec command

Docker exec can be followed by multiple parameters. Here, the-I-t parameter is mainly described.

When using only the-I parameter, because there is no pseudo terminal assigned, the interface does not have the familiar Linux command prompt, but the command execution result can still be returned.

When the-I-t argument is used together, we can see the familiar Linux command prompt.

If you exit from this stdin, it will not cause the container to stop. This is why docker exec is recommended.

Export and import containers

Export Container

If you want to export a local container, use the docker export command.

This snapshots the export container to the local file.

Import Container

You can use docker import to import as a mirror from a container snapshot file, for example:

Cat ubuntu.tar | docker import-test/ubuntu:v1.0

Delete Container

You can use docker container rm or docker rm container id to delete a container that is in a terminated state. For example:

Note: deleting a container requires a stop container.

Clean up all containers in the terminated state

Use the docker container ls-a command to view all the containers that have been created, including the terminated state. If there are too many containers, it may be troublesome to delete them one by one. Use the following command to clean up all the containers in the terminated state.

$docker container prune

After reading the above, have you mastered how the Container container is used in Docker? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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