Docker構建自己的容器(學習筆記二)

sktj發表於2018-05-24

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.


相關文章