名称 | 版本 |
---|---|
系统 | CentOS Linux release 7.6.1810 (Core) |
Docker version | 1.13.1 |
思维:在官方提供的系统镜像上进行软件安装,配置
1使用Dockerfile构建新的镜像
Dockerfile语法 (详解参考https://www.cnblogs.com/dazhoushuoceshi/p/7066041.html):
FROM hub.c.163.com/library/centos #基础镜像
MAINTAINER ZHANGTAO #作者信息
ENV mytest=111 #设置变量
RUN yum install -y net-tools #需要安装的软件
RUN mkdir $mytest #使用变量创建文件夹
RUN useradd zhangtao;echo redhat |passwd --sdin zhangtao #添加用户并设置密码
VOLUME ["/1111","/222"] #容器生成/111和/222目录并分别挂载到物理机,无法指定固定的物理机目录
CMD ["命令1","命令2","命令3"] #指定容器运行时输入的命令
1.下载基础镜像
(详细参考https://www.cnblogs.com/jsonhc/p/7767669.html)
docker pull hub.c.163.com/library/centos:latest
2制作nginx镜像
mkdir /docker_demo
cd /docker_demo
wget http://nginx.org/download/nginx-1.14.2.tar.gz
3.创建 Dockerfile
touch Dockerfile
cat Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER 1785058042@qq.com
# put nginx-1.14.2.tar.gz into /usr/local/src and unpack nginx ,ADD添加压缩文件会自动解压,COPY不会解压
ADD nginx-1.14.2.tar.gz /usr/local/src
#COPY nginx-1.14.2.tar.gz /usr/local/src
# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# change dir to /usr/local/src/nginx-1.14.2
WORKDIR /usr/local/src/nginx-1.14.2
# execute command to compile nginx
RUN ./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 && make && make install
#添加环境变量
ENV PATH /usr/local/nginx/sbin:$PATH
#容器暴露端口
EXPOSE 80
#输入的命令
ENTRYPOINT ["nginx"]
#执行的参数,两句可以合并为CMD ["nginx","-g","daemon off;"] 此处为nginx的守护紧张
CMD ["-g","daemon off;"]
4构建
docker build -t centos_nginx:v1 /docker_demo #(最后目录为Dockerfile目录)
构建时出现key的报错,可以将repo源中gpgcheck=1改为gpgcheck=0
5构建成功后查看镜像
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v1 78d18f16e757 4 hours ago 464MB
6开启容器
docker run -d --name=mynginx -p 80:80 centos_nginx:v1
2使用保存修改后的容器,成为新的镜像
注意:容器导为镜像没有cmd指令,这个镜像启动不能指定cmd输入命令
docker run -d --name=mynginx -p 80:80 centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"
docker export mynginx >/mynginx.tar
cat /mynginx.tar |docker import - mynginx:v2
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mynginx v2 78d18f16e759 4 hours ago 465MB
centos_nginx v1 78d18f16e757 4 hours ago 464MB
网友评论