Docker部署前准备
l 配置主机名
l 关闭防火墙、selinux、iptables
l 配置时间同步
l 安装基础软件包
l 配置docker-ce源,安装docker依赖以及启动docker
l 开启转发功能(修改内核文件)br_netfilter模块
l 配置docker镜像加速
hostnamectl set-hostname docker01
systemctl stop firewalld && systemctl disable firewalld
yum install iptables-services -y
service iptables stop && systemctl disable iptables
iptables -F
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
getenforce
yum install -y ntp ntpdate
ntpdate cn.pool.ntp.org
crontab -e
* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org
systemctl restart crond
docker安装
yum install -y wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y yum-utils device-mapper-persistent-data lvm2 docker-ce
systemctl start docker && systemctl enable docker
systemctl status docker
modprobe br_netfilter
cat > /etc/sysctl.d/docker.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl -p /etc/sysctl.d/docker.conf
vi /etc/rc.sysinit
#!/bin/bash
for file in /etc/sysconfig/modules/*.modules ; do
[ -x $file ] && $file
done
vi /etc/sysconfig/modules/br_netfilter.modules
modprobe br_netfilter
chmod 755 /etc/sysconfig/modules/br_netfilter.modules
lsmod |grep br_netfilter
systemctl restart docker
vi /etc/docker/daemon.json
{
"registry-mirrors":["https://y8y6vosv.mirror.aliyuncs.com","https://registry.docker- cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub- mirror.c.163.com"]
}
sudo systemctl daemon-reload
sudo systemctl restart docker
镜像操作
l docker search centos 在线查找镜像资源
l docker pull centos 下载镜像
l docker images 查看本地镜像
l docker save -o centos.tar.gz centos 把镜像做出离线压缩包
l docker load -I centos.tar.gz 解压离线压缩包
l docker rmi [images id] 删除镜像
l docker rmi docker images -q
批量删除全部镜像
容器操作
l docker run –name=hello -it centos /bin/bash 交互式启动并进入镜像 exit可退闭
// -I 交互式 -t 分配终端
l docker run –name=hello2 -td centos 后台方式启动容器
// -d 后台运行
l docker ps 查看正在运行容器 -a 查看所有容器
l docker stop hello1 停止容器
l docker start hello1 启动容器
l docker exec -it hello /bin/bash 进入容器
l docker rm -f hello01 删除容器
l docker –help 查看docker命令帮助
案例:docker部署nginx服务
docker run –name nginx -p 80 -itd centos
//-p 将80端口映射成物理机的随机端口
docker ps | grep nginx
docker exec -it nginx /bin/bash
yum install wget vim-enhanced nginx -y
mkdir /var/www/html -p
cd /var/www/html/
vi index.html
<html>
<head>
<title>nginx in docker</title> </head>
<body>
<h1>hello,My Name is xianchao</h1>
</body>
</html>
vim /etc/nginx/nginx.conf
root /var/www/html/;
启动 nginx
/usr/sbin/nginx
(退出容器)
docker ps | grep nginx 查看端口
curl [<u>http://192.168.40.180:49153</u>](http://192.168.40.180:49153) 测试(也可以访问容器地址:80)
网友评论