docker命令語法
复制代码
- 1.ADD ADD命令有兩個參數,源和目標。它的基本作用是從源系統的文件系統上復制文件到目標容器的文件系統。如果源是一個URL,那該URL的內容將被下載并復制到容器中 ADD /my_app_folder /my_app_folder 2.ENTRYPOINT 配置容器啟動后執行的命令,并且不可被 docker run 提供的參數覆蓋,每個 Dockerfile 中只能有一個 ENTRYPOINT,當指定多個時,只有最后一個起效。 3.ENV ENV命令用于設置環境變量。這些變量以”key=value”的形式存在,并可以在容器內被腳本或者程序調用。這個機制給在容器中運行應用帶來了極大的便利。 ENV PATH /usr/local/nginx/sbin:$PATH 4.EXPOSE EXPOSE用來指定端口,使容器內的應用可以通過端口和外界交互。 EXPOSE 80 5.FROM 這個命令用于聲明作者,并應該放在FROM的后面。 MAINTAINER authors_name 6.RUN 7.USER USER命令用于設置運行容器的UID。 8.WORKDIR WORKDIR命令用于設置CMD指明的命令的運行目錄。
dockerfile創建鏡像
下面就構建一個簡單的dockerfile
1.需要一個基礎鏡像
复制代码
- docker pull centos
2.在某一個目錄下面創建一個專門存放此demo的目錄,也就是Dockerfile所在的context:
复制代码
- [root@docker ~]# mkdir docker_demo [root@docker ~]# cd docker_demo/ [root@docker docker_demo]# touch Dockerfile [root@docker docker_demo]# pwd /root/docker_demo [root@docker docker_demo]# ll total 0 -rw-r--r--. 1 root root 0 Nov 1 04:34 Dockerfile 下載nginx源碼包到docker_demo這個目錄下: [root@docker docker_demo]# ll total 960 -rw-r--r--. 1 root root 0 Nov 1 04:34 Dockerfile -rw-r--r--. 1 root root 981687 Oct 17 09:20 nginx-1.12.2.tar.gz 以下是編寫好的Dockerfile v1版: [root@docker docker_demo]# cat Dockerfile # base image FROM centos # MAINTAINER MAINTAINER json_hc@163.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 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 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 PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 [root@docker docker_demo]# docker build -t centos_nginx:v2 . Sending build context to Docker daemon 985.6kB Step 1/10 : FROM centos ---> 196e0ce0c9fb Step 2/10 : MAINTAINER json_hc@163.com ---> Using cache ---> cde1d7830106 Step 3/10 : ADD nginx-1.12.2.tar.gz /usr/local/src ---> Using cache ---> 1e4d16340af0 Step 4/10 : RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel ---> Using cache ---> 405835ad9b0b Step 5/10 : RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel ---> Using cache ---> 4002738cf7a6 Step 6/10 : RUN useradd -M -s /sbin/nologin nginx ---> Using cache ---> 02961c5c564d Step 7/10 : WORKDIR /usr/local/src/nginx-1.12.2 ---> Using cache ---> f1da71a93c5e Step 8/10 : 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 ---> Using cache ---> cd2ad4c45004 Step 9/10 : ENV PATH /usr/local/nginx/sbin:$PATH ---> Running in 07ba2f7129bc ---> 9588fa1058aa Removing intermediate container 07ba2f7129bc Step 10/10 : EXPOSE 80 ---> Running in 473cd847154a ---> 2031faf8894a Removing intermediate container 473cd847154a Successfully built 2031faf8894a Successfully tagged centos_nginx:v2 $ docker images $ docker run -d -p81:80 centos_nginx nginx -g "daemon off;" $ docker ps -l
最后通過瀏覽器訪問就可以了