docker-compose 自定義 nginx/php/mysql/redis/go/java/tomcat 組合環境

Dockerfiles

用 Docker 容器服務的方式搭建 nginx/php/mysql/redis/go/node/postgres/phpmyadmin 環境,易於維護、升級。php

鏡像版本html

公用存儲: (網段名: dockerfiles_default)java

  • MySQL 5.7
  • Redis 3.2
  • Postgres 10

其它:node

  • PHP 7.1/7.2/7.3
    • 擴展: swoole v4.3.0
    • 擴展: Composer version 1.8.4
  • Golang 1.12
  • Nginx 1.15
  • Node 11.12
  • tomcat 8-jre8

構建自定義項目組合, 配置說明

公用存儲(MySQL, Redis, Postgres), 使用了網段: dockerfiles_defaultmysql

你能夠經過複製 && 修改 .env.example 來自定義構建項目組合nginx

示例:git

cp .env.example .env
vi .env
複製代碼

使用

1. 下載

下載 zip 壓縮包 && 解壓github

wget -c https://github.com/hopher/dockerfiles/archive/master.zip -O dockerfiles.zip
unzip dockerfiles.zip
mkdir -p ${HOME}/app
複製代碼

其中, ~/app 爲 volumes 名稱,可根據本身須要更改 docker-compose.yml 中 volumes 對應值web

2. docker-compose 構建項目

進入 docker-compose.yml 所在目錄: 執行命令:redis

cp .env.example .env
docker-compose up
複製代碼

若是沒問題,下次啓動時能夠以守護模式啓用,全部容器將後臺運行:

docker-compose up -d
複製代碼

使用 docker-compose 基本上就這麼簡單,Docker 就跑起來了,用 stop,start 關閉開啓容器服務。
更多的是在於編寫 dockerfile 和 docker-compose.yml 文件。

能夠這樣關閉容器並刪除服務:

docker-compose down
複製代碼

3. 測試

將項目源碼放到 ~/app 目錄下, 並運行

cd src
echo "<?php phpinfo();" > index.php
複製代碼

打開 url 訪問 http://localhost/index.php

4. 經常使用指令

  • 幫助

    docker-compose --help
    複製代碼
  • 列出網絡 (包括跨羣集中多個主機的網絡)

    docker network ls
    複製代碼
  • 運行時,指定配置文件

    docker-compose -p java -f docker-compose-tomcat.yml up -d
    複製代碼

    參數:

    • -p 工程名稱, 這裏爲 java, 表明java 相關配置
    • -f 配置文件
    • -d 後臺運行
  • 經常使用shell組合

    # 刪除全部容器
    docker stop `docker ps -q -a` | xargs docker rm
    
    # 刪除全部標籤爲none的鏡像
    docker images|grep \<none\>|awk '{print $3}'|xargs docker rmi
    
    # 查找容器IP地址
    docker inspect 容器名或ID | grep "IPAddress"
    
    # 建立網段, 名稱: mynet, 分配兩個容器在同一網段中 (這樣子才能夠互相通訊)
    docker network create mynet
    docker run -d --net mynet --name container1 my_image
    docker run -it --net mynet --name container1 another_image
    複製代碼

更多幫助信息 docker-compose -h|--help

5. 目錄結構

dockerfiles
    |-- services                    # docker 相關服務
    |-- docker-compose.yml          # 通用配置文件
    |-- docker-compose-tomcat.yml   # tomcat 配置文件
    |-- mirrors                     # source.list 鏡像源地址
~/app                               # 工做源碼存放目錄
複製代碼

Version 3 (docker-composer) 再也不支持參數說明

depends_on

筆者解讀: 經過配置 networks 參數更好地改進

  • depends_on does not wait for db and redis to be 「ready」 before starting web - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.
  • Version 3 no longer supports the condition form of depends_on.
  • The depends_on option is ignored when deploying a stack in swarm mode with a version 3 Compose file.

@docs.docker.com/compose/com…

links

Warning: The --link flag is a legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link. One feature that user-defined networks do not support that you can do with --link is sharing environmental variables between containers. However, you can use other mechanisms such as volumes to share environment variables between containers in a more controlled way.

各系統軟件源

Ubuntu

系統代號 版本
precise 12.04
trusty 14.04
vivid 15.04
xenial 16.04
zesty 17.04

Debian

系統代號 版本
squeeze 6.x
wheezy 7.x
jessie 8.x
stretch 9.x
buster 10.x

NOTE:
查詢本身的Linux版本 cat /etc/issue

git ssh 密鑰配置

For your host machine which run git, all the contents of git config --list is stored in files:

  • If use git config --system to configure them, they are stored in /etc/gitconfig
  • If use git config --global to configure them, they are stored in ~/.gitconfig

@stackoverflow.com/questions/5… @github.com/tomwillfixi…

示例:

volumes:
    # Git and ssh config
    - ~/.ssh:/root/.ssh:ro # Change - ssh key needed to push to github
    - ~/.gitconfig:/root/.gitconfig:ro  # Change - git config needed for user details
    #- /tmp/ssh_auth_sock:/tmp/ssh_auth_sock #Static - needed to push to github without prompt
複製代碼

版本計劃

  • [] 增長 .env 配置鏡相版本

反饋、參與貢獻

點擊 - 提交你的意見, 十分感謝

一塊兒組隊, 參與貢獻

fork 一份, 按照格式:

services/名稱/版本號/Dockerfile

示例:

services/php/v7.1/Dockerfile
複製代碼

在 services/php/v7.1 文件夾中,編寫具體內容,而後 Pull Request

貢獻名單

... 期待你的留名 ...

參考資料

相關文章
相關標籤/搜索