docker 基础入门

docker 的基本使用

帮助命令

docker verion  显示docker的版本信息
docker info    显示docker的系统信息,包括镜像和容器的数量
docker --help  帮助命令

帮助文档

https://docs.docker.com/reference/

镜像命令


docker images 查看所有本地的主机上的镜像

参数解析
REPOSITORY  镜像的仓库源
TAG  				镜像的标签
IMAGE ID 		镜像的ID
CREATE  		镜像的创建时间
SIZE    		镜像的大小

docker search 搜索镜像

docker search xxx 搜索某种镜像

例如:
docker search mysql

--filter=STARTS=3000
搜索收藏数大于3000的镜像

docker pull 下载镜像

aidenwang@AIDENXWANG-MB0 ~ % docker pull mysql
Using default tag: latest  # 不加tag 默认最新版
latest: Pulling from library/mysql 
b4d181a07f80: Pull complete  # 联合文件系统分层下载 
a462b60610f5: Pull complete 
578fafb77ab8: Pull complete 
524046006037: Pull complete 
d0cbe54c8855: Pull complete 
aa18e05cc46d: Pull complete 
32ca814c833f: Pull complete 
9ecc8abdb7f5: Pull complete 
ad042b682e0f: Pull complete 
71d327c6bb78: Pull complete 
165d1d10a3fa: Pull complete 
2f40c47d0626: Pull complete 
Digest: sha256:52b8406e4c32b8cf0557f1b74517e14c5393aff5cf0384eff62d9e81f4985d4b  # 签名
Status: Downloaded newer image for mysql:latest 
docker.io/library/mysql:latest # 真实地址

# 以下两个命令是等价的
docker pull mysql
docker pull docker.io/labrary/mysql:latest

# 指定版本下载
docker pull mysql:5.7

docker rmi 镜像删除命令

docker rmi -f 容器id # 删除指定容器
docker rmi -f 容器id 容器id. #删除多个容器
docker rmi -f $(docker images -aq) # 删除所有容器

容器命令


有了镜像才可以创建容器,我们在下载一个 centos 镜像测试学习

新建容器并启动

docker run [可选参数] image

# 常用参数说明
--name = “Name”  容器名字 例如 tomcat01, xxx 用来区分容器
-d               后台运行方式
-it              使用交互方式运行,进入容器查看内容
-p               指定容器的端口 -p 8080:8080
    -p 主机端口:容器端口(常用)
    -p 主机端口
    容器端口
-p               指定随机端口


启动并进入交互模式  
exit 退出容器回到主机
aidenwang@AIDENXWANG-MB0 ~ % docker run -it centos /bin/bash
[root@be309c889c82 /]# 

列出所有正在运行的容器

# docker ps 命令

-a   列出当前 + 历史运行容器
-n=? 显示最近创建的容器
-q   只显示容器编号

退出容器

exit  # 直接退出容器
ctrl + P + Q  # 容器不停止退出

删除容器

不能删除正在运行的容器,但是可以通过 -f 强制删除

docker rm 容器id    # 删除指定容器
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 镜像名

# 坑点

我们输入 
docker run -d centos 

此时我们使用docker ps 我们会发现 centos 停止了


# -----------
docker 容器使用后台运行,就必须要有一个前台进程,docker 发现没有应用,就会自动停止

查看日志

docker logs

练习:docker配置一个nginx

# 搜索镜像 最好去hub上查看 可以看到更多镜像的信息
docker search nginx
# 下载镜像
docker pull nginx

# 查看当前镜像
docker images

aidenwang@AIDENXWANG-MB0 ~ % docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    4cdc5dd7eaad   2 days ago     133MB
mysql         latest    5c62e459e087   2 weeks ago    556MB
hello-world   latest    d1165f221234   4 months ago   13.3kB
centos        latest    300e315adb2f   7 months ago   209MB
ubuntu        15.10     9b9cb95443b5   4 years ago    137MB


可以发现已经下载成功



# 启动镜像
docker run -d --name nginx01 -p 3344:80 nginx

aidenwang@AIDENXWANG-MB0 ~ % docker run -d --name nginx01 -p 3344:80 nginx
b9469200fed1b86405929eeca3272123a2631912724e78dc4d3252ee549e3657

# 测试本机3344端口
curl localhost:3344  也可以在 web 页面进行查看

端口暴露的概念

![image-20210709165922478](/Users/aidenwang/Library/Application Support/typora-user-images/image-20210709165922478.png)

原文地址:https://www.cnblogs.com/wlw-x/p/14992202.html