你們能夠看完教程親自嘗試下,也能夠直接執行一鍵安裝命令,整個過程大概10分鐘左右,我在四臺不一樣的機器上執行過該命令,因爲網絡緣由,5-15分鐘不等。javascript
如本文章內容與經過一鍵安裝下載的不一樣,以一鍵安裝的爲準,一鍵安裝版本會繼續更新,v1.3.0版本支持memcache 和 redis。php
執行完一鍵安裝後,直接訪問 你的IP:8081 訪問便可出現phpinfo頁面的內容css
本次部署,旨在單臺服務器上使用docker構建集成環境,並運行Nginx+PHP項目html
$ sudo yum update
$ sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-selinux \ docker-engine-selinux \ docker-engine
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum makecache fast
sudo yum -y install docker-ce
sudo systemctl start docker
[root@runoob ~]# docker run hello-world docker run 命令會先在本地查找 hello-world鏡像,若是本地沒有會自動下載一個到本地,而後在運行hello-world
$ sudo yum remove docker-ce $ sudo rm -rf /var/lib/docker
如想詳細瞭解Dockerfile知識的,推薦 https://www.cnblogs.com/jsonhc/p/7767669.htmljava
cd /root mkdir dockerfiles //創建文件夾 cd dockerfiles/ mkdir nginx //存放nginx相關文件 cd nginx/ touch Dockerfile && mkdir conf && mkdir logs && mkdir html && mkdir www //建立Dockerfile文件,創建nginx的配置目錄和日誌等目錄
[root@mdm nginx]# cat Dockerfile # 基礎鏡像 FROM centos # 維護者 MAINTAINER 271648298@qq.com # 安裝wget下載工具 RUN yum install -y wget # 切換到usr/lcoal/src/目錄,至關於cd,並能夠用cd 代替, 但docker官方不建議用cd WORKDIR /usr/local/src # 添加遠程文件到當前文件夾, 注意:後面有個點(.) 表明當前目錄。ADD能夠添加遠程文件到鏡像,但COPY僅能夠添加本地文件到鏡像中。 ADD http://nginx.org/download/nginx-1.17.0.tar.gz . # RUN,在鏡像內運行解壓命令 RUN tar zxvf nginx-1.17.0.tar.gz # 切換目錄 WORKDIR /usr/local/src/nginx-1.17.0 # 更新yum,可不執行 # RUN yum -y update # 安裝必要的軟件和添加nginx用戶 RUN yum install -y gcc gcc-c++ glibc make openssl-devel RUN yum install -y libxslt-devel -y gd-devel GeoIP GeoIP-devel pcre pcre-devel RUN useradd -M -s /sbin/nologin nginx # 掛載卷,測試用例(這裏的掛載卷,不能夠指定本機的目錄,不夠靈活,通常會在 啓動容器時經過 -v 參數指定掛載卷,或在docker-compose.yaml文件中指定,均可以指定本地目錄) VOLUME ["/data"] # 編譯安裝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_degradation_module --with-http_stub_status_module && make && make install # 切換到Nginx的配置目錄 WORKDIR /usr/local/nginx/conf # 創建子配置文件夾,我的愛好,能夠不建,或者叫其它名稱均可以,但最好不要帶特殊符號, RUN mkdir vhost # 設置變量,執行命令時,就能夠省略前綴目錄了 ENV PATH /usr/local/nginx/sbin:$PATH # 暴露端口 EXPOSE 80 # the command of entrypoint ENTRYPOINT ["nginx"] # 執行命令,數組形式, "-g daemon off;" 使咱們運行容器時,容器能夠前臺運行,不會退出 CMD ["-g", "daemon off;"]
[root@mdm nginx]# docker build -t centos_nginx:self . //注意,最後有個點, centos_nginx 是鏡像名稱,self是打的標籤,跟版本號同樣的意思
docker run -d -p 8082:80 --name=test_nginx centos_nginx:self //啓動一個容器,輸出成功會提示一串字符串 -d 是守護進程運行的意思,即容器後臺運行不會退出 -p 映射端口號,宿主機端口:容器端口 --name 容器名稱, 最後的 centos_nginx:self 是使用的鏡像:版本號 docker ps -a //查看全部容器列表,顯示以下,說明nginx容器已經正常啓動 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d51f2c95b66c centos_nginx:self "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 0.0.0.0:8082->80/tcp test_nginx 經過 curl 127.0.0.1:8082 命令,能看到 Welcome to nginx 等英文提示,即說明一切OK 下面咱們進入容器,進入容器有不少方式,還能夠經過ssh進入,這裏我只介紹我經常使用的方式 docker exec -it d51f2c95b66c /bin/bash //這種方式進入,不會形成容器的關閉, docker attach 進入再退出會形成容器關閉, 後邊的 /bin/bash 也能夠換成 /bin/sh(好比alpine 基礎鏡像) 進來後查看Nginx的配置文件 [root@d51f2c95b66c /]# cd /usr/local/nginx/ [root@d51f2c95b66c nginx]# ls client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp 能夠本身瞭解下這個版本的nginx的目錄結構,方便後期配置 經過exit 命令退出容器便可
cd /root/dockerfiles/nginx/conf touch nginx.conf // 該文件未來要掛載到容器中,做爲Nginx的配置文件, 你能夠經過 docker cp d51f2c95b66c :/usr/local/nginx/conf/nginx.conf /root/dockerfiles/nginx/conf 複製一份Nginx容器的原生配置文件,也可使用下面個人nginx配置文件
[root@mdm conf]# cat nginx.conf user nginx; worker_processes 1; error_log /usr/local/nginx/logs/error.log warn; pid /usr/local/nginx/logs/nginx.pid; events { worker_connections 1024; } http { include /usr/local/nginx/conf/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /usr/local/nginx/logs/access.log main; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; #limit_conn_zone $binary_remote_addr zone=perip:10m; ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section. server_tokens off; access_log off; include /usr/local/nginx/conf/vhost/*.conf; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
docker stop d51f2c95b66c docker run -d -p 8082:80 -v /root/dockerfiles/nginx/logs:/usr/local/nginx/logs -v /root/dockerfiles/nginx/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf -v /root/dockerfiles/nginx/conf/vhost:/usr/local/nginx/conf/vhost -v /www:/www centos_nginx:self //這樣的話,nginx的配置文件和項目所在目錄/www都掛載上了,能夠在宿主機修改配置文件並訪問nginx, nginx的基本配置到此結束。
cd /root/dockerfiles mkdir php cd php touch Dockerfile
[root@guangai-app php]# cat Dockerfile FROM php:7.1-fpm-alpine3.9 MAINTAINER 271648298@qq.com # install redis RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \ && apk update \ && apk add --no-cache libmcrypt-dev freetype-dev libjpeg-turbo-dev \ && docker-php-ext-install mcrypt pdo_mysql \ && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ \ && docker-php-ext-install -j$(nproc) gd \ && mkdir -p /usr/src/php/ext/redis \ && curl -L https://github.com/phpredis/phpredis/archive/3.1.6.tar.gz | tar xvz -C /usr/src/php/ext/redis --strip 1 \ && echo 'redis' >> /usr/src/php-available-exts \ && docker-php-ext-install redis
具體php:7.1-fpm-alpine3.9中包含了什麼,能夠到官網查看 地址:https://github.com/docker-library/php/blob/a7e2de0e8f2b902bc36be6f5d61c0b4fcd1052ff/7.1/alpine3.9/fpm/Dockerfilenode
docker build -t alpine_php:self . //注意,最後有個點(.)
docker run --name myphp-fpm -v /root/dockerfiles/nginx/www:/www -d alpine_php:self
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose //這裏可能會很慢 sudo chmod +x /usr/local/bin/docker-compose docker-compose --version
[root@guangai-app ~]#cd /root/dockerfiles [root@guangai-app dockerfiles]# ls docker-compose.yml nginx php 能夠看到,這裏有咱們剛完成的nginx 目錄和 php 目錄,除此以外還有一個docker-compose.yml文件
[root@guangai-app dockerfiles]# cat docker-compose.yml nginx: build: ./nginx volumes: - /root/dockerfiles/nginx/html:/usr/share/nginx/html - /root/dockerfiles/nginx/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf - /root/dockerfiles/nginx/conf/vhost:/usr/local/nginx/conf/vhost - /root/dockerfiles/nginx/logs:/usr/local/nginx/logs - /www:/www ports: - "8081:80" links: - php php: build: ./php volumes: - /www:/www
[root@guangai-app vhost]# cat test-php.conf server { listen 80; server_name localhost; location / { root /www/api_wx_klagri_com_cn/public; index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } } error_page 500 502 503 504 /50x.html; location = /50x.html { root /www/cms/public; } location ~ \.php$ { root /www/api_wx_klagri_com_cn/public; fastcgi_pass php:9000; fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; set $real_script_name $fastcgi_script_name; if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") { set $real_script_name $1; set $path_info $2; } fastcgi_param SCRIPT_FILENAME $document_root$real_script_name; fastcgi_param SCRIPT_NAME $real_script_name; fastcgi_param PATH_INFO $path_info; include vhost/my_params/api_wx.conf; } }
docker-compose up -d //-d 後臺運行 修改nginx配置後,經過 docker-compose up -d --force-recreate 重啓
而後經過你ip+端口號訪問 ,好比http://59.110.217.236:8081/index.phpmysql
OKlinux
wget http://pic.klagri.com.cn/env/dockerfiles-1.1.0.tar.gz && tar zxvf dockerfiles-1.1.0.tar.gz && mv dockerfiles-1.1.0 dockerfiles && cd dockerfiles && /bin/bash init.sh
wget http://pic.klagri.com.cn/env/dockerfiles-1.2.0.tar.gz && tar zxvf dockerfiles-1.2.0.tar.gz && mv dockerfiles-1.2.0 dockerfiles && cd dockerfiles && /bin/bash init.sh
wget http://pic.klagri.com.cn/env/dockerfiles-1.3.0.tar.gz && tar zxvf dockerfiles-1.3.0.tar.gz && mv dockerfiles-1.3.0 dockerfiles && cd dockerfiles && /bin/bash init.sh