基于一个基础镜像启动一个容器,然后对这个容器进行更改,更改完毕后,进行提交。
容器启动
运行一个CentOS容器,命名为mynginx
[root@linux-node1 ~]# docker pull centos[root@linux-node1 ~]# docker run --name mynginx -it centos
在容器里面安装Nginx
[root@2e110e00eef4 ]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm[root@2e110e00eef4 ]# yum install -y nginx
想要Docker容器保持活跃的状态,需要其中运行的进程不能中断,默认情况下,Nginx会以守护进程的方式启动,这会导致容器只是短暂运行,在守护进程被fork启动后,发起守护进程的原始进程就会退出,这时容器就会停止运行了。所以我们需要将Nginx放在前端运行。
[root@2e110e00eef4 ]# vi etc/nginx/nginx.conf#在配置文件最上面增加下面配置daemon off;[root@2e110e00eef4 ]# exit
提交镜像
获取容器ID
[root@linux-node1 ~]# docker ps -a | grep mynginx2e110e00eef4 centos "/bin/bash" 8 minutes ago Exited (0) 2 minutes ago mynginx
提交修改后的容器为镜像
[root@linux-node1 ~]# docker commit -m "My Nginx" 2e110e00eef4 test/mynginx:v1sha256:cfd25da2c9c5dd2bcce9e5d2ef4e316b46b5f03617176b97b60a34f2958a6d70
-m:指定提交的说明信息,类似SVN和Git。
之后是用来创建镜像的容器的 ID;
最后指定目标镜像的仓库名和标签信息。
查看镜像
[root@linux-node1 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtest/mynginx v1 cfd25da2c9c5 49 seconds ago 373MBnginx latest f09fe80eb0e7 2 weeks ago 109MBcentos latest 1e1148e4cc2c 2 months ago 202MB
从我们创建的镜像运行一个容器
[root@linux-node1 ~]# docker run -d -p 91:80 test/mynginx:v1 nginx
文章转载自潍鲸,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




