一、准备工作
1、下载基础镜像
[root@localhost ~]# docker pull centos
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 9f38484d220f 2 months ago 202MB
2、创建存放nginx的目录和Dockerfile文件(注意Dockerfile的D需要大写)
[root@localhost ~]# mkdir docker_nginx
[root@localhost ~]# cd docker_nginx/
[root@localhost docker_nginx]# touch Dockerfile
[root@localhost docker_nginx]# pwd
/root/docker_nginx
[root@localhost docker_nginx]# ll
total 0
-rw-r--r-- 1 root root 0 Jun 14 02:48 Dockerfile
3、下载nginx源码包到docker_nginx目录下
[root@localhost docker_nginx]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
二、编写Dockerfile
1、配置文件,注意添加daemon off;不是以守护进程启动,否则进程进行完成会自动关闭,容器无法再访问
[root@localhost docker_nginx]# cat Dockerfile
FROM centos
MAINTAINER docker_user@email.com
ADD nginx-1.12.2.tar.gz /usr/local/src
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/local/src/nginx-1.12.2
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]
2、查看docker_nginx目录情况
[root@localhost docker_nginx]# pwd
/root/docker_nginx
[root@localhost docker_nginx]# ll
total 964
-rw-r--r-- 1 root root 509 Jun 14 03:17 Dockerfile
-rw-r--r-- 1 root root 981687 Oct 17 2017 nginx-1.12.2.tar.gz
3、执行docker build进行构建
[root@localhost docker_nginx]# docker build -t centos_nginx:v1 .
4、构建成功提示,并查看
Removing intermediate container 9976df44f135
Successfully built 18be94a7553b
[root@localhost docker_nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v1 18be94a7553b 4 minutes ago 524 MB
三、启动新镜像并映射端口到宿主机
1、-d后台运行,-p指定端口
[root@localhost docker_nginx]# docker run -d -p 80:80 centos_nginx:v1
80c508a81a1b2d5f9ecbd6359d16652e1f0c7d8b4cff19035e49351ba84b3921
2、查看容器,可以看到映射端口
[root@localhost docker_nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80c508a81a1b centos_nginx:v1 "nginx -g 'daemon ..." 20 seconds ago Up 19 seconds 0.0.0.0:80->80/tcp ecstatic_clarke
3、容器名称更改:rename 旧名称 新名称
[root@localhost docker_nginx]# docker rename ecstatic_clarke nginx_v1
[root@localhost docker_nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80c508a81a1b centos_nginx:v1 "nginx -g 'daemon ..." 7 minutes ago Up 7 minutes 0.0.0.0:80->80/tcp nginx_v1
4、网页访问
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/95270.html