前言:
dockerfile包含,基础镜像信息,维护者信息,镜像操作命令,容器启动时执行指令
1,创建一个目录
[root@localhost ~]# mkdir dockerfile
[root@localhost ~]# cd dockerfile && mkdir nginx && cd nginx
2,创建Dockerfile文件
Dockerfile这个文件,D必须是大写
[root@gitlab nginx]# cat Dockerfile
######################################################################
# is my nginx dockerfile
# Version 1.0
# Author: www.linuxea.com
# Base on centos
#######################################################################
FROM kriation/centos7
MAINTAINER www.linuxea.com
############################BEGIN INSTALL##############################
#ADD nginx1.10.1.tar.gz /usr/local/src
#RUN yum install wget -y
#RUN wget -P /usr/local/src http://nginx.org/download/nginx-1.10.1.tar.gz && tar xf
ADD http://nginx.org/download/nginx-1.10.1.tar.gz /usr/local/src
RUN cd /usr/local/src && tar xf nginx-1.10.1.tar.gz
RUN yum install epel* -y && yum clean all && yum makecache
RUN yum install -y gcc gcc-c++ make openssl-devel pcre pcre-devel
RUN useradd -s /sbin/nologin -M nginx
WORKDIR /usr/local/src/nginx-1.10.1
RUN ./configure
--prefix=/usr/local/nginx
--conf-path=/etc/nginx/nginx.conf
--user=nginx
--group=nginx
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nginx/nginx.pid
--lock-path=/var/lock/nginx.lock
--with-http_ssl_module
--with-http_stub_status_module
--with-http_gzip_static_module
--with-http_flv_module
--with-http_mp4_module
--http-client-body-temp-path=/var/tmp/nginx/client
--http-proxy-temp-path=/var/tmp/nginx/proxy
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
RUN make && make install
RUN mkdir -p /var/tmp/nginx/{client,fastcgi,proxy,uwsgi}
RUN rm -v /usr/local/nginx/html/index.html
RUN echo "<h1>HELO DOCKER!<h1>" > /usr/local/nginx/html/index.html
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx"]
3,运行测试
mynginx已经ok
[root@gitlab web]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mynginx latest dd46115067d2 33 minutes ago 1.289 GB
benyoo/alpine 3.4.20160812 0ae37ed604b2 12 days ago 11.54 MB
hello-world latest c54a2cc56cbb 7 weeks ago 1.848 kB
kriation/centos7 latest e8630f595772 3 months ago 198.4 MB
将容器80端口映射到物理机100端口
[root@gitlab web]# docker run --name nginx1 -d -p 100:80 mynginx
2b223cf591d3814e1d4995fbbc746c327e4508ee84174811d4d9f806b73a7d72
[root@gitlab web]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2b223cf591d3 mynginx "/usr/local/nginx/sbi" 4 seconds ago Up 2 seconds 0.0.0.0:100->80/tcp nginx1
可以看到100端口已经起来
[root@gitlab web]# ss -tlnp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:* users:(("sshd",pid=1159,fd=3))
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=28892,fd=13))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=1159,fd=4))
LISTEN 0 100 ::1:25 :::* users:(("master",pid=28892,fd=14))
LISTEN 0 1024 :::100 :::* users:(("docker-proxy",pid=43370,fd=4))
[root@gitlab web]#
crul 测试
[root@gitlab web]# curl 127.0.0.1:100
<h1>HELO DOCKER!<h1>
4,解释说明
注释为#号
FROM 镜像 :指定使用哪个基础镜像
MAINTAINER Author : 维护者信息
ADD :移动文件(当前文件 /usr/local/目标文件),或者url
RUN : RUN 可以执行centos中的命令
WORKDIR : 和cd功能类似,切换目录
EXPOSE :映射端口
ENV 可以等于环境变量:PATH /usr/local/nginx/sbin:$PATH
USER :USER命令用于设置运行容器的UID