dockfile

本文介绍如何通过Dockerfile构建Nginx镜像并部署容器,包括配置环境变量、映射端口等关键步骤。

首先需要的包和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>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值