您现在的位置是:首页 > 文章详情

Docker 构建自己的容器(学习笔记二)

日期:2018-05-23点击:355

Dockerfile

---------------------------------------------------------------------------------------------------------------------

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

Welcome to nginx!

body {

width: 35em;

margin: 0 auto;

font-family: Tahoma, Verdana, Arial, sans-serif;

}

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

For online documentation and support please refer to

nginx.org.

Commercial support is available at

nginx.com.

Thank you for using nginx.

原文链接:https://yq.aliyun.com/articles/660851
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章