基于Dockerfile制作apache镜像(优化版)

dockerfile部署

准备工作

[root@localhost ~]# tree /root/apache/
/root/apache/
├── Dockerfile  //创建Dockerfile文件
└── files   //创建files目录存放源码包和脚本
    ├── apr-1.7.0.tar.gz
    ├── apr-util-1.6.1.tar.gz
    ├── httpd-2.4.53.tar.gz
    └── install.sh

编写Dockerfile

FROM centos
  
LABEL MAINTAINER='zjgg 1@2.com'

ENV apr_version=1.7.0 apr_util_version=1.6.1 httpd_version=2.4.53

ADD files/apr-1.7.0.tar.gz  /usr/src/
ADD files/apr-util-1.6.1.tar.gz  /usr/src/
ADD https://downloads.apache.org/httpd/httpd-${httpd_version}.tar.gz  /usr/src/
ADD files/install.sh  /tmp/   //将脚本从本地的files目录传到容器内的/tmp目录内
RUN /bin/bash  /tmp/install.sh   //执行脚本

WORKDIR /usr/local/apache

EXPOSE 80 443
ENTRYPOINT /usr/local/apache/bin/apachectl  -DFOREGROUND

编写脚本

[root@localhost files]# cat install.sh 
#!bin/bash
rm -rf /etc/yum.repos.d/* &&\   
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo  &&\ 
yum -y install make gcc gcc-c++ openssl-devel pcre-devel expat-devel libtool libxml2-devel &&\
useradd -r -M -s /sbin/nologin apache &&\
cd /usr/src &&\
tar xf httpd-${httpd_version}.tar.gz &&\
cd /usr/src/apr-${apr_version} &&\
sed -i '/$RM "$cfgfile"/d' configure &&\
./configure --prefix=/usr/local/apr && make && make install &&\
cd /usr/src/apr-util-${apr_util_version} &&\
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr &&\
make && make install &&\
cd /usr/src/httpd-${httpd_version} &&\
./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork && make && make install &&\
sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf &&\
dnf -y remove install gcc gcc-c++ make &&\  //卸载编译工具优化镜像大小
rm -rf /var/log/* /var/cache/* /usr/src/*  //此项为优化镜像大小删除一些无用的文件

创建镜像

[root@localhost ~]# docker build -t myhttpd:latest /root/apache/
[root@localhost ~]# docker images 
REPOSITORY        TAG       IMAGE ID       CREATED          SIZE
myhttpd           latest    d262e71e5af3   9 minutes ago    390MB
zhajigaga/httpd   v0.3      7e42e385caac   28 minutes ago   390MB
centos            latest    5d0da3dc9764   7 months ago     231MB

测试

curl命令测试

[root@localhost ~]# curl 192.168.220.145
<html><body><h1>It works!</h1></body></html>

使用镜像启动一个容器

[root@localhost ~]# docker run  -it --name web --rm  -p 80:80 myhttpd

浏览器上访问虚拟机ip地址
在这里插入图片描述

上传镜像到docker.hub仓库

[root@localhost ~]# docker login   //登录账号
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: zhajigaga
Password: 
Error response from daemon: Get "https://registry-1.docker.io/v2/": unauthorized: incorrect username or password
[root@localhost ~]# docker login 
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: zhajigaga
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

[root@localhost ~]# docker images 
REPOSITORY        TAG       IMAGE ID       CREATED          SIZE
myhttpd           latest    d262e71e5af3   13 minutes ago   390MB
zhajigaga/httpd   v0.3      7e42e385caac   32 minutes ago   390MB
zsh/httpd         v0.3      7e42e385caac   32 minutes ago   390MB
zsh/httpd         v0.2      15b6fc42c1be   59 minutes ago   422MB
centos            latest    5d0da3dc9764   7 months ago     231MB
[root@localhost ~]# docker tag d262e71e5af3 zhajigaga/httpd:latest  //修改镜像标签
[root@localhost ~]# docker push zhajigaga/httpd:latest   //上传
The push refers to repository [docker.io/zhajigaga/httpd]
d683dee2e44c: Pushed 
37ecbc6f4319: Pushed 
bf456cef5f10: Layer already exists 
78959fd97433: Layer already exists 
f659ac78b105: Layer already exists 
74ddd0ec08fa: Layer already exists 
latest: digest: sha256:c3e8c1b484fd1b5a5e84f784d676de1dc059b042e08199acec30052ec571d682 size: 1580

仓库内查看
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值