docker怎么手动构建新镜像(docker,web开发)

时间:2024-04-28 11:50:43 作者 : 石家庄SEO 分类 : web开发
  • TAG :

查看本地现有镜像:

[root@docker~]#dockerimagesrepositorytagimageidcreatedsizenginxlatestc59f17fe53b04daysago108mbubuntulatest747cb2d60bbe3weeksago122mbcentoslatest196e0ce0c9fb6weeksago197mb

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

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

[root@docker~]#dockerrun-it--name=webcentos/bin/bash[root@bab3b6991467/]#

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

[root@bab3b6991467/]#cd/usr/local/src/[root@bab3b6991467src]#yuminstallwgetvim

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

[root@bab3b6991467src]#wgethttp://nginx.org/download/nginx-1.12.2.tar.gz

编译环境:

[root@bab3b6991467src]#yuminstallgccgcc-c++glibcmakeautoconfopensslopenssl-devel

安装nginx的一些依赖包:

[root@bab3b6991467src]#yuminstalllibxslt-devel-ygdgd-develgeoipgeoip-develpcrepcre-devel

然后开支执行安装:

[root@bab3b6991467src]#lltotal960-rw-r--r--.1rootroot981687oct1713:20nginx-1.12.2.tar.gz[root@bab3b6991467src]#tarxfnginx-1.12.2.tar.gz[root@bab3b6991467src]#cdnginx-1.12.2[root@bab3b6991467nginx-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/nologinnginx

继续编译:

make&&makeinstallchown-rnginx:nginx/usr/local/nginx/

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

[root@bab3b6991467~]#/usr/local/nginx/sbin/nginx-h  -gdirectives:setglobaldirectivesoutofconfigurationfile

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

现在退出container,回到host本机

[root@bab3b6991467~]#exitexit

查看此时容器的状态:

[root@docker~]#dockerps-acontaineridimagecommandcreatedstatusportsnamesbab3b6991467centos"/bin/bash"37minutesagoexited(0)21secondsagoweb

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

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

[root@docker~]#dockercommit--helpusage:dockercommit[options]container[repository[:tag]]createanewimagefromacontainer'schanges-m,--messagestringcommitmessage-a,--authorstringauthor(e.g.,"johnhannibalsmith<hannibal@a-team.com>")

现在开始commit:

[root@docker~]#dockercommit-m"compilenginxoncentos"webwadeson/centos_nginx:v1sha256:210a202d37b8d2c31155c29adf0c7c0b49cfab7ff38234109919de7f4e76d1de

查看本地镜像:

[root@docker~]#dockerimagesrepositorytagimageidcreatedsizewadeson/centos_nginxv1210a202d37b833secondsago464mbnginxlatestc59f17fe53b04daysago108mbubuntulatest747cb2d60bbe3weeksago122mbcentoslatest196e0ce0c9fb6weeksago197mb

docker怎么手动构建新镜像

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

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

然后查看container:

[root@docker~]#dockerps-lcontaineridimagecommandcreatedstatusportsnamesc12669357e2bwadeson/centos_nginx:v1"/usr/local/nginx/..."41secondsagoup40seconds0.0.0.0:80->80/tcpthirsty_murdock

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

docker怎么手动构建新镜像

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

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

dockerrun-d-p80:80wadeson/centos_nginx:v1/usr/local/nginx/sbin/nginx-g"daemonoff;"

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

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

[root@docker~]#dockertopc12669357e2buidpidppidcstimettytimecmdroot3546835451002:55?00:00:00nginx:masterprocess/usr/local/nginx/sbin/nginx-gdaemonoff;10003548935468002:55?00:00:00nginx:workerprocess

利用docker exec进入到容器内:

[root@docker~]#dockerexec-itc12669357e2b/bin/bash[root@c12669357e2b/]#ps-efuidpidppidcstimettytimecmdroot10006:55?00:00:00nginx:masterprocess/usr/local/nginx/sbin/nginx-gdaemonoff;nginx51006:55?00:00:00nginx:workerprocessroot60107:01pts/000:00:00/bin/bashroot206007:01pts/000:00:00ps-ef

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

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:docker怎么手动构建新镜像的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:docker如何使用Dockerfile构建镜像下一篇:

2 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18