Bootstrap

Docker 用Dockerfile部署Apache网站

==========================================================================================

//创建apache服务的dockerfile文件目录

[root@localhost ~]# mkdir httpd/files -p

//上传源码包

[root@localhost ~]# ls httpd/files

apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.51.tar.gz

//项目结构

[root@localhost ~]# tree

.

|-- anaconda-ks.cfg

`-- httpd

|-- Dockerfile

`-- files

|-- apr-1.7.0.tar.gz

|-- apr-util-1.6.1.tar.gz

|-- httpd-2.4.51.tar.gz

`-- install.sh

2 directories, 6 files

//添加脚本权限

[root@localhost ~]# touch httpd/files/install.sh

[root@localhost ~]# chmod +x httpd/files/install.sh

[root@localhost ~]# ll httpd/files/install.sh

-rwxr-xr-x. 1 root root 1444 Dec 10 18:37 httpd/files/install.sh

//编写dockerfile

[root@localhost ~]# vim httpd/Dockerfile

[root@localhost ~]# cat httpd/Dockerfile

FROM centos

LABEL MAINTAINER=‘1314444 [email protected]

#定义变量

ENV apr_version 1.7.0

ENV apr_util_version 1.6.1

ENV httpd_version 2.4.51

#环境变量

ENV PATH /usr/local/apache/bin:$PATH

#把需要安装apache的源码包传输到/usr/src下

COPY files /usr/src/

#运行安装脚本

RUN [“/bin/bash”,“-c”,“/usr/src/install.sh”]

#暴露端口号

EXPOSE 80 443

#安装路径

WORKDIR /usr/local/apache

#传递给ENTRYPOINT的参数

CMD [“-D”,“FOREGROUND”]

#主程序

ENTRYPOINT [“httpd”]

//脚本内容

[root@localhost ~]# cat httpd/files/install.sh

rm -rf /etc/yum.repos.d/*

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-$(awk -F’"’ ‘NR==5{print $2}’ /etc/os-release).repo

sed -i -e ‘/mirrors.cloud.aliyuncs.com/d’ -e ‘/mirrors.aliyuncs.com/d’ /etc/yum.repos.d/CentOS-Base.repo

yum clean all && yum makecache

echo “alias ls=‘ls --color’” >> ~/.bashrc

source ~/.bashrc

yum -y install openssl-devel pcre-devel pcre expat-devel libtool gcc gcc-c++ make

useradd -r -M -s /sbin/nologin apache

cd /usr/src/

tar xf apr-${apr_version}.tar.gz

tar xf apr-util-${apr_util_version}.tar.gz

tar xf httpd-${httpd_version}.tar.gz

cd apr-${apr_version}

sed -i ‘/ R M " RM " RM"cfgfile"/d’ configure

./configure --prefix=/usr/local/apr && \

make -j $(nproc)&& \

make install && \

cd …/apr-util-${apr_util_version} && \

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \

make -j $(nproc) && \

make install && \

cd …/httpd-${httpd_version} && \

./configure --prefix=/usr/local/apache \

–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 -j $(nproc)&& \

make install && \

sed -i ‘/#ServerName/s/#//g’ /usr/local/apache/conf/httpd.conf

yum -y remove gcc gcc-c++ make

rm -rf /usr/src/* /var/cache/*

//制作镜像

[root@localhost ~]# docker build -t httpd:v0.1 httpd

Sending build context to Docker daemon 11.53MB

Step 1/12 : FROM centos

—> 5d0da3dc9764

Step 2/12 : LABEL MAINTAINER=‘1314444 [email protected]

—> Using cache

—> a8b67caa2102

Step 3/12 : ENV apr_version 1.7.0

—> Using cache

—> 0e34b832cb4f

Step 4/12 : ENV apr_util_version 1.6.1

—> Using cache

—> afb0ed0d7b75

Step 5/12 : ENV httpd_version 2.4.51

—> Using cache

—> 5c75f5c66090

Step 6/12 : ENV PATH /usr/local/apache/bin:$PATH

—> Using cache

—> 42d9a10cd81f

Step 7/12 : COPY files /usr/src/

—> Using cache

—> a872f04851c7

Step 8/12 : RUN [“/bin/bash”,“-c”,“/usr/src/install.sh”]

—> Using cache

—> 0e66941d63f0

Step 9/12 : EXPOSE 80 443

—> Using cache

—> e42572634c04

Step 10/12 : WORKDIR /usr/local/apache

—> Using cache

—> 75e478ea8d96

Step 11/12 : CMD [“-D”,“FOREGROUND”]

—> Using cache

—> b647e6a08699

Step 12/12 : ENTRYPOINT [“httpd”]

—> Using cache

—> 3eee02b930f6

Successfully built 3eee02b930f6

Successfully tagged httpd:v0.1

//查看镜像

[root@localhost ~]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

1314444/httpd v0.1 0e0d1f4bd106 10 seconds ago 372MB

//基于新镜像创建容器

[root@localhost ~]# docker run --name httpd02 -dit -p 80:80 1314444/httpd:v0.1

be35164fcf4676117558f96c164980f188246167ee4e70d949e91f391ae71ad2

[root@localhost ~]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

be35164fcf46 1314444/httpd:v0.1 “httpd -D FOREGROUND” 5 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp httpd02

[root@localhost ~]# docker exec -it httpd02 /bin/bash

[root@be35164fcf46 /]# ss -anlt

State Recv-Q Send-Q Local Address:Port Peer Address:Port Process

LISTEN 0 128 0.0.0.0:80 0.0.0.0:*

在这里插入图片描述

上传镜像仓库

[root@localhost ~]# docker login

Authenticating with existing credentials…

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

1314444/httpd v0.1 0e0d1f4bd106 4 minutes ago 703MB

[root@localhost ~]# docker push 1314444/httpd:v0.1

The push refers to repository [docker.io/1314444/httpd]

0afddf7e7a57: Pushed

49d8e5c1af37: Pushed

74ddd0ec08fa: Layer already exists

v0.1: digest: sha256:20337cdfd55de2cd1b87f796661535d6c97e6b837c03cb1390efe01a43cca342 size: 954

基于Alpine 编写Apache的Dockerfile(精简版)

================================================================================================

//创建apache服务的dockerfile文件目录

[root@localhost ~]# mkdir httpd/files -p

//上传源码包

[root@localhost ~]# ls httpd/files

apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.51.tar.gz

//项目结构

[root@localhost ~]# tree

.

|-- anaconda-ks.cfg

`-- httpd

|-- Dockerfile

`-- files

|-- apr-1.7.0.tar.gz

|-- apr-util-1.6.1.tar.gz

|-- httpd-2.4.51.tar.gz

`-- install.sh

2 directories, 6 files

//添加脚本权限

[root@localhost ~]# touch httpd/files/install.sh

[root@localhost ~]# chmod +x httpd/files/install.sh

[root@localhost ~]# ll httpd/files/install.sh

-rwxr-xr-x. 1 root root 1444 Dec 10 18:37 httpd/files/install.sh

//编写dockerfile

[root@localhost ~]# vim httpd/Dockerfile

[root@localhost ~]# cat httpd/Dockerfile

FROM alpine

LABEL MAINTAINER=‘1314444 [email protected]

#定义变量

ENV apr_version 1.7.0

ENV apr_util_version 1.6.1

ENV httpd_version 2.4.51

#环境变量

ENV PATH /usr/local/apache/bin:$PATH

#把需要安装apache的源码包传输到/tmp/下

COPY files /tmp/

#运行安装脚本(alpine这个系统不支持/bin/bash)

RUN /tmp/install.sh

#暴露端口号

EXPOSE 80 443

#安装路径

WORKDIR /usr/local/apache

#传递给ENTRYPOINT的参数

CMD [“-D”,“FOREGROUND”]

#主程序

ENTRYPOINT [“httpd”]

//脚本内容

[root@localhost ~]# cat httpd/files/install.sh

#!/bin/sh

sed -i ‘s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/’ /etc/apk/repositories

apk update

apk add --no-cache -U openssl-dev pcre-dev expat-dev libtool libc-dev gcc make

adduser -S -H -s /sbin/nologin apache

cd /tmp/

tar xf apr-${apr_version}.tar.gz

tar xf apr-util-${apr_util_version}.tar.gz

tar xf httpd-${httpd_version}.tar.gz

cd apr-${apr_version}

sed -i ‘/ R M " RM " RM"cfgfile"/d’ configure

./configure --prefix=/usr/local/apr && \

make -j $(nproc)&& \

make install && \

;