- dockerfile部署
- 准备工作
- 编辑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='zjgg 1@2.com' //标签
ENV apr_version=1.7.0 apr_util_version=1.6.1 httpd_version=2.4.53
ADD files/* /usr/src/ //将本地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 //使用镜像启动容器时 默认目录为apache
EXPOSE 80 //暴露80端口号
ENTRYPOINT /usr/local/apache/bin/apachectl -DFOREGROUND //启动容器时默认启动apache并前台运行
制作镜像
[root@localhost apache]# docker build -t zjgg/httpd:v0.1 /root/apache/ //制作镜像 [root@localhost apache]# docker images //查看镜像 REPOSITORY TAG IMAGE ID CREATED SIZE zjgg/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 zjgg/httpd:v0.1 //用镜像生成容器并映射到宿主机的80端口 5269457374c6da133ff86cfe4d323a5dbf0e93b1cdc36d94b8fd70808da7b23b [root@localhost apache]# curl 192.168.220.145 //访问It works!
真机访问虚拟机ip
#上传镜像
修改标签
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zjgg/httpd v0.1 93e3dba29a33 37 minutes ago 717MB centos latest 5d0da3dc9764 7 months ago 231MB [root@localhost ~]# docker tag 93e3dba29a33 zhajigaga/httpd:v0.1 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zhajigaga/httpd v0.1 93e3dba29a33 38 minutes ago 717MB zjgg/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: 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 push zhajigaga/httpd:v0.1 //上传 The push refers to repository [docker.io/zhajigaga/httpd] fa35feaac5d8: Pushed 941f152fe46a: Pushed 2d51f5fffa0a: Pushed a49661350957: Pushed 74ddd0ec08fa: Mounted from library/centos v0.1: digest: sha256:32e47ce787297e545d25d9444ea737008b99d67fba931c487dcc330dd0aaf040 size: 1368
浏览器访问docker.hub 进入仓库查看
https://hub.docker.com/repository/docker/zhajigaga/httpd



