Docker learning: the specific use of Container containers

Docker learning: the specific use of Container containers

Container is another core concept of Docker.

Simply put, a container is one or a group of applications that run independently, and their operating environment. Correspondingly, a virtual machine can be understood as a complete set of simulated operating systems (providing a running environment and other system environments) and the applications running on it.

The main operations on containers are:

  • create
  • start up
  • stop
  • Import and Export
  • delete
  • etc.

Start the container

There are two ways. One is to create a new container based on the image and start it. 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 command required is mainly docker run

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

The following command starts a bash terminal, allowing user interaction, such as:

docker run -t -i ubuntu:18.04 /bin/bash

The -t option lets Docker allocate a pseudo-tty and bind it to the container's standard input, while -i keeps the container's standard input open.

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

  1. Check whether the specified image exists locally. If not, download it from the public repository and start it.
  2. Create and start a container using an image
  3. Allocate a file system and mount a read-write layer outside the read-only image layer
  4. Bridge a virtual interface from the host host's configured bridge interface to the container
  5. Configure an IP address from the address pool for the container
  6. Executes a user-specified application
  7. After execution, the container is terminated

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

Start a terminated container

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

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

It can be seen that only the specified bash application is running in the container. This feature makes Docker's resource utilization extremely high, and it is truly lightweight virtualization.

Background operation

More often, we will run the container in the background, and we can add the -d parameter to achieve this. The following prints hello world every 1 second.

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


If you use the -d parameter to run the container, it will run 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. After starting with the -d parameter, a unique id will be returned. You can also view the container information through the docker container ls command.

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

docker container logs [container ID or NAMES]
hello world
hello world
hello world
. . .

Terminating the container

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

Containers in terminated state can be seen using the docker container ls -a command. For example:

A terminated container can be restarted using the docker container start command.

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

Entering the container

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

Sometimes you need to enter the container to perform operations, including using the docker attach command or the docker exec command. It is recommended that you use the docker exec command for the reasons explained below.

attach command

Note: If you exit from this stdin, the container will stop.

exec command

docker exec can be followed by multiple parameters, here we mainly explain the -i -t parameters.

When only the -i parameter is used, since no pseudo terminal is allocated, the interface does not have the familiar Linux command prompt, but the command execution results can still be returned.

When the -i -t parameters are used together, we can see the familiar Linux command prompt.

If you exit from this stdin, the container will not be stopped. This is why it is recommended that you use docker exec.

Exporting and importing containers

Export Container

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

This will export the container snapshot to a local file.

Importing a container

You can use docker import to import a container snapshot file as an image, for example:

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

Deleting a container

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

Note: Deleting a container requires stopping the container

Clean up all terminated containers

Use the docker container ls -a command to view all containers that have been created, including those in 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 containers in the terminated state.

$ docker container prune

This operation is quite dangerous, use with caution. The above are common operations about Docker containers.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Goodbye Docker: How to Transform to Containerd in 5 Minutes
  • Docker dynamically exposes ports to containers
  • How to enter and exit the Docker container
  • Detailed explanation on how to get the IP address of a docker container
  • Detailed explanation of docker dynamically mapping running container ports
  • Docker removes abnormal container operations

<<:  Tutorial on resetting the root password of Mac MySQL

>>:  WeChat applet implements user login module server construction

Recommend

How to install and use Server-U 14 version

Introducing Server-U software Server-U is a very ...

Detailed explanation of the solution to font blur when using transform in CSS3

This question is very strange, so I will go strai...

How to use ElementUI pagination component Pagination in Vue

The use of ElementUI paging component Pagination ...

Introduction to TypeScript basic types

Table of contents 1. Basic types 2. Object Type 2...

Let’s take a look at JavaScript precompilation (summary)

JS running trilogy js running code is divided int...

js+css to realize three-level navigation menu

This article example shares the specific code of ...

Introduction to the use of MySQL source command

Table of contents Thoughts triggered by an online...

Docker installation and configuration steps for MySQL

Table of contents Preface environment Install Cre...

Problems installing TensorRT in docker container

Uninstall the installed version on Ubuntu: sudo a...

Introduction to reactive function toRef function ref function in Vue3

Table of contents Reactive Function usage: toRef ...

Native JS realizes compound motion of various motions

This article shares with you a compound motion im...

MySQL variable principles and application examples

In the MySQL documentation, MySQL variables can b...

mysql code to implement sequence function

MySQL implements sequence function 1. Create a se...