Previous review:

  • First Docker and install | Docker series
  • Use the Docker mirror | Docker series

Now that you have the basics of an image, let’s start creating containers. Docker containers are very lightweight and can be created and deleted at any time, which is very convenient.

Create and start the container

Use the docker run command to create and start a container:

$ docker run -it centos /bin/echo 'hello world'
hello world
Copy the code

The -t argument causes Docker to assign a dummy terminal and bind it to the container’s standard input, and the -i argument keeps the container’s standard input open.

$ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES DF84684C3888 centos "/bin/echo 'hello wo..." 10 seconds ago Exited (0) 7 seconds ago admiring_noyceCopy the code

The docker ps command lists the current container, as you can see, the container just executed after output information directly exit. Is there a way to have the container execute in the background? The answer, of course, is yes.

Use the -d argument to make the container run in the background, as a daemon, which is most commonly used in work.

$ docker run -d centos /bin/sh -c "while true; do echo hello world; sleep 1; done"
fcb07a324388d58883e212ff5675ad7947a22c731f677d1a37ff7bc3d8bfa9a7

$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES FCB07A324388 centos "/bin/sh -c 'while t..." 6 seconds ago Up 5 seconds beautiful_northcuttCopy the code

The container then runs in the background.

There is also a way to use the docker create command to create the container, and then use the docker start command to start the container, two commands equivalent to a docker run, this way is not commonly used, I will not do more introduction.

Let’s talk about the story behind Docker run. What does Docker do for us behind this command?

  • Check whether the specified image exists locally. If not, download it from the public repository.
  • Create and start a container with an image;
  • Allocate a file system and mount a read-write layer outside the read-only mirror layer.
  • Bridge a virtual interface from the host’s configured bridge interface to the container.
  • Configure an IP address from the address pool to the container.
  • Execute user-specified commands or applications;
  • The container is terminated after execution.

Into the container

When the container executes in the background, use the Docker exec command to enter the container.

$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES FCB07A324388 centos "/bin/sh -c 'while t..." 6 seconds ago Up 5 seconds beautiful_northcutt
$ docker exec -it fcb07a324388 /bin/bash
[root@fcb07a324388 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@fcb07a324388 /]#
Copy the code

After entering the container, it is a Linux system, and the container also supports almost all the commands that Linux supports.

This command is important. Sometimes we need to go into the container to see what is going on if we can’t see the problem through logs alone.

To exit, just use the exit command.

Another command is Docker attach, but it has a disadvantage. When multiple Windows are attached to the same container at the same time, all Windows are displayed synchronously. When one window is blocked by a command, other Windows cannot perform operations.

As a result, this command is now mostly not used.

Stop the container

Use the Docker stop command to stop a container.

$ docker stop fcb
fcb
$ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES FCB07A324388 centos "/bin/sh -c 'while t..." 23 minutes ago Exited (137) 4 seconds ago beautiful_northcuttCopy the code

If you want to start again, use Docker start.

Check the container

As described above, use the docker ps command to view the current container.

Use Docker logs to view the container log output.

Use the docker container Inspect command to view the details of the container.

Run the docker top command to view the process information in the container, similar to the top command in Linux.

Use the Docker stats command to view container CPU, memory, storage, etc.

Use docker Container Port to view port mappings of containers.

Import and export containers

Export a container to a file using the docker export command.

$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES FCB07A324388 centos "/bin/sh -c 'while t..." 34 minutes ago Up 9 minutes beautiful_northcutt$ docker export -o centos.tar fcb
$ ls
centos.tar
Copy the code

Use the docker import command to import container files into the local image.

$ docker import centos.tar test/centos
sha256:f994c062dae063ffb8c97191d951b9beaac73d99023120191dbbc9741d725578
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/centos         latest              f994c062dae0        6 seconds ago       237MB
Copy the code

In the previous article, we introduced a docker load command that also imports files into a local image. What is the difference between the two?

Container snapshot files discard all history and metadata information, while mirror storage files hold the full record but are larger.

Remove the container

Use the docker rm command to delete the stopped container. If the container is running, you can add the -f parameter to forcibly delete it.

Reference Documents:

Introduction and Actual Combat of Docker Technology

www.dockerinfo.net/docker%e5%a…

Past highlights:

  • First Docker and install | Docker series
  • Use the Docker mirror | Docker series

Tech Blog:

Github.com/yongxinz/te…

Meanwhile, welcome to follow my wechat official account AlwaysBeta, more exciting content waiting for you.