1、安装配置docker并准备基础镜像
1.1 环境准备
Ubuntu 18.04 :192.168.7.38
1.2 Ubuntu安装docker并启动
<root@ubuntu183 ~>#sudo apt-get update
<root@ubuntu183 ~>#sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
<root@ubuntu183 ~>#curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
OK
<root@ubuntu183 ~>#sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
<root@ubuntu183 ~>#sudo apt-get -y update
<root@ubuntu183 />#sudo apt-get -y install docker-ce
<root@ubuntu183 />#docker --version
Docker version 19.03.11, build 42e35e61f3
<root@ubuntu183 />#systemctl start docker
1.3 docker 镜像加速配置
http://cr.console.aliyun.com获取加速地址
<root@ubuntu183 />#cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://1sffmjxd.mirror.aliyuncs.com","https://registry.docker-cn.com"]
}
<root@ubuntu183 />#systemctl daemon-reload
<root@ubuntu183 />#sudo systemctl restart docker
1.4 使用dockerfile制作centos基础镜像
下载centos镜像
<root@ubuntu183 centos>#docker pull centos:centos7
<root@ubuntu183 centos>#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos centos7 b5b4d78bc90c 3 months ago 203MB
启动并查看centos版本
<root@ubuntu183 centos>#docker run -it --rm docker.io/centos:centos7 /bin/bash
[root@6fac768a0493 /]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@6fac768a0493 /]# exit
1.5 创建相关目录
<root@ubuntu183 />#mkdir -pv /opt/dockerfile/{web/{nginx,tomcat,jdk,apache},system/{centos,ubuntu,redhat}}
mkdir: created directory '/opt/dockerfile'
mkdir: created directory '/opt/dockerfile/web'
mkdir: created directory '/opt/dockerfile/web/nginx'
mkdir: created directory '/opt/dockerfile/web/tomcat'
mkdir: created directory '/opt/dockerfile/web/jdk'
mkdir: created directory '/opt/dockerfile/web/apache'
mkdir: created directory '/opt/dockerfile/system'
mkdir: created directory '/opt/dockerfile/system/centos'
mkdir: created directory '/opt/dockerfile/system/ubuntu'
mkdir: created directory '/opt/dockerfile/system/redhat'
1.6 制作centos基础镜像
创建Dockerfile文件
<root@ubuntu183 />#cd /opt/dockerfile/system/centos
<root@ubuntu183 centos>#cat Dockerfile
from centos:centos7
MAINTAINER kong kongxuc@163.com
RUN yum install epel-release -y && yum install vim iotop bc gcc gcc-c++ glibc glibc-devel pcre \
pcre-devel openssl openssl-devel zip unzip zlib-devel net-tools \
lrzsz tree ntpdate telnet lsof tcpdump wget libevent libevent-devel \
bc systemd-devel bash-completion traceroute -y && useradd nginx -u 2019 && useradd tomcat -u 2020 && rm -rf /etc/localtime && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
创建build脚本
<root@ubuntu183 centos>#cat build-command.sh
#!/bin/bash
docker build -t centos-base:7.8 .
<root@ubuntu183 centos>#sh build-command.sh
查看镜像
2、使用dockerfile制作nginx+php-fpm镜像,实现lnmp
2.1 查看nginx目录文件
2.2 创建Dockerfile文件
<root@ubuntu183 />#cd /opt/dockerfile/web/nginx
<root@ubuntu183 nginx>#cat Dockerfile
FROM centos-base:7.8
MAINTAINER kong kongxuc@163.com
RUN yum install -y wget vim pcre unzip pcre-devel openssl openssl-devel libicu-develgcc 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 openjpeg-devel #安装相关依赖
ADD php-7.2.31.tar.gz /usr/local/src/
ADD nginx-1.14.2.tar.gz /usr/local/src/
RUN cd /usr/local/src/php-7.2.31 && ./configure --prefix=/apps/php --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 && make install && cd /apps/php/etc/php-fpm.d/ && cp /usr/local/src/php-7.2.31/php.ini-production /apps/php/etc/php.ini && useradd www -s /sbin/nologin -u 1001 && mkdir /apps/php/log/ && cp /apps/php/etc/php-fpm.conf.default /apps/php/etc/php-fpm.conf && ln -sv /apps/php/sbin/php-fpm /usr/sbin/php-fpm
COPY wordpress-5.4.1.zip /data/nginx/
COPY www.conf /apps/php/etc/php-fpm.d/
RUN cd /usr/local/src/nginx-1.14.2 && ./configure --prefix=/apps/nginx --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make && make install && ln -sv /apps/nginx/sbin/nginx /usr/sbin/nginx
RUN cd /data/nginx/ && unzip wordpress-5.4.1.zip && chown www.www /data/nginx/wordpress/ /apps/nginx/ -R
COPY wp-config.php /data/nginx/wordpress/
COPY nginx.conf /apps/nginx/conf
COPY start.sh /data/
EXPOSE 80 443 9000
CMD ["/data/start.sh"]
<root@ubuntu183 nginx>#cat start.sh
#!/bin/bash
php-fpm -c /apps/php/etc/php.ini
nginx
2.3 创建build脚本
<root@ubuntu183 nginx>#cat build-command.sh
#!/bin/bash
TAG=$1
docker build -t nginx:$1 .
2.4 制作镜像
root@ubuntu183 nginx>#sh build-command.sh lnmp
查看镜像
2.5 启动并验证(mysql设置见一)
<root@ubuntu183 nginx>#docker run -it -p 80:80 -d ea622adb49e0
9f52ab7dd28e2c77322f96e685e8dccb244d032703e051f014ac22773693117c
3、使用dockerfile制作tomcat镜像,并实现对jsp测试页访问
3.1查看tomcat目录文件
3.2 创建Dockerfile文件
<root@ubuntu183 tomcat>#cat Dockerfile
FROM centos-base:7.8
MAINTAINER kong kongxuc@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
ADD apache-tomcat-8.5.37.tar.gz /apps
RUN ln -sv /apps/apache-tomcat-8.5.37 /apps/tomcat
ADD code.tar.gz /data/tomcat/webapps/app1
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD server.xml /apps/tomcat/conf
RUN chown -R tomcat.tomcat /apps/apache-tomcat-8.5.37 /apps/tomcat /data/tomcat
EXPOSE 8080 8443
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
<root@ubuntu183 tomcat>#cat run_tomcat.sh
#!/bin/bash
source /etc/profile
echo "1.1.1.1 www.magedu.net" >> /etc/hosts
su - tomcat -c "/apps/tomcat/bin/catalina.sh start"
#su - tomcat -c "/apps/tomcat/bin/catalina.sh run"
tail -f /etc/hosts
3.3 创建build脚本
<root@ubuntu183 tomcat>#cat build-command.sh
#!/bin/bash
docker build -t tomcat:v1 .
3.4制作镜像并查看镜像
<root@ubuntu183 tomcat>#sh build-command.sh
查看镜像
3.5启动并验证
<root@ubuntu183 tomcat>#docker run -it -p 8080:8080 -d 0ea9aea6edc9
798be86577e388175165d4c7455f1b9e2fa04326f0156ba052306b3e669c6dd1
访问:http://192.168.7.38:8080/app1/
4、安装配置harbor服务,并将打包好的镜像提交到harbor仓库(待完成)
er run -it -p