(Docker notes): Basic commands for container commands

table of Contents

Basic commands for container commands

Create a new container and start

List all running containers docker ps

Exit the container

Delete container docker rm

Start and stop the container


Basic commands for container commands

  • Note: You can create a container only if you have an image. You must download a centos image to test and learn.
docker pull centos 

Create a new container and start

docker run [可选参数] image

# 常用参数说明
--name="NAME"  容器名字 用于区分容器
-d             后台方式运行
-it            使用交互方式运行,要进入容器查看内容
-p             指定容器端口 -p 8080:8080
	-p ip:主机端口:容器端口
	-p 主机端口:容器端口 (常用)
	-p 容器端口
	容器端口    (直接写容器端口也行)
-P(大写的P)              随机指定端口

List all running containers docker ps

可选项:
-a    # 列出当前正在运行的容器 + 历史运行过的容器 就是全部容器
-n=?  # 显示最近创建的容器 ?表示个数
-q    # 只显示容器的编号

Exit the container

exit    # 直接停止容器并退出
ctrl + p + q  # 容器不停止退出

Delete container docker rm

docker rm 容器id                 # 删除指定id的容器,不能删除正在运行的容器 rm -f 强制删除
docker rm -f $(docker ps -aq)    # 删除所有容器
docker ps -a -q|xargs docker rm  # 通过管道删除所有的容器

Start and stop the container

docker start 容器id      # 启动容器
docker restart 容器id    # 重启容器
docker stop 容器id       # 停止容器
docker kill 容器id       # 杀死容器

Guess you like

Origin blog.csdn.net/baidu_41388533/article/details/108536037
Recommended