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

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.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 喝醉的时候,才能看透世间冷暖。 为什么这么说,因为你喝醉是一种个体行为,而你所谓的朋友却不想让它成为一种集体行为。...
    大个dae阅读 815评论 0 0

友情链接更多精彩内容