使用Dockerfile構建鏡像

我的理解:使用Dockerfile來構建鏡像實際上是將應用部署及相關環境配置的命令集成到Dockerfile中,以便能統一鏡像內容,與傳統的部署差異在於,使用Dockerfile部署能夠減少鏡像內容的差別,方便控制。nginx

先說明Dockerfile中包含哪些內容:c++

  • 基礎鏡像信息docker

  • 維護者信息vim

  • 鏡像操做指令centos

  • 容器啓動時執行指令app


clipboard.png

準備環境

#mkdir -p /opt/docker-file/nginx
#cd /opt/docker-file/nginx
#vim Dockerfile

Dockerfile內容

[lileikf2016@instance-1 nginx]$ cat Dockerfile
#This is my docker file about nginx
#Version: 1.0.0
#This is my docker file about nginx
#Version: 1.0.0
#Author: Profeel Wang

#Base image
FROM centos

#Maintainer
MAINTAINER Profeel Wang cqupt_wq@hotmail.com

#Add files
ADD pcre-8.37.tar.gz /usr/local/src
ADD nginx-1.11.13.tar.gz /usr/local/src

#RUN
RUN yum install -y wget gcc gcc-c++ make openssl-devel
RUN useradd -s /sbin/nologin -M www

#WORKDIR
WORKDIR /usr/local/src/nginx-1.11.13

#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 echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf


ENV PATH /usr/local/nginx/sbin:$PATH
#Expose Port
EXPOSE 80

#Run the application
CMD ["nginx"]

啓動鏡像

sudo docker build -t nginx-file:v1 .ui

查看構建的鏡像

[lileikf2016@instance-docker nginx]$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx-file          v1                  cbb545419976        15 minutes ago      394.9 MB
docker.io/centos    latest              a8493f5f50ff        11 days ago         192.5 MB

根據構建的鏡像建立容器

sudo docker run -d -p 80:80 nginx-file:v1

查看容器中的鏡像是否能訪問

clipboard.png
大功告成!spa

相關文章
相關標籤/搜索