记录下使用docker-compose搭建开发环境

目录结构

 

docker-compose.yml

version: "3"
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./code:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php

  php:
    #image: php:8.0-fpm
    ######################################################
    build:
      context: .
      dockerfile: php-dockerfile
    ######################################################
    volumes:
      - ./code:/var/www/html
      - ./php/php.ini:/usr/local/etc/php/php.ini
    working_dir: /var/www/html
    depends_on:
      - memcached
      - redis

  swoole:
    build:
      context: .
      dockerfile: php-swoole-dockerfile
    ports:
      - "9501:9501"
    volumes:
      - ./code:/var/www/html
      - ./php/php.ini:/usr/local/etc/php/php.ini
    working_dir: /var/www/html

  django:
    build:
      context: .
      dockerfile: python-django-dockerfile
    command: python code/hello_django/manage.py runserver 0.0.0.0:8989
    ports:
      - "8989:8989"
    volumes:
      - .:/code
    working_dir: /code

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: test
    ports:
      - "3306:3306"

  memcached:
    image: memcached:latest
    ports:
      - "11211:11211"
 
  redis:
    image: redis:latest
    ports:
      - "6379:6379"

php-dockerfile

FROM php:8.0-fpm

RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
RUN sed -i 's/security.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

RUN apt-get update && apt-get install libbrotli-dev zlib1g-dev build-essential

#安装PHP扩展
RUN docker-php-ext-install pdo_mysql mysqli bcmath
RUN pecl install redis

 

php-swoole-dockerfile

FROM php:8.0-fpm

RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
RUN sed -i 's/security.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

RUN apt-get update && apt-get install libbrotli-dev zlib1g-dev build-essential

#安装PHP扩展
RUN docker-php-ext-install pdo_mysql mysqli bcmath
RUN pecl install swoole

CMD ["php", "/var/www/html/swoole-script.php"]

 

python-django-dockerfile

# 指定使用的语言镜像
FROM python:3.10

# 设置环境变量,方便后续使用
ENV PYTHONUNBUFFERED 1

# 设置镜像内工作目录
WORKDIR /code

# 复制当前项目代码到工作目录中
COPY . /code/

# 安装项目依赖
#cd code
#cd hello_django
#pip freeze > requirements.txt
RUN pip install -r code/hello_django/requirements.txt

 

执行命令:

docker-compose up -d --build --remove-orphans

 

 

效果:

 

 

 

 

 

 

 

 

posted @ 2024-10-26 17:57  河北大学-徐小波  阅读(12)  评论(0编辑  收藏  举报