Docker 折腾笔记


安装

使用  清华大学开源软件镜像站

https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/

更换下载镜像

http://blog.csdn.net/small_to_large/article/details/77334973

https://docker.mirrors.ustc.edu.cn

中文文档

https://docs.docker-cn.com/

搜索镜像

sudo docker search nginx

拉取镜像

sudo docker pull nginx

nginx 与 php-fpm

https://www.centos.bz/2016/11/dockerise-your-php-application-with-nginx-...

docker 的使用

docker 容器仅作为运行环境使用。代码放外面

需要服务器的知识,需要软件的配置知识,总之 docker 有点难

使用 docker-compose.yml 

www/docker-compose.yml

version: '2'
services:
  web:
    build:
      context: ./services/web
      dockerfile: Dockerfile
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./code:/var/www/html
      - ./logs/apache2/:/var/log/apache2/
      - ./services/web/config/sites-enabled:/etc/apache2/sites-enabled
      - ./services/web/config/cert:/etc/apache2/cert
    restart: always
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_DATABASE: "app"
      MYSQL_USER: "app"
      MYSQL_PASSWORD: "app"
    restart: always
    volumes:
      - ./services/db/config/:/etc/mysql/mysql.conf.d/
      - ./mysql:/var/lib/mysql
  redis:
    image: redis
    volumes:
      - ./services/redis/config/redis.conf:/usr/local/etc/redis/redis.conf
  memcached:
    image: memcached:alpine

在php中连接数据库的时 host 为 db

同样 连接 memcache 的 host 为 memcached

apache 开启 https 证书需要绝对路径

www/services/web/Dockerfile

FROM php:5.6-apache-jessie
MAINTAINER sosyuki <master@sosyuki.com>

# 更新阿里云的jessie版本包源
RUN echo "deb http://mirrors.aliyun.com/debian jessie main contrib non-free" > /etc/apt/sources.list && \
    echo "deb-src http://mirrors.aliyun.com/debian jessie main contrib non-free" >> /etc/apt/sources.list  && \
    echo "deb http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb-src http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb-src http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list
    
RUN apt-get update && apt-get install -y libpng12-dev libjpeg-dev libmemcached-dev zlib1g-dev \
  && rm -rf /var/lib/apt/lists/* \
  && docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
  && docker-php-ext-install gd mysqli pdo_mysql zip opcache \
  && pecl install memcached-2.2.0 \
  && pecl install redis-3.1.6 \
  && docker-php-ext-enable memcached redis



RUN a2enmod rewrite
RUN a2enmod ssl

COPY ./config/php.ini /usr/local/etc/php/conf.d/

修改 Dockerfile 后 运行

docker-compose build

重建镜像

使用 

docker-compose up -d

更新容器 

www/services/web/config/sites-enabled/default.conf

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog /var/log/apache2/error.log
        CustomLog /var/log/apache2/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>


<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
SSLSessionCache nonenotnull
SSLCertificateFile "/etc/apache2/cert/devdesktop.crt"
SSLCertificateKeyFile "/etc/apache2/cert/devdesktop.key"
</IfModule>

<VirtualHost *:443>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com


        SSLEngine on

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html



        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog /var/log/apache2/error.log
        CustomLog /var/log/apache2/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>