Day 04 Docker container
1. Container Command
docker run 镜像id 新建容器并启动
docker ps 列出所有运行的容器 docker container list
docker rm 容器id 删除指定容器
docker start 容器id #启动容器
docker restart容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
Note: We can only create a container if we have a mirror, Linux, download the centos mirror to learn
docker container
There are various commands
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
.....
2. Create a new container and start
docker run [可选参数] image
or docker container run [可选参数] image
Parameter Description
parameter name | Parameter function |
---|---|
–name=‘Name’ | Set a name for the started containerdocker run --name nginx -d nginx:1.19.5 |
-d | Run in daemon mode, run in background mode |
-it | Run interactively, enter the container to view the content |
-p | Specify the port of the container -p 8080 (host): 8080 (container)docker run -d -p 8099:80 nginx:1.19.5 |
-P (uppercase) | Random designated portdocker run -d -P nginx:1.19.5 |
About -d
Use docker run -d
created in the background and start the name centos
of the vessel, through the docker ps
command does not find the vessel in operation by docker ps -a
find command has stopped running centos
container.
So the doubt arises. It -d
is to ensure that the container is running in the background. Why does my container stop running?
As mentioned earlier, docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
there is a COMMAND
parameter, the container will start execution COMMAND
command, its default value /bin/bash
. That container in the background after a successful start, execute the COMMAND
command directly closed.
Aware of this principle, we can pass in docker run -d
the increase command a run in the process of long-term resident can ensure that the container does not shut down. Like endless loop
docker run -d centos /bin/sh -c "while true;do echo 6666;sleep 1;done"
Print 666 simulation logs circularly in the sh directory
3. Exit the container
[root@97b50fe359e8 /]# exit
exit
[root@hecs-x-medium-2-linux-20201118090009 ~]#
exit #容器直接退出
ctrl +P +Q #容器不停止退出
4. List all running containers
1.docker ps command
List currently running containers
docker ps
------------------------------------------------------------------------
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
faaac215cea0 centos "/bin/bash" 23 seconds ago Up 22 seconds dazzling_borg
docker ps -a # 列出所有的容器包括 已经结束的
docker ps -a|grep nginx
------------------------------------------------------------------------
757de974c587 nginx "/docker-entrypoint.…" 3 hours ago Exited (0) 51 minutes ago nginx01
docker ps -aq # 列出所有容器的 id
docker ps -aq
------------------------------------------------------------------------
faaac215cea0
97b50fe359e8
667d29058e73
18f61570de4a
....
5. Delete the container
docker rm 容器id #删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -rf
docker rm -f $(docker ps -aq) #删除指定的容器
docker ps -a -q|xargs docker rm #删除所有的容器
6. Operations to start and stop the container
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止容器(id)
Note: What we see in use docker ps -a
is actually a real dealcontainerHe has not run since the end of the pin to be destroyed , but isPut asideThe more shelved containers are also very memory-intensive. These shelved containers provide us with Prompt and restart
7. Enter the currently running container
In the process of using the container, we inevitably need to enter the container to troubleshoot problems. Let's introduce the centralized way to enter the container.
1.attach (important)
attach is the first official docker command to enter the container, but there is a problem with this command. When multiple windows use this command to enter the container at the same time, all windows will be displayed simultaneously. If one window is blocked, then other windows can no longer be operated. When all windows exit, the container ends.
2.exec (important)
After attaching, exec is officially launched with a new command to enter the container, which is equivalent to executing a command in the container.
3.nsenter
It needs to be used with docker inspect (in the early days when there was no exec command, it was one of the longest ways in enterprises). Docker was developed in golang language, so it also supports the pattern syntax of go language.
Not used!
[root@instance-gvpb80ao docs]# nsenter --target $( docker inspect -f {
{.State.Pid}}
nginxv1 ) --mount --uts --ipc --net --pid
mesg: ttyname failed: No such device
root@6f99ae8757f7:/#
4.ssh
After excluding the use of the docker attach command to enter the container in the production environment, I believe that the first thing you think of is ssh. Install SSH Server in the image (or container), so that multiple people can enter the container without interfering with each other. I believe everyone does this in the current production environment (without using Docker). However, it is not recommended to use ssh to enter the Docker container after using the Docker container.
the difference
docker exec #Open a new terminal after entering the current container, you can operate in it. (Commonly used)
docker attach # Enter the terminal where the container is executing
8. Import and export containers
Sometimes, it is necessary to migrate containers from one system to another. At this time, you can use Docker's import and export functions, which is also an important feature provided by Docker itself.
The image saved by export can only be imported by import save The image saved by export can only be imported
by load
Export container
Exporting a container refers to exporting a created container to a file, no matter whether the container is running or not, you can use the docker [container] export command. The command format is:
export Export the container as a mirror [export]
docker export [容器ID|容器名称]>[压缩包名称]
docker ps
--------------------------------------------
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
870a4301ce9f centos "/bin/bash" 7 minutes ago Up 7 minutes pedantic_maxwell
docker export 870a4301ce9f>centos1.tar
--------------------------------------------
ll
--------------------------------------------
total 217148
-rw-r--r-- 1 root root 222356480 Dec 1 15:14 centos1.tar
save Export the image as an image compression package (save)
docker save [镜像id|镜像名称]>[压缩包]
parameter:
-o Specifies to save multiple images in a compressed package
docker save -o save_many.tar nginx hello-world centos
Write the name of the package first and then separate it with a space
docker save -o save_many.tar nginx hello-world centos
ll
--------------------------------------------
total 568224
-rw-r--r-- 1 root root 0 Dec 1 15:18 centos1.tar
-rw-r--r-- 1 root root 222366720 Dec 1 15:19 save_centos.tar
-rw------- 1 root root 359482368 Dec 1 15:32 save_many.tar
commit exports the container as an image
docker commit [parameter] [container ID] [new image name]
-a: designated maintainer
-m: introduction
-p: when the container is saved, the container is suspended
Import container
import import package as mirror
docker import [压缩包名称] [镜像名称:Tag]
docker import centos1.tar centos:v1
--------------------------------------------
sha256:2d82ce5bce4aac4143c7718833120855dd134f814cf15571fe114fe2aaeb4a4b
docker images
--------------------------------------------
REPOSITORY TAG IMAGE ID CREATED SIZE
centos v1 2d82ce5bce4a 9 seconds ago 0B
nginx latest bc9a0695f571 6 days ago 133MB
centos latest 0d120b6ccaa8 3 months ago 215MB
hello-world latest bf756fb1ae65 11 months ago 13.3kB
load import one or more images (depending on the compressed package)
docker load <[压缩包名字] == dockerload -i [压缩包名字]
[root@docker ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker ~]# ll
total 568224
-rw-r--r-- 1 root root 0 Dec 1 15:18 centos1.tar
-rw-r--r-- 1 root root 222366720 Dec 1 15:19 save_centos.tar
-rw------- 1 root root 359482368 Dec 1 15:32 save_many.tar
[root@docker ~]# docker load < save_centos.tar
Loaded image: centos:latest
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0d120b6ccaa8 3 months ago 215MB
Compared
What is the difference between save and load and export and import?
1. Save is relatively complete, export only saves the image.
2. The volume saved by save is larger than the volume saved by export (save contains build information or cache files, which is more comprehensive)
3. Import can be renamed, load cannot be renamed
4. Save can save multiple images, export can only save one container.
What are the scenarios for save and export?
1. Generally use save for packaging images, and export for packaging containers.
2. If historical information needs to be constructed, save can be used.
What is the difference between docker export and docker commit?
1. docker export exports compressed packages
2. docker commit exports as mirrors
9. Commonly used other commands
1. Background start command
# 命令 docker run -d 镜像名
➜ ~ docker run -d centos
a8f922c255859622ac45ce3a535b7a0e8253329be4756ed6e32265d2dd2fac6c
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
# 问题docker ps. 发现centos 停止了
# 常见的坑,docker容器使用后台运行,就必须要有要一个前台进程,docker发现没
2. View log
docker logs --help
Options:
--details Show extra details provided to logs
* -f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or
relative (e.g. 42m for 42 minutes)
* --tail string Number of lines to show from the end of the logs
(default "all")
* -t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37)
or relative (e.g. 42m for 42 minutes)
➜ ~ docker run -d centos /bin/sh -c "while true;do echo 6666;sleep 1;done" #模
拟日志
#显示日志
-tf #显示日志信息(一直更新)
--tail number #需要显示日志条数
docker logs -t --tail n 容器id #查看n行日志
docker logs -ft 容器id #跟着日志
3. View process information ps in the container
docker top 容器id
4. View the image metadata
# 命令
docker inspect 容器id
#测试
➜ ~ docker inspect 55321bcae33d
[
{
"Id":
"55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066",
"Created": "2020-05-15T05:22:05.515909071Z",
"Path": "/bin/sh",
...
}
10. Copy the contents of the container to the host
.
Current directory
docker cp 容器id:容器内路径 主机目的路径
docker cp 2ad9bd2e9f03:/home/Test/. /home/.
docker cp 主机文件路径 容器id:容器内部路径
Summary of container instructions
[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-JF1R0H5j-1606915442190)(https://gitee.com/A1L19/PicGO/raw/master/PicGo/image -20201201160011696.png)]