参考:https://www.jianshu.com/p/dc4cd0547d1e
一、基于容器制作镜像
拉取centos镜像
docker pull centos
创建容器实例
docker run --name mycentos -it centos
容器内环境
安装nginx
yum install epel-release -y
yum install nginx -y
vi /etc/nginx/nginx.conf 添加daemon off;
退出并提交容器实例到新的镜像中
docker commit -c 'CMD ["/usr/sbin/nginx"]' f409f5063ce1 localhost:5000/nginx:v2
运行并测试
docker run localhost:5000/nginx:v2
或docker run --name nginx-test -p 80:80 -d localhost:5000/nginx:v2
访问宿主机地址:
curl localhost
直接访问容器地址:
docker inspect nginx-test |grep IPAddress 查出容器的IP地址
curl IP地址 访问nginx默认首页
进入容器查看进程:
docker exec -it nginx-test sh
ps -ef|grep nginx
二、基于Dockerfile制作
vi dockerfile
# 来源基础镜像
FROM centos:latest
# 作者信息
MAINTAINER "hehe <hehe@hehe.com>"
# 构建时运行的命令
RUN yum install epel-release -y
RUN yum install nginx -y
RUN sed -i '/pid \/run\/nginx.pid;/a\daemon off;' /etc/nginx/nginx.conf
# 容器启动时执行的命令,一般用来打开前台程序
CMD /usr/sbin/nginx
构建:
docker build ./ -t localhost:5000/nginx:v3
查看是否已经创建:
docker images|grep v3|grep nginx
运行容器:
docker run --name nginx-test -p 80:80 -d localhost:5000/nginx:v3
测试:
curl localhost
注意事项:
1.前面两种方式,最后要push到仓库,否则客户端将无法pull
2.k8s利用该镜像创建nginx应用,请参考https://www.jianshu.com/p/6bcb3c3545c5,image地址修改成私有仓库地址即可。