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