[Cloud Native|Docker Series Part 4] Introductory Practice of Docker Containers

insert image description here

Welcome to the fourth blog in the Cloud Native series! In the previous two blogs, we have learned the basic concepts and introductory practices of Docker images. This blog will take you in-depth understanding of Docker containers and explore how to use Docker containers to build, run and manage applications. Whether you are a novice or an experienced developer, through the practical guidance of this blog, you will be able to become more familiar with and master the use of Docker containers.

1. Review of the basic concepts of Docker containers

Before we start exploring the practice of Docker containers, let's briefly review the basic concepts of Docker containers. A Docker container is an instance created based on a Docker image, and it is an independent and isolated operating environment. Each container contains everything needed to run an application, including the file system, system tools, libraries, and configuration. The emergence of containerization technology makes the development, testing and deployment of applications more flexible and efficient.

1.1 Features of Docker container

  • Isolation : Docker containers provide strong isolation. Each container is an isolated operating environment and does not affect each other.
  • Lightweight : Compared with traditional virtual machines, Docker containers are lighter and start and stop faster.
  • Portability : Docker containers can run on different hosts and environments, regardless of differences in the underlying operating systems.
  • Easy to manage : Containers can be managed through the command line or the Docker client, such as starting, stopping, restarting, and deleting.

2. Create and start Docker container

The required command is mainly docker run.
For example, the following command outputs a "Hello World" and then terminates the container.

$ docker run ubuntu:18.04 /bin/echo 'Hello world'
# Hello world

insert image description here

3. Run in guard state

More often, it is necessary to let Docker run in the background instead of directly outputting the result of executing the command to the current host. At this point, it can be achieved by adding the -d parameter.
Here are two examples to illustrate.
If you run the container without the -d parameter.

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

insert image description here

The container will print the output (STDOUT) to the host
if the container is run with the -d parameter.

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

At this time, the container will run in the background and will not print the output (STDOUT) to the host (the output can be viewed with docker logs).

docker container logs [container ID or NAMES]

insert image description here

4. Terminate the container

You can use docker container stopto terminate a running container.
In addition, when the application specified in the Docker container terminates, the container is automatically terminated.
For example, for the container that only started one terminal in the previous chapter, when the user exits the terminal through the exit command or Ctrl+d, the created container will terminate immediately.
example:

docker container stop 2d893c6294df604c3fd8f0e25e6975c0328fa6c65dcd763dab162e11dc281cfd

Terminated containers can be seen with the docker container ls -a command. For example

$ docker container ls -a
CONTAINER ID        IMAGE                    COMMAND                CREATED             STATUS                          PORTS               NAMES
ba267838cc1b        ubuntu:18.04             "/bin/bash"            30 minutes ago      Exited (0) About a minute ago                       trusting_newton

insert image description here
In addition, the docker container restart command will terminate a running container and then restart it.

5. Enter the container

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

5.1 attach command

$ docker run -dit ubuntu
243c32535da7d142fb0e6df616a3c3ada0b8ab417937c853a9e1c251f499f550

$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
243c32535da7        ubuntu:latest       "/bin/bash"         18 seconds ago      Up 17 seconds                           nostalgic_hypatia

$ docker attach 243c
root@243c32535da7:/#

5.2 exec command

5.2.1 -i -t parameters

Multiple parameters can be followed by docker exec. 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 result can still be returned.
When the -i -t parameters are used together, you can see the familiar Linux command prompt.

$ docker exec -it 69d1 bash
root@69d137adef7a:/#

You can also come in directly when you just run the container

docker run -it ubuntu:latest /bin/bash

insert image description here

6. View container list

To see a list of running containers, you can use docker psthe command. This command will display information such as container ID, image name, creation time, and running status.

docker ps

insert image description here
If you need to see all containers including stopped ones, you can use docker ps -athe command.
It can also be viewed on the Docker client:
insert image description here

7. Export and import

7.1 Export container

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

$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
7691a813g370e        ubuntu:17.04        "/bin/bash"         21 hours ago        Exited (0) 11 hours ago                       test
$ docker export 7691a813g370e > ubuntu.tar

This will export the container snapshot to a local file.

7.2 Import container snapshot

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

$ cat ubuntu.tar | docker import - dev/ubuntu:v2.0
$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
dev/ubuntu         v2.0                9d37a608a97        About a hour ago   143.3 MB

7.3 Delete container

To delete a stopped container, use docker rmthe command, specifying the ID or name of the container.

docker rm <容器ID或名称>

Summarize

Through the practical guidance of this blog, we have a deep understanding of the creation, operation, interaction, management and data management of Docker containers. Docker containers provide a lightweight, isolated operating environment that makes application development and deployment more flexible and efficient. Whether you are a developer or an operator, by mastering the use of Docker containers, we can better build, run, and manage applications.

I hope this blog has played a good guiding role in your understanding and use of Docker containers. In the next blog, we will further explore more functions and application scenarios of Docker to help you better use cloud native technology to build and deploy applications. Continue to pay attention to our cloud native|Docker series, which will take you into the wonderful world of cloud native!

Guess you like

Origin blog.csdn.net/A_D_H_E_R_E/article/details/131745889