docker使用心得和技巧

个人认为docker最大的用途就是做linux开发环境,各种版本的os和软件通过docker compose能够简单安装,做好了还可以大量分发给开发人员,让开发人员用于独立的开发环境。与安装物理机和虚拟机相比省时省力太多了,同时docker compose的脚本就是物理机的安装说明,真的是非常方便。

下面是我使用过程中的心得

1.不要试图使用systemctl start来启动服务,这样会报错,要使用systemctl enable(privileged: true)来自动启动服务
RUN systemctl enable squid.service

2.测试环境和生产环境连接的数据库时不一样的,往往需要修改代码或配置文件来切换,可以在docker compose中使用container_name来覆盖dns,这样就实现了不修改代码和配置在docker测试环境中连接测试数据库或者是其他服务
    container_name: rds.amazonaws.com

3.docker容器只提供软件环境,不存放数据和程序。数据和程序放在host上,容器通过映射访问host数据,临时数据和日志就放在容器里面,这样删掉容器也不影响数据,让容器更轻量。

4.配置文件放在host上,可以通过映射,追加,修改的方式来设置容器里面配置

5.配置timezone,比较简单的办法是设置环境变量TZ,但对于很多应用来说这样是不行的。比如说设置了timezone后,apache的日志时间还是UTC
这时需要修改Dockerfile来修改系统的timezone,这样容器里面的所有应用就设置上了

ENV TZ Asia/Tokyo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

6.windows和mac的docker环境的容器无法直接获取web请求的IP地址,只能获取docker内部的网关地址,需要通过反向代理设置x-forward-for来间接获取IP地址,这让windows和mac的docker通常只能用于测试环境。

7.windows和mac的docker没有host模式,要调用host上的服务一般需要通过host.docker.internal这个内部域名来实现

8.程序要放在host的目的是可以在windows上使用开发工具vscode来编辑调试代码,并通过windows本地的git来提交代码。windows上有TortoiseGit等图形化git工具,让开发更加简单。在windows的docker中,调试php需要注意,如果需要远程调试,需要设置remote_host为实际的ip地址

xdebug.remote_host = host.docker.internal
;xdebug.remote_connect_back = 1  ---------------这个不能使用了,因为容器无法获取正确IP地址
xdebug.remote_port=9000
xdebug.remote_enable = 1
xdebug.remote_autostart = 1

参考下面的脚本

docker-compose.yml

version: '3.0'
services:
  web:
    build:
      context: .
      dockerfile: ./apache/Dockerfile
    # dns: 8.8.8.8
    privileged: true
    command: /sbin/init
    # logging:
    #   driver: json-file
    extra_hosts:
    - "cross.com:127.0.0.1"
    - "client.com:127.0.0.1"
    - "a.client.com:127.0.0.1"
    - "b.client.com:127.0.0.1"
    - "media.com:127.0.0.1"
    # - "host.docker.internal:xxx"
    ports:
      - "80:80"
      - "443:443"
      - "3128:3128"
    volumes:
      - ./apache/src:/var/www/html
    environment:
      TZ: "Asia/Tokyo"
  mysql:
    image: "mysql:5.7.17"
    container_name: rds.amazonaws.com
    environment:
      TZ: "Asia/Tokyo"
      MYSQL_ROOT_PASSWORD: ""
      MYSQL_USER: 'user'
      MYSQL_PASS: ''
    ports:
      - "3306:3306"
    # restart: always
    volumes:
      - "./mysql/data:/var/lib/mysql"
      - "./mysql/my.cnf:/etc/mysql/conf.d/mysql.cnf"

Dockerfile

FROM centos:7.2.1511
RUN yum install -y epel-release
RUN yum update -y
RUN yum install -y wget
RUN wget --no-check-certificate https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
RUN rpm -Uvh webtatic-release.rpm
RUN yum install -y \
    gcc \
    cc \
    make \
    openssl \
    mod_ssl \
    php70w \
    php70w-opcache \
    php70w-mcrypt \
    php70w-pdo \
    php70w-pdo_mysql \
    php70w-mbstring \
    php70w-gd \
    php70w-pear \
    php70w-devel \
    postfix \
    cyrus-sasl-plain cyrus-sasl-lib cyrus-sasl-md5 \
    squid

# timezone env with default
ENV TZ Asia/Tokyo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN pecl install seaslog
RUN pecl install xdebug-2.9.0
COPY apache/isam1 /root/isam1

COPY apache/php.txt php.txt
COPY apache/vhost.txt vhost.txt
COPY apache/postfix.txt postfix.txt

RUN cat php.txt >> /etc/php.ini
RUN cat vhost.txt >> /etc/httpd/conf.d/ssl.conf
RUN cat postfix.txt >> /etc/postfix/main.cf

# ADD apache/php.txt  /etc/php.ini
# ADD apache/ssl.txt  /etc/httpd/conf.d/ssl.conf
# ADD apache/postfix.txt  /etc/postfix/main.cf

COPY apache/relay_password.txt /etc/postfix/relay_password
RUN postconf -e inet_protocols=ipv4
RUN postmap hash:/etc/postfix/relay_password

RUN mkdir -m 777 /var/www/appdata
RUN mkdir /var/log/php
RUN systemctl enable httpd.service
RUN systemctl enable postfix.service
RUN systemctl enable squid.service

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值