Docker構建程序員的平常

clipboard.png

前言

拋開Docker那些強大的功能,今天咱們來部署下本地的開發環境。並寫上幾個腳原本提升開發效率。php

本章以MacOs系統的Docker演示,其餘系統做者爲接觸過。不知是否有差異。html

安裝

傻瓜式安裝,這裏就再也不闡述了。下載地址以下

https://www.docker.com/produc...前端

目錄

建立一些目錄,就如在項目開發中建立Controller,Model,Service同樣。咱們將本地的Docker開發環境先從目錄開始整理如下。node

目錄名 用途
app 項目目錄,源程序存放的地方
services 服務目錄,例如mysql,php等
ssh 遠程服務器目錄,用於連接服務器
web 前端目錄,正常node會指向它

部分文件列表mysql

文件名 用途
.env 配置公共文件
docker-compose.yml docker-composer 配置文件
hosts 系統 hosts 文件
php.sh php 相關操做文件
start.sh 本地環境操做腳本

因是我的使用,因此對命名和規範稍有出入。請諒解nginx

配置服務

配置你所須要的服務到Docker容器內git

MySQL

docker-composer.yml程序員

db:
    container_name: 'dev_db'
    build: ./services/mysql // 指向dockerfile文件
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} // .env文件的配置項
    ports:
      - "3306:3306"
    volumes:
      - ${MYSQL_DATA_PATH}:/var/lib/mysql

建立mysql-dockerfileservices\mysql
Dockerfilegithub

FROM mysql:5.6
MAINTAINER crazycodes <919342864@qq.com>

ADD ./config/default.cnf /etc/mysql/conf.d/default.cnf

Nginx

docker-composer.ymlweb

web:
    container_name: 'dev_nginx'
    build: ./services/nginx
    ports:
      - "80:80"
    depends_on:
      - php
    volumes_from:
      - php
    volumes:
      - ${NGINX_VOLUME_CONFIG_PATH}:/etc/nginx/conf.d
    dns: 8.8.8.8

Dockerfile

FROM nginx:latest
MAINTAINER crazycodes <919342864@qq.com>

RUN apt-get update && apt-get install -y vim sudo gcc make unzip wget mercurial libpcre3-dev zlib1g-dev libssl-dev devscripts debhelper dpkg-dev quilt lsb-release

COPY nginx.conf /etc/nginx/nginx.conf
COPY nginx-rtmp-module /etc/nginx/nginx-rtmp-module
COPY nginx-1.13.9 /etc/nginx/nginx-1.13.9

WORKDIR /etc/nginx/nginx-1.13.9
RUN ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.13.7/debian/debuild-base/nginx-1.13.7=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-specs=/usr/share/dpkg/no-pie-link.specs -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-module=../nginx-rtmp-module
RUN make && make install

站點配置在services\nginx\config內,你能夠正常配置

server {
  listen       80;
  server_name  localhost;
  root          /mnt/app/flarum;
  index         index.php index.html index.htm;

  location / { try_files $uri $uri/ /index.php?$query_string; }
  location /api { try_files $uri $uri/ /api.php?$query_string; }
  location /admin { try_files $uri $uri/ /admin.php?$query_string; }

  location /flarum {
    deny all;
    return 404;
  }
  location ~ .php$  {
     fastcgi_split_path_info ^(.+.php)(/.+)$;
     fastcgi_pass   php:9000; // 容器內,此處要填 容器名稱:容器映射端口
     fastcgi_index  index.php;
     include        fastcgi_params;
  }
}

PHP

dockerfile文件內容過多。這裏就不貼代碼了。文章最後會貼出源碼地址。

其餘

活學活用,代碼不單單是幫助你開發,也能夠幫你提示開發效率。將全部工做變得自動化,更能體現你的本領。

ssh

ssh中放着全部服務器連接文件

set -x
ssh root@xxx.xxx.xxx.xxx

每次當你須要連接服務器時,直接使用

sh dev.sh

便可

php.sh

若是想要操做容器內php的命令。命令過長,不方便。咱們能夠將代碼放入sh文件中

set -x

docker exec -it dev_php /bin/sh -c "cd /mnt/app/${1} && ${2}"

這時你若是須要操做容器內的PHP,就能夠這樣寫

sh php.sh My_Blog artisan migrate

start.sh

start.sh文件中存放着一些基本命令,固然你也能夠繼續擴展你想要的。例如對composer的操做,php的操做,mysql的操做等等。具體你能夠去點擊下方連接查看。

致謝

習慣將許多命令封裝到執行文件內。方便本身使用。提高開發效率和質量是每位程序員必備的技能。

https://github.com/CrazyCodes...這並非一個很是認真的docker「操做」,請勿使用到生產環境。很高興你看到這裏,但願本篇文章能夠幫到你。謝謝。

相關文章
相關標籤/搜索