环境:基于CentOS Linux release 7.6.1810
注意:以下所有操作基于root用户
#centos7
# step 1: 安装必要的一些系统工具
yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装Docker-CE
yum makecache fast
yum -y install docker-ce
# Step 4: 开启Docker服务
service docker start
#安装指定版本的Docker-CE:
#Step 1: 查找Docker-CE的版本:
yum list docker-ce.x86_64 --showduplicates | sort -r
#安装指定版本
yum -y install docker-ce-19.03.12
#设置开机自启动
systemctl enable --now docker
构建centos基础镜像
注意:以下业务(nginx、php)镜像都是基于centos基础镜像
#centos
[root@localhost system]# tree centos/
centos/
├── build-command.sh
├── CentOS-Base.repo
└── Dockerfile
0 directories, 3 files
#CentOS-Base.repo镜像仓库地址为阿里云镜像下载
#dockerfile
[root@localhost centos]# cat Dockerfile
#!/bin/bash
#centos-base:7.8.2003
From centos:7.8.2003
LABEL chenjianhong 1434207372@qq.com
RUN rm -f /etc/yum.repos.d/*
ADD CentOS-Base.repo /etc/yum.repos.d/
RUN yum -y install vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel unzip iproute net-tools iotop && groupadd -g 2001 www && useradd www -u 2001 -g www && groupadd -g 2002 nginx && useradd nginx -u 2002 -g nginx
#主要辨别标识和管理,因为镜像一多自己可能都不会记得
[root@localhost centos]# cat build-command.sh
#!/bin/bash
docker build -t centos-base:7.8.2003 .
查看镜像
可以看到基础镜像构建完成
构建nginx+php业务镜像
构建nginx业务镜像
#查看源码编译安装docker目录结构
[root@localhost nginx]# tree src/
src/
├── build-command.sh
├── Dockerfile
├── nginx-1.18.0.tar.gz
└── nginx.conf
0 directories, 4 files
#查看Dockerfile
[root@localhost src]# cat Dockerfile
#!/bin/bash
From centos-base:7.8.2003
LABEL chenjianhong 1434207372@qq.com
ADD nginx-1.18.0.tar.gz /usr/local/src
RUN cd /usr/local/src/nginx-1.18.0 && ./configure --prefix=/usr/local/nginx --with-http_sub_module && make && make install
ADD nginx.conf /usr/local/nginx/conf/
RUN echo "This is test dockerfile nginx" > /usr/local/nginx/html/index.html && chown -R nginx.nginx /usr/local/nginx/html && ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx
expose 80
CMD ["nginx", "-g", "daemon off;"]
#查看nginx.conf
#php配置root要和php部署目录保持一致
[root@localhost nginx]# cat nginx.conf
user www www;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location /usr/local/html {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name 192.168.100.101;
location / {
root /data/wwwroot;
index index.html index.htm index.php;
}
location ~ \.php$ {
root /data/wwwroot;
fastcgi_pass 192.168.100.101:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
#查看build-command.sh构建脚本
[root@localhost src]# cat build-command.sh
#!/bin/bash
docker build -t centos-base/nginx-1.18.0:v1 .
#执行构建脚本
[root@localhost src]# bash build-command.sh
Sending build context to Docker daemon 1.045MB
Step 1/8 : From centos-base:7.8.2003
---> 4cbb6c4c2d61
Step 2/8 : LABEL chenjianhong 1434207372@qq.com
---> Using cache
---> 48db51f6b2c7
Step 3/8 : ADD nginx-1.18.0.tar.gz /usr/local/src
---> Using cache
---> 751a73fb2633
Step 4/8 : RUN cd /usr/local/src/nginx-1.18.0 && ./configure --prefix=/usr/local/nginx --with-http_sub_module && make && make install
---> Using cache
---> 4d3d62f8d38b
Step 5/8 : ADD nginx.conf /usr/local/nginx/conf/
---> Using cache
---> 4d7066bf03c9
Step 6/8 : RUN echo "This is test dockerfile nginx" > /usr/local/nginx/html/index.html && chown -R nginx.nginx /usr/local/nginx/html && ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx
---> Using cache
---> 5974c5bd6a50
Step 7/8 : expose 80
---> Using cache
---> ad139be43cb7
Step 8/8 : CMD ["nginx", "-g", "daemon off;"]
---> Using cache
---> 75d15350c4b7
Successfully built 75d15350c4b7
Successfully tagged centos-base/nginx-1.18.0:v1
可以看到镜像构建成功!~
构建业务镜像php
# 查看目录树
[root@localhost lnmp]# tree php
php
├── build-command.sh
├── Dockerfile
├── index.php
├── php-7.1.30.tar.gz
├── run_php.sh
└── www.conf
0 directories, 6 files
#查看php配置文件
[root@localhost php]# cat www.conf
[www]
user = www
group = www
listen = 0.0.0.0:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
#查看php配置
[root@localhost php]# cat www.conf
[www]
user = www
group = www
listen = 0.0.0.0:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
#查看启动脚本
[root@localhost php]# cat index.php
<?php
phpinfo();
?>
[root@localhost php]# cat run_php.sh
#!/bin/bash
su - www -c "/data/tools/php/sbin/php-fpm"
su - www -c "tail -f /etc/hosts"
#查看dockerfile
[root@localhost php]# cat Dockerfile
#!/bin/bash
From centos-base:7.8.2003
LABEL chenjianhong 1434207372@qq.com
RUN yum -y install wget vim pcre pcre-devel openssl openssl-devel libicudevel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpegdevel
ADD php-7.1.30.tar.gz /usr/loca/src
RUN cd /usr/loca/src/php-7.1.30/ && ./configure --prefix=/data/tools/php-7.1.30 --enable-fpm --with-fpm user=www --with-fpm-group=www --with-pear --with-curl --with-png-dir --with-freetype-dir --with-iconv --with-mhash --with-zlib --with-xmlrpc --with-xsl --with-openssl --with-mysqli --with-pdo-mysql --disable-debug --enable-zip --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-exif --enable-wddx --enable-bcmath --enable-calendar --enable-shmop --enable-dba --enable-sysvsem --enable-sysvshm --enable-sysvmsg && make -j 4 && make install && ln -sv /data/tools/php-7.1.30/ /data/tools/php && cp /usr/loca/src/php-7.1.30/php.ini-production /data/tools/php-7.1.30/etc/php.ini
ADD www.conf /data/tools/php-7.1.30/etc/php-fpm.d
ADD index.php /data/wwwroot/
RUN cp /data/tools/php-7.1.30/etc/php-fpm.conf.default /data/tools/php-7.1.30/etc/php-fpm.conf && chown -R www.www /data/wwwroot/
ADD run_php.sh /data/tools/php/sbin/
EXPOSE 9000
CMD ["/data/tools/php/sbin/run_php.sh"]
查看镜像
启动容器nginx+php
#启动php
docker run -it -d -p 9000:9000 centos-lnmp/php:v1
#启动nginx
docker run -it -d -p 80:80 centos-lnmp/php:v1
访问测试
至此dockerfile的源码编译安装nginx和php已经完成
dockerfile源码编译安装tomcat请查看我上一篇链接:
https://blog.51cto.com/546700/2516050
安装harbor
#因为docker安装上面有讲到,就不在讲诉,请参考上面的基础镜像
#安装docker-compose
[root@localhost soft]# mv docker-compose-Linux-x86_64 docker-compose && chmod +x docker-compose && mv docker-compose /usr/bin/
#安装harbor
[root@localhost harbor]# pwd
/data/soft/harbor
[root@localhost harbor]# mv harbor.yml.tmpl harbor.yml
#设置有注释的行,注释掉证书设置
[root@localhost harbor]# egrep -v "#|^$" harbor.yml
#设置域名访问
hostname: reg.harbor.com
http:
port: 80
#设置密码
harbor_admin_password: Harbor12345
database:
password: root123
max_idle_conns: 50
max_open_conns: 1000
#设置数据目录
data_volume: /data/harbor
clair:
updaters_interval: 12
trivy:
ignore_unfixed: false
skip_update: false
insecure: false
jobservice:
max_job_workers: 10
notification:
webhook_job_max_retry: 10
chart:
absolute_url: disabled
log:
level: info
local:
rotate_count: 50
rotate_size: 200M
location: /var/log/harbor
_version: 2.0.0
proxy:
http_proxy:
https_proxy:
no_proxy:
components:
- core
- jobservice
- clair
- trivy
#部署
[root@localhost harbor]# ./install.sh
# 如果需要更新harbor请执行以下操作
[root@localhost harbor]# pwd
/data/soft/harbor
#更新配置
[root@localhost harbor]# ./prepare
#执行完毕后会在当前目录生成一个 docker-compose.yml 文件,用于配置数据目 录等配置信息
#启动服务
docker-compose start
``
![](https://s4.51cto.com/images/blog/202008/10/472f185e34b433c7baa1befedba1f432.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
#登录harbor
windows服务设置host解析:
C:\Windows\System32\drivers\etc\hosts---->192.168.100.101 reg.harbor.com
登录harbor:
配置 docker 使用 harbor 仓库上传下载镜像
#以下在客户端操作
#编辑 docker 配置文件
#添加解析记录
root@ubuntu-kvm:~# vim /etc/hosts
192.168.100.101 reg.harbor.com
#添加仓库解析记录
root@ubuntu-kvm:~# vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry 192.168.100.101:5000 --insecure-registry reg.harbor.com
#重启docker
root@ubuntu-kvm:~# systemctl daemon-reload
root@ubuntu-kvm:~# systemctl restart docker
#验证能否登录 harbor
测试上传和下载镜像
#上传镜像
**注意**:
私有:只有登录用户才能上传和下载镜像
公开:只可以下载镜像不能上传镜像
#修改镜像tag
#修改 images 的名称,不修改成指定格式无法将镜像上传到 harbor 仓库,格式 为: HarborIP/项目名/image 名字:版本号
root@ubuntu-kvm:/data/soft# docker tag centos-base:7.8.2003 reg.harbor.com/system-base/centos:7.8.2003
[root@localhost ~]# docker images
#将镜像push到harbor上
root@ubuntu-kvm:~# docker push reg.harbor.com/system-base/centos:7.8.2003
将镜像pull到本地
root@ubuntu-kvm:~# docker pull reg.harbor.com/system-base/centos:7.8.2003
至此,镜像的harbor仓库已经搭建完成