一、建立Dockerfile文件
vim Dockerfilenginx
#設置基本的CentOS7鏡像,後續指令都以這個鏡像爲基礎 FROM centos:latest #做者信息 MAINTAINER cjh #安裝依賴工具&刪除默認YUM源,使用YUM源爲國內163 YUM源; RUN rpm --rebuilddb;yum install make wget tar gzip passwd openssh-server gcc pcre-devel openssl-devel net-tools vim -y RUN rm -rf /etc/yum.repos.d/*;wget -P /etc/yum.repos.d/ http://mirrors.163.com/.help/CentOS7-Base-163.repo #配置SSHD&修改root密碼爲Mufeng07 RUN ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N '' RUN ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' RUN ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ED25519_key -N '' RUN echo 'Mufeng07' | passwd --stdin root #Nginx官網下載Nginx最新版本軟件; RUN wget -P /tmp/ http://nginx.org/download/nginx-1.14.2.tar.gz #解壓Nginx軟件包,隱藏WEB服務器版本號; RUN cd /tmp/;tar xzf nginx-1.14.2.tar.gz;cd nginx-1.14.2;sed -i -e 's/1.14.2//g' -e 's/nginx\//WS/g' -e 's/"NGINX"/"WS"/g' src/core/nginx.h #基於源碼安裝,建立配置文件; RUN cd /tmp/nginx-1.14.2;./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module;make;make install #啓動Nginx服務進程,對外暴露22和80端口; EXPOSE 22 EXPOSE 80 CMD /usr/local/nginx/sbin/nginx;/usr/sbin/sshd -D
二、生成鏡像docker
docker build -t centos7:nginx .
三、建立容器
#基於centos7:nginx鏡像建立容器vim
docker run -itd --privileged -p 80:80 -p 2201:22 --name=mynginx centos7:nginx
#進入容器centos
docker exec -it mynginx /bin/bash