---------------------------------------------------------------------------------------------------------------------
首先需要的包和dockerfile放在一起
[root@localhost docker-file]# ls
Dockerfile nginx-1.9.3.tar.gz pcre-8.37.tar.gz
[root@localhost docker-file]# pwd
/opt/docker-file
[root@localhost docker-file]#
---------------------------------------------------------------------------------------------------------------------
创建dockerfile
[root@localhost docker-file]# vim Dockerfile
#base images,name or id 可以是镜像名字或者镜像ID
FROM 3bee3060bfc8

#maintainer 作者
MAINTAINER wangzz

#add 把包添加到容器的指定目录,如果是tar包会自动解压
ADD pcre-8.37.tar.gz /usr/local/src
ADD nginx-1.9.3.tar.gz /usr/local/src

#run 在容器里运行命令安装nginx需要软件
RUN yum install -y wget gcc gcc-c++ make openssl-devel

#run 在容器里运行命令创建用户
RUN useradd -s /sbin/nologin -M www

#workdir 相当于cd到这个目录
WORKDIR /usr/local/src/nginx-1.9.3

#run 在容器里运行命令进行编译安装
RUN ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.37 && make && make install

#run 在容器里运行命令更改nginx配置文件,使其前台运行
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf

#env 将nginx启动命令加到环境变量里
ENV PATH /usr/local/nginx/sbin:$PATH

#映射80端口
EXPOSE 80

执行nginx命令
CMD ["nginx"]
---------------------------------------------------------------------------------------------------------------------
执行dockerfile
[root@localhost docker-file]# docker build -t my-nginx-1:v1 /opt/docker-file/
当成功后会出现这个,说明执行成功
Successfully built 21273b8e163a
---------------------------------------------------------------------------------------------------------------------
查看镜像
[root@localhost docker-file]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-nginx-1 v1 21273b8e163a 14 minutes ago 429.1 MB
---------------------------------------------------------------------------------------------------------------------
使用创建好的镜像启动容器
[root@localhost docker-file]# docker run -dit --name mynginx 21273b8e163a
---------------------------------------------------------------------------------------------------------------------
查看运行的容器
[root@localhost docker-file]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa30accbfc06 21273b8e163a "nginx" 3 seconds ago Up 2 seconds 80/tcp mynginx
---------------------------------------------------------------------------------------------------------------------
访问这个容器的80端口,看nginx是否安装启动成功
[root@localhost docker-file]# curl 172.17.0.3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐