container command
6. Container command
6.1 Create and start a container (start an interactive container)
docker run [OPTIONS] image [COMMAND][ARG...] //新建并启动容器
ORTIONS description (commonly used): some have a minus sign and some have two minus signs
parameter Function - -name New name for container: Specify a new name for the container -d Run the container in the background and return the container ID, that is, start the daemon container -i Run the container in interactive mode, usually with -t -t Reassign a pseudo-input terminal for the container, usually used with -i -P random port mapping -p Specify port mapping There are four forms of specified port mapping
- ip:hostPort:containerPort
- ip::containerPort
- hostPort:containerPort
- containerPort
6.2 List all currently running containers
docker ps [OPTIONS] //列出当前所有正在运行的所有容器
OPTIONS description (commonly used):
parameter Function -a List all currently running containers + historically run containers -l Show recently created containers -n Show the last n created containers -q Silent mode, only display the container number, only return the currently running container ID - -no-trunc don't truncate output
6.3 Exit the container (two ways)
6.3.1 Container stops and exits
exit //容器停止退出
docker ps View running containers
6.3.2 Exit directly (without closing)
ctrl + p + q //容器退出但不关闭
Prove that the container was exited but not closed
6.4 Start the container
6.4.1 Start the container
docker start 容器ID //启动上述退出的容器
6.4.2 Restart the container
docker restart + 容器ID //重新容器【运行时间开始重新计时】
6.5 Stop container (both forms)
6.5.1 Stop the container (gentle) [like pressing the power switch of a refrigerator]
docker stop + 容器ID/容器名称 //停止当前正在运行的容器
6.5.2 Forcibly closing the container [like turning off the refrigerator directly]
docker kill + 容器ID/容器名称 // 强制关闭当前正在运行的容器
6.6 Deletestoppedcontainer
6.6.1 Removing a single stopped container
docker rm + 容器ID //删除单个已停止的容器
If the current container is running
usedocker rm -f + 容器ID //强制删除正在运行的容器
6.6.2 Delete multiple containers at once
docker rm -f $(docker ps -aq) //批量删除
docker ps -aq | xargs docker rm //效果与上面的命令一致,不再赘述验证
7. Container command (important)
7.1 Start the daemon container
docker run -d + 容器名 //启动守护式容器
Use docker run -d centos
problem: Then docker ps -a to check, found that the container has exited
Key points: Docker container running in the background, there must be a foreground process. If the commands run by the container are not those that have been suspended (such as running top and tail), they will automatically exit.
This is a problem with the docker operating mechanism. For example, your web container, let's take ngnix as an example. Under normal circumstances, we only need to start the corresponding service to configure the startup service. Example: service nginx start, but if nginx runs in background process mode, there will be no applications running in the docker foreground. After such a container starts in the background, it will immediately commit suicide because he feels that he has nothing to do , so it is the best The solution is to run the program you are running as a foreground process
7.2 View container logs
docker logs -f -t --tail + 容器ID //查看容器的运行日志
parameter | Function |
---|---|
-t | add timestamp |
-f | Follow the latest log print |
–tail | The number shows how many records were last |
7.3 View the process running in the container
-docker top + 容器ID //查看当前容器中运行的进程
7.4 Check the internal details of the container
docker inspect + 容器ID //查看容器内部细节
7.5 Enter a running container and interact with commands
docker attach + 容器ID //直接进入正在运行的容器内部
Directly enter the terminal of the container start command, and will not start a new process
-docker exec -t + 容器ID //进入正在运行的容器内部查看内容,没有真正意义上的进入内部
Open a new terminal in the container and start a new process
the difference:
- After the attach command enters the container, exit exiting the container will cause the container to stop running;
after the exec command enters the container, exit exits the container, and the container will not stop- attach directly enters the terminal of the container startup command and does not start a new process
exec opens a new terminal in the container and can start a new process
7.6 Copy files from the container to the host
docker cp + 容器ID:容器内路径 目的主机路径 //将容器内的文件拷贝到宿主机上
Attached: