这里写目录标题
镜像相关命令
从dockerhub查找镜像
docker search centos #查找centos镜像
NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
OFFICIAL: 是否 docker 官方发布
AUTOMATED: 自动构建。
docker pull centos #下载centos镜像
docker images #查看本地镜像
docker save -o centos.tar.gz centos #把镜像做成离线压缩包
docker load -i centos.tar.gz #解压离线镜像包
docker rmi -f centos:latest #删除镜像
如未联网可下载centos.tar.gz
百度网盘链接:https://pan.baidu.com/s/1OxsFR_fQpV5DYvb-IaMf-w?pwd=xhrh
提取码:xhrh
容器相关操作
以交互式方式启动并进入容器
docker run --name=hello -it centos /bin/bash
输入exit,退出容器,退出之后容器也会停止,不会再前台运行
docker run运行并创建容器
–name 容器的名字
-i 交互式
-t 分配伪终端
centos: 启动docker需要的镜像
/bin/bash说明你的shell类型为bash,bash shell是最常用的一种shell, 是大多数Linux发行版默认的shell。 此外还有C shell等其它shell。
以守护进程方式启动容器
-d在后台运行docker
-t 分配伪终端
docker run --name=hello1 -td centos #在后台生成个叫hello1的centos容器
docker ps |grep hello1
docker exec -it hello1 /bin/bash #交互式登录hello1这个容器
exit #退出容器,且容器不会停止
查看正在运行的容器
docker ps -a #查看所有容器,包括运行和退出的容器
docker logs hello1 #查看容器日志
docker stop hello1 #停止容器
docker start hello1 #启动已经停止的容器
docker exec -it hello1 /bin/bash #进入容器
docker rm -f hello1 #删除容器
docker --help #查看docker帮助命令
通过docker部署nginx服务
-p把容器端口随机在物理机随机映射一个端口
docker run --name nginx -p 80 -itd centos
在docker里安装nginx
docker inspect nginx #查看容器IP
或者进入容器查看
docker exec -it nginx /bin/bash #进入容器后查看容器ip
ip addr
通过上面可以看到容器的ip是172.17.0.3
rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum install wget -y
yum install nginx -y
yum install vim-enhanced -y
创建静态页面
mkdir /var/www/html -p
cd /var/www/html/
vim /var/www/html/index.html
<html>
<head>
<title>nginx in docker</title>
</head>
<body>
<h1>hello,My Name is docker</h1>
</body>
</html>
vim /etc/nginx/nginx.conf
vim /etc/nginx/nginx.conf
root /var/www/html/;
启动Nginx
/usr/sbin/nginx
访问docker里的nginx服务
复制一个终端窗口,执行如下命令
docker ps | grep nginx
能查看到nginx容器在物理机映射的端口是32768
查看宿主机IP和映射端口
curl http://192.168.40.10:32768
直接查看容器ip和端口
curl 172.17.0.3:80
流量走向:
访问物理节点ip:port(容器在物理节点映射的端口)–>容器ip:port(容器里部署的服务的端口)–>就可以访问到容器里部署的应用了