docker-compose 小结

version: '3'

services:

  nginx:
    container_name: ${COMPOSE_PROJECT_NAME}_nginx_con
    build:
      context: .
      dockerfile: ./.docker/nginx/Dockerfile
      args:
        - WEB_USER=${WEB_USER}
        - WEB_GROUP=${WEB_GROUP}
    ports:
      - 8080:80
    depends_on:
      - php-fpm
    volumes:
      - ./src:/var/www:ro
      - ./logs/nginx:/var/log/nginx
  mysql:
    container_name: ${COMPOSE_PROJECT_NAME}_mysql_con
    build:
      context: ./.docker/mysql
      args:
        - MYSQL_CONTAINER_USER=${MYSQL_CONTAINER_USER}
        - MYSQL_CONTAINER_GROUP=${MYSQL_CONTAINER_GROUP}
    volumes:
      - ./logs/mysql:${MYSQL_LOG_DIR}
      - ./database:/docker-entrypoint-initdb.d
    environment:
      - MYSQL_CONTAINER_USER=${MYSQL_CONTAINER_USER}
      - MYSQL_CONTAINER_GROUP=${MYSQL_CONTAINER_GROUP}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
  redis:
    container_name: ${COMPOSE_PROJECT_NAME}_redis_con
    build:
      context: .
      dockerfile: ./.docker/redis/Dockerfile
  php-fpm:
    container_name: ${COMPOSE_PROJECT_NAME}_php_con
    build:
      context: .
      dockerfile: ./.docker/php${PHP_VER}-fpm/Dockerfile
      args:
        - WEB_USER=${WEB_USER}
        - WEB_GROUP=${WEB_GROUP}
        - PHP_ROOT_DIR=${PHP_ROOT_DIR}
    working_dir: ${PHP_APP_DIR}
    volumes:
      - ./src:${PHP_APP_DIR}
      - ./logs/php:${PHP_ROOT_DIR}/logs
    environment:
      - WEB_USER=${WEB_USER}
      - WEB_GROUP=${WEB_GROUP}
      - MYSQL_HOST=${MYSQL_HOST}
      - MYSQL_ROOT_USER=${MYSQL_ROOT_USER}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - REDIS_HOST=${REDIS_HOST}
      - REDIS_PORT=${REDIS_PORT}

docker-compose run 命令允许你为你的应用程序运行一次性命令。例如,查看哪些环境变量可以用于 web 服务:

docker-compose run nginx env
docker-compose run nginx ls -lah /var/www

.bulid服务除了可以基于指定的镜像,还可以基于一份 Dockerfile,在使用 up 启动之时执行构建任务,这个构建标签就是 build,它可以指定 Dockerfile 所在文件夹的路径。Compose 将会利用它自动构建这个镜像,然后使用这个镜像启动服务容器。

设定上下文根目录,然后以该目录为准指定 Dockerfile

  nginx:
    container_name: ${COMPOSE_PROJECT_NAME}_nginx_con
    build:
      context: .
      dockerfile: ./.docker/nginx/Dockerfile
      args:
        - WEB_USER=${WEB_USER}
        - WEB_GROUP=${WEB_GROUP}

args 添加构建参数,这些参数是仅在构建过程中可访问的环境变量,如 nginx 的 Dockerfile

FROM nginx:1.15-alpine

ARG WEB_USER
ARG WEB_GROUP

COPY ./.docker/nginx/conf.d /etc/nginx/conf.d

COPY ./src /var/www

RUN echo "Change folder owner to: ${WEB_USER}:${WEB_GROUP}"

RUN chown -R ${WEB_USER}:${WEB_GROUP} /var/www

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据