1. 刪除docker可能有的早期版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2. 安裝docker需要的一些依賴
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
3. 配置docker的repo以便yum install時能夠使用到最新的docker版本image
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
4. 安裝docker-ce及cli
yum install docker-ce docker-ce-cli containerd.io
5. 立即啟動docker engine並設定開機啟動
systemctl start docker
systemctl enable docker
systemctl list-unit-files | grep enabled 檢查確認是否已經開機啟動
6. 試執行docker container
docker run hello-world
結果出錯,google後發現是kernel不匹配,因為centos7.2的kernel太舊,必須更新kernel
下面將記錄如何將centos7.2升級kernel到5.0,以下連結可以供參考:
https://www.tecmint.com/install-upgrade-kernel-version-in-centos-7/
https://www.jianshu.com/p/1261ed6f8399
7.配置repo,並且安裝最新的linux kernel(5.0)
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install kernel-ml
8. 將5.0的kernel在centos7.2中配置為開機預設項
/etc/default/grub 中增加一個GRUB_DEFAULT=0 這樣就選擇最新安裝的那個kernel了
grub2-mkconfig -o /boot/grub2/grub.cfg 重新生成kernel配置資訊,以便自動應用新安裝的kernel
9. 重新啟動
10. 如果有必要可以建立一個docker group,並將對應使用者名稱加到這個group中,避免使用root直接操作,提高安全性
groupadd docker
11. 映象加速:
在/etc/docker/daemon.json檔案中新增以下內容,
{
"registry-mirrors": ["https://etomhx9s.mirror.aliyuncs.com"]
}
並執行:
sudo systemctl daemon-reload
sudo systemctl restart docker
之後所有對docker官方的映象都會自動加速使用阿里雲的mirror
12. centos安裝docker-compose
curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
13. 安裝command-line completion以便使用tab鍵快速列出可選子命令
請參考頁面: https://docs.docker.com/compose/completion/
curl -L https://raw.githubusercontent.com/docker/compose/1.23.2/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
//// trouble shooting:
curl https://raw.githubusercontent.com/docker/docker/master/contrib/check-config.sh > check-config.sh
bash ./check-config.sh
準備工作已經做完,下面需要安裝nginx+php-fpm(7.1)+mysql(5.6.35)+redis的容器了.
思路是:通過docker-compose方式來編排微服務,分別切分為nginx web伺服器,nginx使用官方映象;upstream php-fpm應用服務,php-fpm則基於官方7.1的映象新增一些擴充套件安裝以及composer安裝,
注意php-fpm7.2以上版本往往會出現以下錯誤"Parameter must be an array or an object that implements Countable"
mysql和redis也直接使用官方映象起一個微服務即可。其中mysql使用一個volume直接應用已經存在的資料庫檔案,後續可以考慮使用一個資料卷容器來服務資料
由於眾所周知的偉大的牆,給我們苦逼的程式猿帶來了太多的麻煩,好在docker公司提供的play-with-docker可以免費提供4G記憶體的雲主機足夠我們使用。我們直接在該雲主機上使用docker-compose build命令做映象構建,隨後push到docker hub上。最後在國內的主機上docker pull下來映象並使用docker-compose up -d nginx一下子就把所有服務拉起了!
14. 將我們的docker-compose up -d xxx作為開機啟動
a)在/etc/systemd/system目錄中建立一個myapp.service文字檔案
[Unit] Description=myappservice After=docker.service#注意本服務依賴於docker service,而dockerservice又require docker.socket 必須在docker engine起來之後才能執行,docker啟動比較慢,需要2分鐘左右
[Service]
# see man systemd.service
Type=oneshot
WorkingDirectory=/yourworkingdirectory
ExecStart=/yourworkingdirectory/start-all.sh
RemainAfterExit=true
ExecStop=/yourworkingdirectory/stop-all.sh
StandardOutput=journal
[Install]
WantedBy=multi-user.target
b)在/yourworkingdirectory中分別建立start-all和stop-all.sh指令碼
#!/bin/bash
# This is the stop script
docker-compose up -d xx
#!/bin/bash
# This is the stop script
docker-compose stop
c)將啟動和停止指令碼賦予執行許可權: chmod u+x start-all.sh
d)# systemctl enable yourservice
Created symlink from /etc/systemd/system/multi-user.target.wants/yourservice.service to /etc/systemd/system/yourserivce.service.
搞定!
optional:
你可能希望起一個mysqladmin方便你圖形化管理資料庫
docker run --name myadmin -d --link dockerdeployconf_mysql_1:mysql --net dockerdeployconf_default -e PMA_HOST=mysql -p 8080:80 phpmyadmin/phpmyadmin
docker-compose.yml
version: '3' services: myapp:
build:
context: /pathtoconfig
dockerfile: Dockerfile-phpfpm
image: xx/myimage:7.1 depends_on: - mysql - redis volumes: - /pathtophpapp/:/application mysql: image: mysql:5.6 environment: - MYSQL_ROOT_PASSWORD=yourrootpassword - MYSQL_DATABASE=yourdatabase volumes: - /pathtodatafile/:/var/lib/mysql nginx: image: nginx ports: - "80:8000" volumes: - /pathtoconfig/nginx.conf:/etc/nginx/conf.d/default.conf - /pathtophpapp/:/application depends_on: - myapp redis: image: redis ports: - "6379:6379"
dockerfile-phpfpm
FROM php:7.1-fpm LABEL maintainer="zhang@xx.com" # Installing dependencies RUN apt-get update && apt-get install -y \ build-essential \ mysql-client \ libpng-dev \ libjpeg62-turbo-dev \ libfreetype6-dev \ libzip-dev \ locales \ zip \ jpegoptim optipng pngquant gifsicle # Clear cache RUN apt-get clean && rm -rf /var/lib/apt/lists/* # Installing extensions RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl bcmath opcache RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ RUN docker-php-ext-install gd # Installing composer RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer # Setting locales RUN echo zh_CN.UTF-8 UTF-8 > /etc/locale.gen && locale-gen # Changing Workdir WORKDIR /application
nginx.conf
server { listen 8000; index index.php index.html index.htm; root /application/public; # default Laravel's entry point for all requests access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { # try to serve file directly, fallback to index.php try_files $uri /index.php?$args; } location ~ \.php$ { fastcgi_index index.php; fastcgi_pass myapp:9000; # address of a fastCGI server fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_https://github.com/rlerdorf/php7dev/issues/48_info; include fastcgi_params; } }
其中需要另外注意的一點是:
php-fpm的log蒐集問題:
catch_workers_output = yes
https://github.com/rlerdorf/php7dev/issues/48
https://stackoverflow.com/questions/8677493/php-fpm-doesnt-write-to-error-log