Dockerfile构建nginx、php和tomcat镜像以及搭建企业级harbor

时间:2020-09-21 19:51:33 来源:

【摘要】 Dockerfile构建nginx、php和tomcat镜像以及搭建企业级harbor考必过小编为大家整理了关于Dockerfile构建nginx、php和tomcat镜像以及搭建企业级harbor的信息,希望可以帮助到大家!

Dockerfile构建nginx、php和tomcat镜像以及搭建企业级harbor

标签:althtmlweb浏览器blepplpneexternetc

1、使用dockerfile制作nginx+php-fpm镜像,实现lnmp。

1.1 制作基础镜像

[root@offpne base]# cat Dockerfile 
FROM centos:centos7.8.2003

MAINTAINER RICKZHU
RUN yum install wget -y     && rm -rf /etc/yum.repos.d/*.repo     && wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.apyun.com/repo/Centos-7.repo     && wget -O /etc/yum.repos.d/epel.repo http://mirrors.apyun.com/repo/epel-7.repo
RUN yum install -y gcc gcc-c++ gpbc make autoconf openssl openssl-devel ntpdata crontabs
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@offpne base]# docker build -t centos:base .
[root@offpne base]# docker images |grep centos
centos                                        base                            dbddb0186fa6        4 minutes ago        542MB

1.2 制作nginx+php-fpm镜像

[root@offpne nginx-php]# cat Dockerfile 
FROM centos:base
MAINTAINER Rickzhu
RUN yum install nginx -y && mkdir -p /data/php
ADD lnmp.conf /etc/nginx/conf.d/
ADD index.php /data/php
ADD abc.html /data/php
RUN yum install php php-mysql php-fpm -y
EXPOSE 80 9000
CMD /usr/sbin/php-fpm -D && nginx -g "daemon off;"
[root@offpne nginx-php]# ls
abc.html  Dockerfile  index.php  lnmp.conf  nginx.conf
[root@offpne nginx-php]# cat abc.html 
<h1>Hello Docker nginx-php</h1>
[root@offpne nginx-php]# cat index.php 
<?php phpinfo() ?>
[root@offpne nginx-php]# cat lnmp.conf 
server {
        psten       80;
        server_name  10.0.1.24;
        root /data/php;
        index index.html index.php;
        location ~* \.php$ {
                root /data/php;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}
[root@offpne nginx-php]# docker build -t nginx-php:v1 .
[root@offpne nginx-php]# docker images |grep nginx-php
nginx-php                                     v1                              b35cdbd20e76        3 minutes ago       669MB

1.4 启动nginx-php容器

[root@offpne nginx-php]# docker run --name nginx-php -d -p 80:80 nginx-php:v1
[root@offpne nginx-php]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                          NAMES
1531116fc0a0        nginx-php:v1        "/bin/sh -c ‘/usr/sb…"   7 seconds ago       Up 6 seconds        0.0.0.0:80->80/tcp, 9000/tcp   nginx-php

1.5 测试


2、使用dockerfile制作tomcat镜像,并实现对jsp测试页访问

2.1 编写Dockerfile文件

[root@offpne tomcat]# cat Dockerfile 
#Tomcat Base Image
FROM centos:centos7.8.2003

MAINTAINER rickzhu "1779526363@qq.com"

ADD apache-tomcat-8.5.57.tar.gz /usr/local/src/
RUN ln -sv /usr/local/src/apache-tomcat-8.5.57 /usr/local/src/tomcat
RUN yum install java-1.8.0-openjdk -y
ADD index.jsp /usr/local/src/tomcat/webapps/ROOT/
EXPOSE 8080 8009
ADD run_tomcat.sh /
CMD ["/run_tomcat.sh"]

2.2 准备所需文件

[root@offpne tomcat]# cat run_tomcat.sh 
#!/bin/bash

sh /usr/local/src/tomcat/bin/startup.sh start
tail -f /etc/hosts
[root@offpne tomcat]# cat index.jsp 
  <%@ page language="java" %>
<%@ page import="java.util.*" %>
  <html>

  <head>
  <title>JSP Test Page</title>
  </head>

  <body>
     <% out.println("Welcom to access Tomcat!");%>
         </body>
  </html>
[root@offpne tomcat]# ls
apache-tomcat-8.5.57.tar.gz  Dockerfile  index.jsp  run_tomcat.sh

2.3 创建镜像

[root@offpne tomcat]# docker build -t tomcat-web:app1 .
[root@offpne tomcat]# docker images |grep tomcat
tomcat-web                                    app1                            ec07ca837027        3 minutes ago       506MB

2.4 测试

#创建容器
[root@offpne tomcat]# docker run --name tomcat -it -d -p 8080:8080 tomcat-web:app1
1d97384560c6faced5c198d083be01be5dd09e7259acb194eb48d06c5e5d8934
[root@offpne tomcat]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                              NAMES
1d97384560c6        tomcat-web:app1     "/run_tomcat.sh"    5 seconds ago       Up 4 seconds        8009/tcp, 0.0.0.0:8080->8080/tcp   tomcat

3、安装配置harbor服务,并将打包好的镜像提交到harbor仓库

3.1.安装Docker Compose

root@offpne:~#curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
root@offpne:~#chmod +x /usr/local/bin/docker-compose
root@offpne:~#ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
root@offpne:~# docker-compose --version
docker-compose version 1.18.0, build 8dd22a9

3.2下载并解压harbor离线安装包

root@offpne:~# wget https://github.com/goharbor/harbor/releases/download/v1.10.3/harbor-offpne-installer-v1.10.3.tgz
root@offpne:~# cd /usr/local/src/
root@offpne:/usr/local/src# tar xf harbor-offpne-installer-v1.10.3.tgz
root@offpne:/usr/local/src# ls
harbor  harbor-offpne-installer-v1.10.3.tgz

3.3 编辑配置文件并安装harbor

[root@offpne harbor]# grep hostname harbor.yml
# The IP address or hostname to access admin UI and registry service.
hostname: 10.0.1.24
# And when it enabled the hostname will no longer used
[root@offpne harbor]# ./install.sh --with-clair

3.4 验证

浏览器输入10.0.1.25,账号admin,默认密码Harbor12345

4.配置https的harbor

4.1 生成相关证书

#生成ca证书
[root@offpne cert]# mkdir /data/cert/^C
[root@offpne cert]# openssl genrsa -out ca.key 4096
Generating RSA private key, 4096 bit long modpus
......................++
.........................................................................................................................................................................................................++
e is 65537 (0x10001)
[root@offpne cert]# ls
ca.key
[root@offpne cert]# openssl req -x509 -new -nodes -sha512 -days 3650 >  -subj "/C=CN/ST=Guangdong/L=Guangzhou/O=example/OU=Personal/CN=harbor.nassoft.net" >  -key ca.key >  -out ca.crt
[root@offpne cert]# ls
ca.crt  ca.key
#生成服务器证书
[root@offpne cert]# openssl genrsa -out harbor.nassoft.net.key 4096
Generating RSA private key, 4096 bit long modpus
.........++
................++
e is 65537 (0x10001)
[root@offpne cert]# openssl req -x509 -new -nodes -sha512 -days 3650  -subj "/C=CN/ST=Guangdong/L=Guangzhou/O=example/OU=Personal/CN=harbor.nassoft.net"  -key harbor.nassoft.net.key  -out harbor.nassoft.net.crt
[root@offpne cert]# ks
bash: ks: command not found...
[root@offpne cert]# ls
ca.crt  ca.key  harbor.nassoft.net.crt  harbor.nassoft.net.key
#分发server证书
[root@offpne cert]# mkdir /etc/docker/certs.d/harbor.nassoft.net -p
[root@offpne cert]# cp harbor.nassoft.net.crt /etc/docker/certs.d/harbor.nassoft.net/

4.2 修改harbor配置

[root@offpne harbor]# docker-compose down -v
Stopping harbor-jobservice ... done
Stopping nginx             ... done
Stopping harbor-core       ... done
Stopping clair             ... done
Stopping redis             ... done
Stopping registry          ... done
Stopping registryctl       ... done
Stopping harbor-portal     ... done
Stopping harbor-db         ... done
Stopping harbor-log        ... done
Removing harbor-jobservice ... done
Removing nginx             ... done
Removing harbor-core       ... done
Removing clair             ... done
Removing redis             ... done
Removing registry          ... done
Removing registryctl       ... done
Removing harbor-portal     ... done
Removing harbor-db         ... done
Removing harbor-log        ... done
Removing network harbor_harbor
Removing network harbor_harbor-clair
[root@offpne harbor]# cat harbor.yml 
# Configuration file of Harbor

# The IP address or hostname to access admin UI and registry service.
# DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external cpents.
hostname: harbor.nassoft.net

# http related config
http:
  # port for http, defapt is 80. If https enabled, this port will redirect to https port
  port: 80

# https related config
https:
#   # https port for harbor, defapt is 443
  port: 443
#   # The path of cert and key files for nginx
  certificate: /data/cert/harbor.nassoft.net.crt
  private_key: /data/cert/harbor.nassoft.net.key
[root@offpne harbor]# ./prepare 
prepare base dir is set to /usr/local/src/harbor
Clearing the configuration file: /config/log/logrotate.conf
Clearing the configuration file: /config/log/rsyslog_docker.conf
Clearing the configuration file: /config/nginx/nginx.conf
Clearing the configuration file: /config/core/env
Clearing the configuration file: /config/core/app.conf
Clearing the configuration file: /config/registry/config.yml
Clearing the configuration file: /config/registry/root.crt
Clearing the configuration file: /config/registryctl/env
Clearing the configuration file: /config/registryctl/config.yml
Clearing the configuration file: /config/db/env
Clearing the configuration file: /config/jobservice/env
Clearing the configuration file: /config/jobservice/config.yml
Clearing the configuration file: /config/clair/postgresql-init.d/README.md
Clearing the configuration file: /config/clair/postgres_env
Clearing the configuration file: /config/clair/config.yaml
Clearing the configuration file: /config/clair/clair_env
Generated configuration file: /config/log/logrotate.conf
Generated configuration file: /config/log/rsyslog_docker.conf
Generated configuration file: /config/nginx/nginx.conf
Generated configuration file: /config/core/env
Generated configuration file: /config/core/app.conf
Generated configuration file: /config/registry/config.yml
Generated configuration file: /config/registryctl/env
Generated configuration file: /config/db/env
Generated configuration file: /config/jobservice/env
Generated configuration file: /config/jobservice/config.yml
loaded secret from file: /secret/keys/secretkey
Generated configuration file: /compose_location/docker-compose.yml
Clean up the input dir
[root@offpne harbor]# ls
common  docker-compose.yml  harbor.v1.9.4.tar.gz  harbor.yml  install.sh  LICENSE  prepare
[root@offpne harbor]# docker-compose up -d
Creating network "harbor_harbor" with the defapt driver
Creating harbor-log ... done
Creating registry      ... done
Creating redis         ... done
Creating harbor-db     ... done
Creating registryctl   ... done
Creating harbor-portal ... done
Creating harbor-core   ... done
Creating harbor-jobservice ... done
Creating nginx             ... done
[root@offpne harbor]# 

4.3 测试

4.3.1 测试上传镜像

[root@offpne cert]# echo 10.0.1.24 harbor.nassoft.net >> /etc/hosts
[root@offpne cert]# docker login harbor.nassoft.net
Username: admin
Password: Harbor12345
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/commandpne/login/#credentials-store

Login Succeeded
[root@offpne cert]# docker ppl busybox:latest
[root@offpne cert]# docker tag busybox:latest harbor.nassoft.net/baseimages/busybox:latest
[root@offpne cert]# docker push harbor.nassoft.net/baseimages/busybox:latest
The push refers to repository [harbor.nassoft.net/baseimages/busybox]
50761fe126b6: Pushed 
latest: digest: sha256:2131f09e4044327fd101ca1fd4043e6f3ad921ae7ee901e9142e6e36b354a907 size: 527
[root@offpne cert]# 

4.3.2 浏览器测试

Dockerfile构建nginx、php和tomcat镜像以及搭建企业级harbor

标签:althtmlweb浏览器blepplpneexternetc

以上就是Dockerfile构建nginx、php和tomcat镜像以及搭建企业级harbor的内容,更多资讯请及时关注考必过网站,最新消息小编会第一时间发布,大家考试加油!

上一篇      下一篇
前端相关推荐 更多>>
前端热点专题 更多>>
热点问答
国家公务员考试年龄限制是多少 公务员国考和省考考试内容有什么区别 函授大专学历能不能考公务员 国家公务员考试考点能自己选择吗 新闻学专业能报考2022年公务员考试吗 什么是联合培养研究生 什么是破格录取研究生 什么人不适合读研 研究生报名户口所在地填什么 研究生结业和毕业有什么区别
网站首页 网站地图 返回顶部
考必过移动版 https://m.kaobiguo.net