docker file 编译安装apache

[root@localhost yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost yum.repos.d]# ls		//配置好源,和docker的安装包
CentOS-Base.repo  epel-modular.repo  epel-testing-modular.repo
docker-ce.repo    epel.repo          epel-testing.repo
[root@localhost yum.repos.d]# dnf -y install --allowerasing docker-ce
//安装docker
[root@localhost ~]#  systemctl start docker
//启动docker
[root@localhost ~]# docker version		//看docker的版本
Client: Docker Engine - Community
 Version:           20.10.14
 API version:       1.41
 Go version:        go1.16.15
 Git commit:        a224086
 Built:             Thu Mar 24 01:47:44 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@localhost ~]# cd /etc/docker/		//进到这个目录下配置加速器
[root@localhost docker]# ls
key.json
[root@localhost docker]# vi daemon.json
[root@localhost docker]# cat daemon.json 
{
  "registry-mirrors": ["https://6somtdrl.mirror.aliyuncs.com"]
}

dockerfile部署
准备工作

[root@localhost ~]# mkdir apache
[root@localhost ~]# cd apache/
[root@localhost apache]# mkdir files
[root@localhost apache]# touch Dockerfile
[root@localhost apache]# cd files/
[root@localhost files]# wget https://downloads.apache.org/httpd/httpd-2.4.53.tar.gz https://downloads.apache.org/apr/apr-1.7.0.tar.gz https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz

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

编辑Dockerfile

[root@localhost apache]# vim Dockerfile 
[root@localhost apache]# cat Dockerfile 
FROM centos
  
LABEL MAINTAINER='test 1@2.com'  //标签

ENV apr_version=1.7.0 apr_util_version=1.6.1 httpd_version=2.4.53  //

ADD files/* /usr/src/

RUN rm -rf /etc/yum.repos.d/* &&\   //删除yum仓库内的国外源
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/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

WORKDIR /usr/local/apache  //指定工作路径

EXPOSE 80   //暴露80端口号

ENTRYPOINT /usr/local/apache/bin/apachectl  -DFOREGROUND   //启动容器时默认启动apache并前台运行

制作镜像

[root@localhost apache]# docker build -t test/httpd:v0.1 /root/apache/  //制作镜像
[root@localhost apache]# docker images   //查看镜像
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
test/httpd   v0.1      93e3dba29a33   16 minutes ago   717MB
centos       latest    5d0da3dc9764   7 months ago     231MB

测试

[root@localhost apache]# docker run -it --rm --name web -p 80:80 test/httpd:v0.1   //用镜像生成容器并映射到宿主机的80端口
5269457374c6da133ff86cfe4d323a5dbf0e93b1cdc36d94b8fd70808da7b23b
[root@localhost apache]# curl 192.168.220.145  //访问
<html><body><h1>It works!</h1></body></html>

上传镜像
改标签

[root@localhost ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
test/httpd   v0.1      93e3dba29a33   37 minutes ago   717MB
centos       latest    5d0da3dc9764   7 months ago     231MB
[root@localhost ~]# docker tag 93e3dba29a33 feigeaq/httpd:v0.1
[root@localhost ~]# docker images 
REPOSITORY        TAG       IMAGE ID       CREATED          SIZE
feigeaq/httpd   v0.1      93e3dba29a33   38 minutes ago   717MB
test/httpd        v0.1      93e3dba29a33   38 minutes ago   717MB
centos            latest    5d0da3dc9764   7 months ago     231MB

上传镜像

[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: feigeaq
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: feigeaq
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 push feigeaq/httpd:v0.1   //上传
The push refers to repository [docker.io/feigeaq/httpd]
fa35feaac5d8: Pushed 
941f152fe46a: Pushed 
2d51f5fffa0a: Pushed 
a49661350957: Pushed 
74ddd0ec08fa: Mounted from library/centos 
v0.1: digest: sha256:32e47ce787297e545d25d9444ea737008b99d67fba931c487dcc330dd0aaf040 size: 1368

posted @ 2022-05-05 23:57  姜翎  阅读(128)  评论(0)    收藏  举报