千家信息网

docker怎么手动构建新镜像

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,本文小编为大家详细介绍"docker怎么手动构建新镜像",内容详细,步骤清晰,细节处理妥当,希望这篇"docker怎么手动构建新镜像"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知
千家信息网最后更新 2025年02月01日docker怎么手动构建新镜像

本文小编为大家详细介绍"docker怎么手动构建新镜像",内容详细,步骤清晰,细节处理妥当,希望这篇"docker怎么手动构建新镜像"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

查看本地现有镜像:

[root@docker ~]# docker imagesrepository     tag         image id      created       sizenginx        latest       c59f17fe53b0    4 days ago     108mbubuntu       latest       747cb2d60bbe    3 weeks ago     122mbcentos       latest       196e0ce0c9fb    6 weeks ago     197mb

现在利用基础镜像centos,在此基础上手动构建一个web服务,这里采用nginx

启动一个container并进入到容器内:

[root@docker ~]# docker run -it --name=web centos /bin/bash[root@bab3b6991467 /]#

然后在容器内进行安装nginx服务:

[root@bab3b6991467 /]# cd /usr/local/src/[root@bab3b6991467 src]# yum install wget vim

这里采用编译安装nginx,所以下载nginx源码包,并安装好编译环境:

[root@bab3b6991467 src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

编译环境:

[root@bab3b6991467 src]# yum install gcc gcc-c++ glibc make autoconf openssl openssl-devel

安装nginx的一些依赖包:

[root@bab3b6991467 src]# yum install libxslt-devel -y gd gd-devel geoip geoip-devel pcre pcre-devel

然后开支执行安装:

[root@bab3b6991467 src]# lltotal 960-rw-r--r--. 1 root root 981687 oct 17 13:20 nginx-1.12.2.tar.gz[root@bab3b6991467 src]# tar xf nginx-1.12.2.tar.gz [root@bab3b6991467 src]# cd nginx-1.12.2[root@bab3b6991467 nginx-1.12.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module  --with-http_addition_module  --with-http_xslt_module  --with-http_image_filter_module  --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module  --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module  --with-http_secure_link_module  --with-http_degradation_module  --with-http_stub_status_module

创建需要用到的用户:

useradd -m -s /sbin/nologin nginx

继续编译:

make && make installchown -r nginx:nginx /usr/local/nginx/

这里需要介绍nginx命令的一个参数:

[root@bab3b6991467 ~]# /usr/local/nginx/sbin/nginx -h   -g directives : set global directives out of configuration file

-g:为nginx的配置文件设置指令

现在退出container,回到host本机

[root@bab3b6991467 ~]# exitexit

查看此时容器的状态:

[root@docker ~]# docker ps -acontainer id    image        command       created       status           ports        namesbab3b6991467    centos       "/bin/bash"     37 minutes ago   exited (0) 21 seconds ago            web

利用docker diff查看该容器进行了哪些修改,由于输出太多,这里不给予显示了

利用docker commit将web容器进行加层成一个新镜像:

[root@docker ~]# docker commit --help  usage: docker commit [options] container [repository[:tag]]  create a new image from a container's changes  -m, --message string  commit message  -a, --author string  author (e.g., "john hannibal smith ")

现在开始commit:

[root@docker ~]# docker commit -m "compile nginx on centos" web wadeson/centos_nginx:v1sha256:210a202d37b8d2c31155c29adf0c7c0b49cfab7ff38234109919de7f4e76d1de

查看本地镜像:

[root@docker ~]# docker imagesrepository       tag         image id      created       sizewadeson/centos_nginx  v1         210a202d37b8    33 seconds ago   464mbnginx         latest       c59f17fe53b0    4 days ago     108mbubuntu         latest       747cb2d60bbe    3 weeks ago     122mbcentos         latest       196e0ce0c9fb    6 weeks ago     197mb

可以看见刚刚docker commit的新镜像了,现在由此镜像进行启动一个container提供nginx服务:

[root@docker ~]# docker run -d -p80:80 wadeson/centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"c12669357e2b09a05a396ac480a04dd1956303b784f894b615d4edb889a737ab

然后查看container:

[root@docker ~]# docker ps -lcontainer id    image           command         created       status       ports        namesc12669357e2b    wadeson/centos_nginx:v1  "/usr/local/nginx/..."  41 seconds ago   up 40 seconds    0.0.0.0:80->80/tcp  thirsty_murdock

可以看见nginx服务已经开启了,于是进行访问:

于是整个手动构建就成功了

针对上面的一些命令做下解释:

docker run -d -p80:80 wadeson/centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"

后面运行的命令都是旨在container的命令,由于没有进行环境变量设置,所以全路径,而nginx -g这个参数是指可以在外面添加指令到nginx的配置文件中,daemon off是指nginx服务不运行在后端而是在前台运行(container中的服务必须运行在前台)

利用docker top可以查看container的运行进程:

[root@docker ~]# docker top c12669357e2buid         pid         ppid        c          stime        tty         time        cmdroot        35468        35451        0          02:55        ?          00:00:00      nginx: master process /usr/local/nginx/sbin/nginx -g daemon off;1000        35489        35468        0          02:55        ?          00:00:00      nginx: worker process

利用docker exec进入到容器内:

[root@docker ~]# docker exec -it c12669357e2b /bin/bash[root@c12669357e2b /]# ps -efuid     pid  ppid c stime tty     time cmdroot     1   0 0 06:55 ?    00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off;nginx     5   1 0 06:55 ?    00:00:00 nginx: worker processroot     6   0 1 07:01 pts/0  00:00:00 /bin/bashroot     20   6 0 07:01 pts/0  00:00:00 ps -ef

而使用ctrl+p+q可以将该容器置于后台,而不是马上exited

读到这里,这篇"docker怎么手动构建新镜像"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。

镜像 容器 服务 手动 运行 命令 编译 文章 环境 内容 前台 参数 基础 指令 文件 配置 妥当 成功 变量 后台 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 进行某系统数据库设计和实现 华为软件开发类岗位有哪些 优房网络技术有限公司怎么样 十大数据库名字 dna导入数据库吗 u8安装服务器必须安装数据库吗 外网访问内网服务器安全审计 马斯克评价中国互联网科技 济南汇邦互联网科技有限公司 网络安全图书目录 邮储银行软件开发怎么样 无人机实时视频传输到服务器 广州火柴人网络技术有限公司 永兴学电脑软件开发工资 sql收缩数据库和收缩文件 如何做网络安全教育的ppt 服务器带宽压力测试 青少年网络安全意识教育 共享智能锁系统软件开发 入侵数据库的手段 天龙八部单机数据库 内蒙古自治区网络安全知识有奖 商务服务软件开发 mc进入服务器要花钱吗 亚信科技软件开发待遇如何 什么是交换机和服务器 网页连数据库jsp 中俄网络安全谅解备忘录 dmz区服务器怎么才能安全 网络安全宣传周司法局
0