4、CentOS7 安装Docker之Dockerfile

Docker之Dockerfile

上面我们学习了docker的理念,架构以及安装指令。本章我们来学习如何写Dockerfile(注意:D必须是大写),Dockerfile的步骤,如下图:

 

先查看下本地的镜像,选一个作为base image:

[root@docker-node1 ~]# docker images

REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE

docker.io/nginx                            latest              c82521676580        8 days ago          109 MB

docker.io/alexeiled/docker-oracle-xe-11g   latest              f7304758169d        2 years ago         2.39 GB

 

1、创建Dockerfile的安装包文件文件存放目录:

[root@docker-node1 ~]# mkdir /opt/docker-file

 

 

  1. 以下是编写好的Dockerfile v1版:

 

[root@docker-node1 docker-file]# cat Dockerfile

#This docker file

#VERSION 1

#Author:yanyb

#Base image 可以是镜像名字或者镜像ID

FROM centos

 

#Maintainer 作者 邮箱

MAINTAINER yanyb yongbang_yan@139.com

 

#put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx

ADD nginx-1.12.2.tar.gz /usr/local/src

 

# running required command 在容器里运行nginx需要依赖包的命令

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

 

# change dir to /usr/local/src/nginx-1.12.2 去到nginx解压目录

WORKDIR /usr/local/src/nginx-1.12.2

 

# execute command to compile nginx 在容器里运行命令进行编译安装

RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install

 

#env 将nginx启动命令加到环境变量里

ENV PATH /usr/local/nginx/sbin:$PATH

 

#expose 映射80端口

EXPOSE 80

 

 

 

3、查看docker_demo目录情况:

[root@docker-node1 docker-file]# ll

-rw-r--r-- 1 root root   1416 Aug  2 09:00 Dockerfile

-rw-r--r-- 1 root root 981687 Aug  2 08:50 nginx-1.12.2.tar.gz

 

 

 

 

4、执行docker build进行构建:

[root@docker-node1 ~]# docker build -t centos_nginx:v1 /opt/docker-file/

注意:后面的/opt/docker-file/要指定存放Dockerfile文件和安装包的目录。

 

当成功后会出现这个,说明执行成功

Successfully built 21273b8e163a

查看新构建的镜像:

 

 

5、然后使用构建的镜像启动一个container并开启nginx服务:

 

[root@docker-node1 docker-file]# docker run -d centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"

e56d38b6821877416bd260bead0e4d1fe0152f74711ee3676263b30fddf3b740

然后查看启动的container是否在运行:

docker ps

 

虽然nginx服务开启了,但是port并没有进行映射到本机host,所以这个container并不能进行访问,重新启动一个进行了映射端口的容器

[root@docker-node1 docker-file]# docker run -d -p8080:80 centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"

265c9d91afc2a12e02491588cdbe7354680c70e27aeab25389cb93bfbeb41d4e

再次查看端口映射信息:

[root@docker-node1 docker-file]# docker ps -a | grep 265c9d91afc2

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值