▲单独架构的小伙伴看这里:(学习杰哥视频的作业第17-18天)
1、使用dockerfile制作nginx+php-fpm镜像,实现lnmp。
一、yum 安装docker-ce
1 配置yum源
[root@docker~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@docker~]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
[root@docker~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@docker~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@docker~]# yum makecache fast
2 安装docker-ce
[root@docker~]# yum install docker-ce-18.06.3.ce-3.el7
[root@docker~]# docker version
[root@docker ~]# systemctl start docker
[root@docker ~]# systemctl enable docker
3 docker-ce镜像加速配置
[root@docker~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://9916w1ow.mirror.aliyuncs.com"]
}
4 重启docker服务
[root@docker~]# systemctl daemon-reload
[root@docker~]# systemctl restart docker
5 验证镜像加速配置
[root@docker ~]# docker info
…………
Registry Mirrors:
https://9916w1ow.mirror.aliyuncs.com/
Live Restore Enabled: false
…………
二、制作centos基础镜像
1 编写Dockerfile
[root@docker ~]# mkdir /docker/centos -pv
[root@docker ~]# cd /docker/centos
[root@docker centos]# vim Dockerfile
FROM centos:centos7.8.2003
RUN yum install epel-release -y && yum install -y vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop systemd-devel
RUN useradd nginx -u 2019 && useradd php-fpm -u 2020 && rm -rf /etc/localtime && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
2 生成docker镜像
[root@docker centos]# docker build -t centos7-base:v1 .
三、nginx+php-fpm镜像
1 编写Dockerfile
[root@docker~]# mkdir /docker/nginx
[root@docker~]# cd /docker/nginx
[root@docker nginx~]# vim Dockerfile
FROM centos7-base:v1
RUN yum install nginx -y && yum install --enablerepo=remi,remi-php56 php php-opcache php-pecl-apcu php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-pdo php-pear php-fpm php-cli php-xml php-bcmath php-process php-gd php-common -y
ADD php-fpm.conf /etc/php-fpm.conf
ADD test.conf /etc/nginx/conf.d/
ADD index.php /usr/share/nginx/html/
EXPOSE 80
CMD php-fpm && nginx -g "daemon off;"
2 编写配置文件
[root@docker nginx~]# vim test.conf
server {
listen 80;
server_name 192.168.10.204;
root /usr/share/nginx/html;
location ~* \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@docker nginx~]# vim index.php
<?php echo '<p>Hello World</p>'; ?>
[root@docker nginx~]# vim php-fpm.conf
…………
daemonize = yes
3 生成docker镜像
[root@docker nginx~]# docker build -t nginx-php-base:v1 .
四、运行docker镜像
[root@docker nginx~]# docker run -itd -p 80:80 nginx-php-base:v1
2、使用dockerfile制作tomcat镜像,并实现对jsp测试页访问
一、环境准备
1 centos的docker镜像使用上个实验生成的镜像:centos7-base:v1
2 JDK软件版本:jdk-8u192-linux-x64.tar
二、构建 JDK 镜像
1 创建JDK镜像文件夹
[root@docker~]# mkdir /opt/dockerfile/web/jdk -pv
[root@docker~]# cd /opt/dockerfile/web/jdk
2 编辑Dockerfile文件
[root@docker jdk~]# vim Dockerfile
#JDK Base Image
FROM centos7-base:v1
MAINTAINER chenyb "chenyb@163.com"
ADD jdk-8u192-linux-x64.tar.gz /usr/local/src/
RUN ln -sv /usr/local/src/jdk1.8.0_192 /usr/local/jdk
ADD profile /etc/profile
ENV JAVA_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
ENV PATH $PATH:$JAVA_HOME/bin
RUN rm -rf /etc/localtime && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
3 上传 JDK 压缩包和 profile 文件至 /opt/dockerfile/web/jdk
[root@docker jdk~]# cat profile
…………
export JAVA_HOME=/usr/local/jdk
export TOMCAT_HOME=/apps/tomcat
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$TOMCAT_HOME/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
4 执行构建镜像脚本并执行
[root@docker jdk~]# cat build-command.sh
#!/bin/bash
docker build -t centos-7.5-jdk:v1 .
[root@docker jdk~]# bash build-command.sh
…………
Successfully built 686401a2a55c
Successfully tagged centos-7.5-jdk:v1
5 镜像启动JDK容器验证JAVA及时间
[root@docker jdk~]# docker run -it --rm 686401a2a55c bash
[root@eab0a9003a25 /]# java -version
java version "1.8.0_192"
Java(TM) SE Runtime Environment (build 1.8.0_192-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)
[root@eab0a9003a25 /]# date +%F_%T
2020-09-04_11:08:03
三、从 JDK 镜像构建 tomcat 镜像
1 创建JDK镜像文件夹并上传tomcat压缩包至/opt/dockerfile/web/tomcat
[root@docker~]# mkdir /opt/dockerfile/web/tomcat
[root@docker~]# cd /opt/dockerfile/web/tomcat
2 编辑Dockerfile文件
[root@docker tomcat~]# vim Dockerfile
FROM centos-7.5-jdk:v1
MAINTAINER chenyb@163.com
ADD apache-tomcat-8.5.37.tar.gz /apps
RUN ln -sv /apps/apache-tomcat-8.5.37 /apps/tomcat
3 执行构建镜像脚本并执行
[root@docker tomcat~]# cat build-command.sh
#!/bin/bash
docker build -t tomcat-base:8.5.37 .
[root@docker tomcat~]# bash build-command.sh
…………
Successfully built d00683ec7671
Successfully tagged tomcat-base:8.5.37
四、构建业务镜像
1 创建JDK镜像文件夹
[root@docker~]# mkdir /opt/dockerfile/web/tomcat-app
[root@docker~]# cd /opt/dockerfile/web/tomcat-app
2 编辑Dockerfile文件
[root@docker tomcat-app~]# vim Dockerfile
#Tomcat Web Image
FROM tomcat-base:8.5.37
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD myapp/* /apps/tomcat/webapps/myapp/
RUN useradd www -u 2002 && RUN chown www.www /apps/ -R
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
EXPOSE 8080 8009
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
3 准备自定义 myapp 页面:
[root@docker tomcat-app~]# mkdir myapp
[root@docker tomcat-app~]# echo "Tomcat Web Page1" > myapp/index.html
[root@docker tomcat-app~]# cat myapp/index.html
Tomcat Web Page1
4 准备容器启动执行脚本
[root@docker tomcat-app~]# cat run_tomcat.sh
#!/bin/bash
echo "1.1.1.1 abc.test.com" >> /etc/hosts
echo "nameserver 223.5.5.5" > /etc/resolv.conf
su - www -c "/apps/tomcat/bin/catalina.sh start"
su - www -c "tail -f /etc/hosts"
5 准备构建脚本
[root@docker tomcat-app~]# cat build-command.sh
#!/bin/bash
docker build -t tomcat-app1:v1 .
[root@docker tomcat-app~]# bash build-command.sh
Successfully built 55c81b6e5513
Successfully tagged tomcat-app1:v1
6 运行容器
[root@docker tomcat-app~]# docker run -it -d -p 8888:8080 tomcat-app1:v1
3、安装配置harbor服务,并将打包好的镜像提交到harbor仓库
一、服务器端docker服务
[root@docker-server1 ~]# yum install docker -y
[root@docker-server1 ~]# systemctl satrt docker
[root@docker-server1 ~]# systemctl enable docker
二、服务器端Harbor 安装
1 下载离线完整安装包
[root@docker-server2 ~]# cd /usr/local/src/
[root@docker-server2 src]# wget https://github.com/vmware/harbor/releases/download/v1.2.2/harbor-offline-installer-v1.2.2.tgz
2 解压并编辑 harbor.cfg
[root@docker-server1 src]# tar xvf harbor-offline-installer-v1.2.2.tgz
[root@docker-server1 src]# ln -sv /usr/local/src/harbor /usr/local/
[root@docker-server1 harbor]# cd /usr/local/harbor/
[root@docker-server1 harbor]# vim harbor.cfg
hostname = 192.168.10.205
harbor_admin_password = abc.123
3 安装配置harbor服务
[root@docker-server1 harbor]# yum install docker-compose –y
[root@docker-server1 harbor]# bash install.sh
[root@docker-server1 harbor]# docker-compose start
三、web 访问 Harbor 管理界面
四、编辑 docker 配置文件:
[root@docker~]# vim /etc/sysconfig/docker
OPTIONS='--selinux-enabled --log-driver=journald --insecure-registry 192.168.10.205' #其中 192.168.10.205 是Harbor的地址
[root@docker-server1 ~]# systemctl stop docker
[root@docker-server1 ~]# systemctl start docker
五、验证能否登录 harbor
[root@docker~]# docker login 192.168.10.205
Username: admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
六、镜像打 tag: #修改 images 的名称,不修改成指定格式无法将镜像上传到 harbor 仓库,格式为: HarborIP/项目名/image 名字:版本号:
[root@docker~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat-app1 v1 e87780af4649 2 hours ago 932MB
tomcat-base 8.5.37 d00683ec7671 4 hours ago 917MB
centos-7.5-jdk v1 686401a2a55c 4 hours ago 904MB
nginx-php-base v1 8ae873270d77 25 hours ago 741MB
centos7-base v1 54d15c9bff27 26 hours ago 507MB
[root@docker~]# docker tag centos7-base:v1 192.168.10.100/base-images/centos7-base:20200904-1547
[root@docker~]# docker images
192.168.10.100/base-images/centos7-base 20200904-1547 54d15c9bff27 26 hours ago 507MB
centos7-base v1 54d15c9bff27 26 hours ago 507MB
centos centos7.8.2003 afb6fca791e0 4 months ago 203MB
七、将镜像 push 到 harbor
[root@docker~]# docker push 192.168.10.100/base-images/centos7-base:20200904-1547
The push refers to repository [192.168.10.100/base-images/centos7-base]
a71b17a3477d: Pushed
28588315395e: Pushed
fb82b029bea0: Pushed
20200904-1547: digest: sha256:67e2611b2f98b4a9a4c79ec34257c9a4e1349d7a85593a3ea40928e1eece7436 size: 950