通过dockerfile制作基于centos8系统的nginx镜像

通过dockerfile制作基于centos8系统的nginx镜像


在拉取docker默认的nginx镜像后发现是debian系统,每次更新软件都非常痛苦,于是想制作一个自定义的nginx系统作为替代默认的nginx镜像

国内centos使用的人更多,yum源更加稳定,浪费一些空间和性能是可以接受的

1.拉取centos最新镜像
docker pull centos

2.交互式运行该镜像并在里面安装常用的工具软件
docker run -it --name=mycentos centos

a.安装aliyun的yum源
地址:
https://developer.aliyun.com/mirror/

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

b.安装epel源
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

c.设置网络
vim /etc/profile

export http_proxy=http://10.11.0.148:808
export https_proxy="https://10.11.0.148:808"
export ftp_proxy=http://10.11.0.148:808
export socks_proxy="socks://10.11.0.148:808/"
export http_proxy=http://10.11.0.148:808
export https_proxy=https://10.11.0.148:808

# 让配置生效

# source /etc/profile


# 设置yum的网络,方便安装软件
# vim /etc/yum.conf
[main]
gpgcheck=1
installonly_limit=3
clean_requirements_on_remove=True
best=True
skip_if_unavailable=False
proxy=http://10.11.0.148:808


d.安装常用软件
yum install -y telnet tree net-tools bind-utils screen nginx

3.制作镜像
# 在centos镜像中安装好需要的软件后需要提交容器名为 mynginx
[root@server05 ~]# docker commit mycentos mynginx

# 命名mynginx 这个镜像为符合我们本地 docker 仓库的命名规范
[root@server05 ~]# docker tag mynginx harbor.chinasoft.com/public/mynginx

# 编辑dockerfile
[root@server05 dockerfile]# more /data/dockerfile/entrypoint.sh
#!/bin/bash
/usr/sbin/nginx -g "daemon off;"

[root@server05 dockerfile]# more /data/dockerfile/Dockerfile
FROM harbor.chinasoft.com/public/mynginx
ADD entrypoint.sh /entrypoint.sh
ENV WWW /usr/share/nginx/html
ENV CONF /etc/nginx/conf.d
RUN /usr/bin/ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\
    echo 'Asia/Shanghai' >/etc/timezone
WORKDIR $WWW
ENTRYPOINT /entrypoint.sh

# 制作镜像
[root@server05 dockerfile]# docker build . -t harbor.chinasoft.com/public/mynginx
Sending build context to Docker daemon  2.048kB
Step 1/8 : FROM harbor.chinasoft.com/public/mynginx
 ---> 9295f7029377
Step 2/8 : USER root
 ---> Running in fe3934f06944
Removing intermediate container fe3934f06944
 ---> 45cc4be244a5
Step 3/8 : ENV WWW /usr/share/nginx/html
 ---> Running in 6cb17d4cab7c
Removing intermediate container 6cb17d4cab7c
 ---> fa759741583e
Step 4/8 : ENV CONF /etc/nginx/conf.d
 ---> Running in 8afa0d30e5f1
Removing intermediate container 8afa0d30e5f1
 ---> 7f0a150d5d0f
Step 5/8 : RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&    echo 'Asia/Shanghai' >/etc/timezone
 ---> Running in 8e2594b80656
Removing intermediate container 8e2594b80656
 ---> a25d5f97272d
Step 6/8 : WORKDIR $WWW
 ---> Running in 043eaf56d32c
Removing intermediate container 043eaf56d32c
 ---> 4fc08640ac6a
Step 7/8 : EXPOSE 80
 ---> Running in f7d30addfc42
Removing intermediate container f7d30addfc42
 ---> 150142c55f81
Step 8/8 : CMD ["nginx","-g","daemon off;"]
 ---> Running in 1dd362d3ae84
Removing intermediate container 1dd362d3ae84
 ---> ce3a233d57f7
Successfully built ce3a233d57f7
Successfully tagged harbor.chinasoft.com/public/mynginx:latest

# Dockerfile也可以自定义首页和虚拟主机,此处我们不需要就略过了
FROM harbor.chinasoft.com/public/mynginx
USER root
ENV WWW /usr/share/nginx/html
ENV CONF /etc/nginx/conf.d
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\
    echo 'Asia/Shanghai' >/etc/timezone
WORKDIR $WWW
ADD index.html $WWW/index.html
ADD demo.od.com.conf $CONF/demo.od.com.conf
EXPOSE 80
CMD ["nginx","-g","daemon off;"]

# 推送镜像到harbor仓库中
[root@server05 ~]# docker push harbor.chinasoft.com/public/mynginx

# 运行容器,将容器映射到宿主机的8888端口
docker run -d --name=mynginx01 -p8888:80 harbor.chinasoft.com/public/mynginx:latest

harbor崩溃后需要执行./install.sh即可,之前的配置和已经制作的镜像不受影响


测试刚才制作的镜像是否成功,运行docker后nginx是否会正常运行

拉取镜像

运行镜像,进入容器中,并测试nginx

 

通过kubectl创建基于mycentos的pod

 

centos7的环境初始化

# yummv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

# socks代理
http_proxy=http://10.11.0.150:808
https_proxy="https://10.11.0.150:808"
export ftp_proxy=http://10.11.0.150:808
export socks_proxy="socks://10.11.0.150:808/"
export http_proxy=http://10.11.0.150:808
export https_proxy=https://10.11.0.150:808


# wget命令代理
[root@node03 ~]# cat ~/.wgetrc 
http_proxy = http://10.11.0.150:808
ftp_proxy = http://10.11.0.150:808


# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# Step 4: 更新并安装Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo service docker start

 

posted @ 2020-09-04 20:11  reblue520  阅读(925)  评论(0编辑  收藏  举报