docker的常用命令
帮助命令
docker version // 显示docker的版本信息
docker info // 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help // 帮助命令
帮助文档地址链接: https://docs.docker.com/reference/.
镜像命令
docker images 查看所有本地的主机上的镜像
可选项
-a --al // 列出所有镜像
-q --quiet // 只显示镜像的id
docker search搜索镜像
可选项,通过搜索来过滤
–filter=STARS=3000 //搜索出来的镜像时stars大于3000的
docker pull 下载镜像
docker pull [:tag] //指定版本下载
docker pull mysql //未指定版本则下载最新的
docker rmi 删除
docker -rmi -f 容器id
docker -rmi -f 容器id 容器id 容器id 。。。
docker -rmi -f $(docker images -aq) //删除全部容器
容器命令
说明,我们有了镜像才可以创建容器,linux,下载一个镜像来学习
docker pull centos
新建容器并启动
docker run [可选参数] image
参数说明
–name=“Name” 容器名字,tomcat01, tomcat02,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口
-p ip: 主机端口: 容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
-p 随机制定端口
测试,启动并进入容器
从容器退回主机exit
查看当前正在运行的容器
docker ps 列出当前正在运行的容器
-a 列出当前正在运行的容器+ 带出历史运行过的容器
-n=? 显示最近创建的n个容器
-q 只显示容器的编号
因为已经退出了所以此时并没有正在运行的容器
查看历史运行的容器
退出容器
exit 直接停止容器并退出
Ctrl + P + Q 容器不停止并退出
删除容器
docker rm 容器id 删除指定的容器,不能删除正在运行的容器,若要强制删除,rm -f
docker rm -f $(docker ps -aq) 删除所有的容器
docker ps -a -q|xargs docker rm 删除所有容器
启动和停止容器的操作
docker start 容器id 启动容器
docker restart 容器id 重启容器
docker stop 容器id 停止当前正在运行的容器
docker kill 容器id 强制停止当前容器
常用其他命令
后台启动容器
docker run -d centos
问题docker ps,发现centos停止了,
常见的坑: docker容器后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看容器中进程信息ps
命令 docker top 容器id
**
**
查看容器所有信息
docker inspect 容器id 查看镜像的元数据
进入当前正在运行的容器
我们通常容器都是使用后台方式运行的,需要进入容器修改一些配置
docker exec -it 容器id bashShell
方式二: docker attach 容器id
区别: exec是进入容器后新打开一个终端,而attach是进入正在运行的终端
从容器内拷贝文件到主机
docker cp 容器id:容器内路径 目的主机路径
小结:
attach Attach to a running container
build Build an image from Dcokerfile
commit Create a new image from a container changes
cp Copy a files/folders from the containers filesystem to the host path
create Create a new container
diff Inspect changes to a container’s filesystem
events Get real time events from the Servers
exec
export Stream the contents of a container as a tar archive
history show the history of images
images List images
import Create a new filesystem image from the contents of a tarball
info Display system-wide infomation
inspect
kill
load Load an image from a tar archive
login Register or login to the docker register server
logout Log out from a docker register server
logs Fetch the logs of container
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
pause Pause all process within a container
pull
push Push an image or a repository to the docker registory server
restart
rm
rmi
run
save Save an image to a tar archive
search Search for an image on the Docker Hub
start
stop
tag
top
unpause
version
wait