Cloud Native Docker Container Explanation

Preface

Hello, this is a detailed explanation of the Docker container, explaining how to start the container, enter the container, and the commands to operate the container. Hope to help you all~

1. Docker Container - Operating Docker Container

Container is another core concept of Docker.

Simply put, containers are one or a group of applications that run independently and their running environment. Accordingly, a virtual machine can be understood as a set of operating systems (which provide a runtime environment and other system environments) and applications running on it.

1. Start Container

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

2. New and Start Container

The main command required is docker run.

For example, the following command outputs a "Hello World" and terminates the container.

docker run ubuntu:18.04 /bin/echo 'Hello World'
Hello World

This is hardly different from executing / bin/echo'Hello world'locally.

The following command starts a bash terminal that allows the user to interact.

docker run -t -i ubuntu:18.04 /bin/bash
root@af8bae53bdd3:/#

The -t option allows Docker to assign a pseudo-tty to the standard input of the container, while -i keeps the standard input of the container open.

In interactive mode, users can enter commands through the terminal they create, for example

root@af8bae53bdd3:/# pwd
/
root@af8bae53bdd3:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

When using docker run to create containers, the standard actions Docker performs in the background include:

  • Check to see if the specified image exists locally and not downloaded from a common repository
  • Create and start a container with a mirror
  • Assign a file system and hang it outside a read-only mirror layer on a read-write layer
  • Bridge a virtual interface from the host host configured bridge interface to the container
  • Configure an ip address from the address pool to the container
  • Execute user-specified application
  • Container terminated after execution

3. Start Terminated Container

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

The core of the container is the application being executed, and all the resources needed are necessary for the application to run. There are no other resources. ps or top can be used in pseudo-terminals to view process information.

root@af8bae53bdd3:/# ps
  PID TTY          TIME CMD
    1 ?        00:00:00 bash
   11 ?        00:00:00 ps

Visible, only the specified bash application is running in the container. This feature makes Docker extremely efficient in resource utilization and is a lightweight virtualization with authenticity.

4. Daemon operation

More often, you need to have Docker run in the background instead of directly outputting the results of executing the command to the current host. This can be achieved by adding the -d parameter.

Here are two examples.

Run the container without the -d parameter.

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

The container prints the output (STDOUT) to the host machine

If the -d parameter is used to run the container.

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

The container runs in the background and does not print the output (STDOUT) to the host machine (the output can be viewed with docker logs).

Note: Whether the container will run for a long time depends on the command specified by docker run, not on the -d parameter.

Starting with the -d parameter returns a unique id, or you can view container information through the docker container command.

$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                              NAMES
264ff93e690b        ubuntu:18.04        "/bin/sh -c 'while t..."   14 seconds ago      Up 13 seconds                                                          eager_goldberg

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

$ docker container logs [containerId or NAMES]
# For example:
$ docker container logs 264ff93e690b
hello world
hello world
hello world
. . .

5. Terminate container

You can use docker container stop to terminate a running container.

In addition, when the application specified in the Docker container terminates, the container automatically terminates.

For example, if only one terminal container is started and the user exits the terminal with the exit command or Ctrl+d, the container created terminates immediately.

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

$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS                                              NAMES
264ff93e690b        ubuntu:18.04        "/bin/sh -c 'while t..."   2 minutes ago       Up 2 minutes                                                                    eager_goldberg
3c45ebb4c80b        ubuntu:18.04        "/bin/sh -c 'while t..."   2 minutes ago       Up 2 minutes                                                                    practical_gagari

For containers in the terminated state, you can restart them with the docker container start command.

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

6. Enter Containers

When the -d parameter is used, the container will go into the background when it is started.

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

7. attach command

docker attach is the command that comes with Docker. The following example shows how to use this command.

$ docker run -dit ubuntu
77b458b98ecb6b89c7acbfbe83497a6eff6dab9d76bf8c0600c331fccb8f1803

$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
a31d8dd20fa6        ubuntu              "bash"              16 seconds ago      Up 16 seconds                           nostalgic_noether

$ docker attach a31d
root@a31d8dd20fa6:/# 

Executing exit after entering the container will cause the container to stop.

8. exec commands

8.1, -i-t parameters

The docker exec can be followed by several parameters, mainly the -i-t parameter.

When using the -i parameter only, the interface does not have a Linux command prompt that we are familiar with because no pseudo-terminals are assigned, but the results of command execution can still be returned.

When the -i-t parameter is used together, you can see the Linux command prompt, which is familiar to us all.

$ docker run -dit ubuntu
4a31d9ca418520453ff890173731d61953a8c818014c0f8681b6d48362a92250

$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4a31d9ca4185        ubuntu              "bash"              25 seconds ago      Up 24 seconds                           brave_brown

root@ubuntu:~# docker exec -i 4a31 bash
ls
bin
boot
dev
etc
home
lib
lib32
lib64
libx32
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

$ docker exec -it 4a31 /bin/bash
root@4a31d9ca4185:/# 

Executing exit after entering the container will not cause the container to stop. This is why docker exec is recommended.

Use docker exec --help for more parameter descriptions.

By reading this, you must be familiar with how to start, enter and operate containers. Don't forget to support bloggers three times.
I am Micromaple, looking forward to your attention~ 💪💪💪

Tags: Docker Container Cloud Native

Posted by tito on Wed, 20 Jul 2022 05:25:22 +0930