使用dockerfile構建nginx鏡像

docker構建鏡像的方法: commit、dockerfilenginx

一、使用commit來構建鏡像: commit是基於原有鏡像基礎上構建的鏡像,使用此方法構建鏡像的目的:保存鏡像裏的一些配置信息和修改的信息。至關於一個鏡像的快照。git

二、使用dockerfile來構建鏡像:docker

實驗過程:ubuntu

  1. 下載nginx-1.14.0 http://nginx.org/download/nginx-1.14.0.tar.gz瀏覽器

    下載pcre-8.7 https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gzui

  2. 安裝docker,參考docker官方文檔 https://yeasy.gitbooks.io/docker_practice/install/code

  3. 書寫Dockerfile

# Base image

FROM ubuntu:14.04ip

MAINTAINER

MAINTAINER Chuanxing luckyton@foxmail.com文檔

ENV DEBIAN_FRONTEND noninteractiveget

將nginx以及pcre源代碼加入鏡像

ADD nginx-1.14.0.tar.gz /usr/local/src/ ADD pcre-8.37.tar.gz /usr/local/src

安裝編譯器

RUN apt-get update RUN apt-get install gcc -y RUN apt-get install g++ -y RUN apt-get install make -y

指定工做目錄

WORKDIR /usr/local/src/nginx-1.14.0/

編譯nginx

RUN ./configure --prefix=/usr/local/nginx --without-http_gzip_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

暴露80端口

EXPOSE 80

容器默認啓動命令

ENTRYPOINT ["nginx"]


  4. 建立鏡像

    將下載的nginx-1.14.0.tar.gz和pcre-3.87.tar.gz與Dockerfile放置在同一目錄。

    運行命令 : docker build -t ubuntu14.04_nginx1.14.0:v1

  5. 啓動容器

    運行命令 : docker run -d -p 81:80 ubuntu14.04_nginx1.14.0:v1

  6. 檢驗

    在瀏覽器地址欄,輸入 localhost:81,看效果。

  7. 關閉容器

    查看啓動的容器id : 運行命令 docker container ls

    關閉容器 :運行命令 docker stop 容器id

相關文章
相關標籤/搜索