基于Alpine镜像编写Dockerfile
想了解Alpine请移步至👉Alpine Linux详解
项目目录
[root@localhost alpine_httpd]# tree .
.
├── Dockerfile
└── files
├── apr-1.6.5.tar.gz
├── apr-util-1.6.1.tar.gz
└── httpd-2.4.54.tar.gz
编写Dockerfile
[root@localhost alpine_httpd]# vim Dockerfile
# This is a Httpd image based on alpine
# Author yefeng
from alpine
LABEL MAINTAINER='yefeng yf021121@163.com'
ENV apr_version=1.6.5 apr_util_version=1.6.1 httpd_version=2.4.54
ENV PATH /usr/local/apache/bin:$PATH
ADD files/* /tmp/
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
apk update && \
apk add --no-cache -U gcc libc-dev expat-dev pcre-dev openssl-dev make && \
cd /tmp && \
cd /tmp/apr-${apr_version} && \
sed -i '/$RM "$cfgfile"/d' configure &&\
./configure --prefix=/usr/local/apr && \
make && make install && \
cd /tmp/apr-util-${apr_util_version} && \
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
make && make install && \
cd /tmp/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 && make install && \
apk del gcc make && \
rm -rf /var/cache/* && \
rm -rf /tmp/*
WORKDIR /usr/local/apache
EXPOSE 80
CMD ["httpd","-D","FOREGROUND"]
创建&上传镜像
创建镜像
[root@localhost ~]# docker build -t httpd:v4 .
测试镜像
[root@localhost ~]# docker run -d --name web1 -P httpd:v4
[root@localhost ~]# curl 172.17.0.3
<html><body><h1>It works!</h1></body></html>
上传镜像
[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: guguniao
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 tag httpd:v5 guguniao/httpd:v4
[root@localhost ~]# docker push guguniao/httpd:v4
The push refers to repository [docker.io/guguniao/httpd]
6318eddfaf8c: Pushed
d6b343796082: Pushed
8d3ac3489996: Pushed
v4: digest: sha256:32cdfb207bf8c833e26c0d868a071b613f1f86cc5c10b3b8e6cad6322e3f8578 size: 952
[root@localhost ~]# docker logout
Removing login credentials for https://index.docker.io/v1/
部署网站(拓展)
利用上面创建好的镜像运行一个容器,在该容器里部署网站
项目目录
[root@localhost alpine_httpd]# tree
.
├── Dockerfile
└── files
├── apr-1.6.5.tar.gz
├── apr-util-1.6.1.tar.gz
├── chishenme.tar.gz #这个是网站的源码包
└── httpd-2.4.54.tar.gz
编写Dockerfile
[root@localhost alpine_httpd]# cat Dockerfile
# This is a Httpd image based on alpine
# Author yefeng
from alpine
LABEL MAINTAINER='yefeng yf021121@163.com'
ENV apr_version=1.6.5 apr_util_version=1.6.1 httpd_version=2.4.54
ENV PATH /usr/local/apache/bin:$PATH
ADD files/* /tmp/
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
apk update && \
apk add --no-cache -U gcc libc-dev expat-dev pcre-dev openssl-dev make && \
cd /tmp && \
cd /tmp/apr-${apr_version} && \
sed -i '/$RM "$cfgfile"/d' configure &&\
./configure --prefix=/usr/local/apr && \
make && make install && \
cd /tmp/apr-util-${apr_util_version} && \
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
make && make install && \
cd /tmp/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 && make install && \
sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf && \
rm -f /usr/local/apache/htdocs/index.html && \
mv /tmp/chishenme/* /usr/local/apache/htdocs/ && \
apk del gcc make && \
rm -rf /var/cache/* && \
rm -rf /tmp/*
WORKDIR /usr/local/apache
EXPOSE 80
CMD ["httpd","-D","FOREGROUND"]
创建镜像
[root@localhost ~]# docker build -t httpd:v4.2 .
测试
[root@localhost ~]# docker run -d --name web1 -P httpd:v4.2
浏览器访问
上传镜像至Docker Hub
[root@localhost ~]# docker tag httpd:v4.2 guguniao/httpd:v4.2
[root@localhost ~]# docker login
[root@localhost ~]# docker push guguniao/httpd:v4.2
[root@localhost ~]# docker logout