文章目录
- 手动制作Docker容器镜像
- 说明
- 前期准备
- 制作镜像
- 1.启动一个centos系统的容器
- 2.在centos容器中源码安装httpd服务
- 3.基于已经安装好httpd服务的centos容器制作一个httpd镜像
- 4.验证制作出来的镜像的功能
- 5.上传至自己的docker镜像仓库(可选)
手动制作Docker容器镜像
说明
基于centos镜像启动一个容器,在这个容器里面源码安装一个httpd服务,并写一个简单的网页。再把这个运行了httpd服务的容器制作成一个镜像。这样以后直接把这个制作的镜像运行起来,就可以运行一个httpd服务。
前期准备
需要在主机上安装docker。如何安装docker请阅读Docker基础
还需要注册一个docker账号(上传镜像需要)docker官网
制作镜像
1.启动一个centos系统的容器
//拉取centos镜像
[root@wanf ~]# docker pull centos
[root@wanf ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB
//基于centos镜像运行一个centos容器,并进入该容器
[root@wanf ~]# docker run --name centos -it centos
[root@7c01047df948 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@7c01047df948 /]#
2.在centos容器中源码安装httpd服务
在centos容器里面的操作与在centos主机上操作一样
源码编译安装httpd
//配置国内yum源
[root@7c01047df948 ~]# rm -rf /etc/yum.repos.d/*
[root@7c01047df948 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@7c01047df948 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@7c01047df948 ~]# yum clean all
[root@7c01047df948 ~]# yum makecache
[root@7c01047df948 ~]#
//安装依赖包和工具包
[root@7c01047df948 ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make wget
//下载源码包
[root@7c01047df948 ~]# wget https://downloads.apache.org/apr/apr-1.7.4.tar.gz -P /usr/src/
[root@7c01047df948 ~]# wget https://downloads.apache.org/apr/apr-util-1.6.3.tar.gz -P /usr/src/
[root@7c01047df948 ~]# wget http://archive.apache.org/dist/httpd/httpd-2.4.57.tar.gz -P /usr/src/
//开始编译安装
[root@7c01047df948 ~]# cd /usr/src/
[root@7c01047df948 src]# tar -xf apr-1.7.4.tar.gz
[root@7c01047df948 src]# cd apr-1.7.4
[root@7c01047df948 apr-1.7.4]# sed -i 's/$RM "$cfgfile"/#$RM "$cfgfile"/g' configure
[root@7c01047df948 apr-1.7.4]# ./configure --prefix=/usr/local/apr
[root@7c01047df948 apr-1.7.4]# make -j4 && make install
[root@7c01047df948 apr-1.7.4]# cd ..
[root@7c01047df948 src]# tar -xf apr-util-1.6.3.tar.gz
[root@7c01047df948 src]# cd apr-util-1.6.3
[root@7c01047df948 apr-util-1.6.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@7c01047df948 apr-util-1.6.3]# make -j4 && make install
[root@7c01047df948 apr-util-1.6.3]# cd ..
[root@7c01047df948 src]# groupadd -r apache
[root@7c01047df948 src]# useradd -r -M -s /sbin/nologin -g apache apache
[root@7c01047df948 src]# tar -xf httpd-2.4.57.tar.gz
[root@7c01047df948 src]# cd httpd-2.4.57
[root@7c01047df948 httpd-2.4.57]# ./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
(过程省略)
[root@7c01047df948 httpd-2.4.57]# make -j4 && make install
(过程省略)
//配置并启动httpd
[root@7c01047df948 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@7c01047df948 ~]# bash
[root@7c01047df948 ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@7c01047df948 ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
[root@7c01047df948 ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf
//准备一个简单的静态web页面
[root@7c01047df948 ~]# vi /usr/local/apache/htdocs/index.html
[root@7c01047df948 ~]# cat /usr/local/apache/htdocs/index.html
<html><body><h1>nice moon!</h1></body></html>
[root@7c01047df948 ~]# /usr/local/apache/bin/apachectl start
//删除安装时用到的源码包
[root@7c01047df948 ~]# cd /usr/src/
[root@7c01047df948 src]# ls
apr-1.7.4 apr-util-1.6.3 debug httpd-2.4.57.tar.gz
apr-1.7.4.tar.gz apr-util-1.6.3.tar.gz httpd-2.4.57 kernels
[root@7c01047df948 src]# rm -rf a* h*
[root@7c01047df948 src]# ls
debug kernels
[root@7c01047df948 src]#
3.基于已经安装好httpd服务的centos容器制作一个httpd镜像
建议另起一个终端进行制作镜像的操作
lcwanf/httpd:v0.1的含义:lcwanf是我的docker账号名字,httpd是镜像名字,v0.1是版本号。如果需要上传到自己的docker仓库的话就需要加上lcwanf/
//制作镜像
[root@wanf ~]# docker commit -a 'lcwanf' -c 'CMD ["/usr/local/apache/bin/httpd","-X","-D","FOREGROUND"]' -p centos lcwanf/httpd:v0.1
[root@wanf ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lcwanf/httpd v0.1 d078f9fc7d54 35 seconds ago 574MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@wanf ~]#
4.验证制作出来的镜像的功能
运行这个容器,并映射到真机的8080端口号
//查看镜像id
[root@wanf ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lcwanf/httpd v0.1 62551f713247 4 minutes ago 574MB
centos latest 5d0da3dc9764 2 years ago 231MB
//运行容器
[root@wanf ~]# docker run -d -p 8080:80 --name httpd -it 62551f713247
fd52d76f603c94d9d1310f6b8925b107045728f468c63eb22d9c8036831c1ba1
//容器正在运行
[root@wanf ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd52d76f603c 62551f713247 "/usr/local/apache/b…" 8 seconds ago Up 7 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp httpd
//端口映射成功
[root@wanf ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 2048 0.0.0.0:8080 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 2048 [::]:8080 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@wanf ~]#
在浏览器访问测试
成功访问到
5.上传至自己的docker镜像仓库(可选)
//登录docker账号
[root@wanf ~]# docker login
Log in with your Docker ID or email address 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.
You can log in with your password or a Personal Access Token (PAT). Using a limited-scope PAT grants better security and is required for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/
Username: lcwanf //输入自己的用户名
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@wanf ~]#
//上传镜像
[root@wanf ~]# docker push lcwanf/httpd:v0.1
(耐心等待...)
在docker官网登录自己的账号,在镜像仓库里面查看
上传完毕