Laravel Sail 和 SQL Driver

Laravel Sail and SQL Driver

我想将 SQL Driver 添加到 Docker 文件,我将文件从 vendor/laravel/runtimes/8.0/Docker 文件复制到根项目并修改以添加 SQL 服务器 Driver 的依赖项。 这是我的根项目中的 Docker 文件:

FROM ubuntu:20.04

LABEL maintainer="Taylor Otwell"

ARG WWWGROUP

WORKDIR /var/www/html

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC

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


RUN apt-get update \
    && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 \
    && mkdir -p ~/.gnupg \
    && chmod 600 ~/.gnupg \
    && echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf \
    && apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E5267A6C \
    && apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C300EE8C \
    && echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu focal main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
    && apt-get update \
    && apt-get install -y php8.0-cli php8.0-dev \
       php8.0-pgsql php8.0-sqlite3 php8.0-gd \
       php8.0-curl php8.0-memcached \
       php8.0-imap php8.0-mbstring \
       php8.0-xml php8.0-zip php8.0-bcmath php8.0-soap \
       php8.0-intl php8.0-readline \
       php8.0-msgpack php8.0-igbinary php8.0-ldap \
       php8.0-redis \
       php-pear \
    && php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && curl -sL https://deb.nodesource.com/setup_15.x | bash - \
    && apt-get install -y nodejs \
    && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
    && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
    && curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list \
    && apt-get update \
    && apt-get install -y yarn \
    && ACCEPT_EULA=Y apt-get install -y msodbcsql17 \
    && ACCEPT_EULA=Y apt-get install -y mssql-tools \
    && apt-get install -y unixodbc-dev \
       gcc \
       musl-dev \
       make \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.0

RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail

RUN pecl install sqlsrv
RUN pecl install pdo_sqlsrv

RUN printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.0/mods-available/sqlsrv.ini
RUN printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.0/mods-available/pdo_sqlsrv.ini
RUN phpenmod sqlsrv pdo_sqlsrv

COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/8.0/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container

RUN source ~/.bashrc
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc

EXPOSE 8000

ENTRYPOINT ["start-container"]

而我的 docker-compose.yml 是

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: .
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
#            - mysql
            - redis
            # - selenium
    # selenium:
    #     image: 'selenium/standalone-chrome'
    #     volumes:
    #         - '/dev/shm:/dev/shm'
    #     networks:
    #         - sail
    #     depends_on:
    #         - laravel.test
#    mysql:
#        image: 'mysql:8.0'
#        ports:
#            - '${FORWARD_DB_PORT:-3306}:3306'
#        environment:
#            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
#            MYSQL_DATABASE: '${DB_DATABASE}'
#            MYSQL_USER: '${DB_USERNAME}'
#            MYSQL_PASSWORD: '${DB_PASSWORD}'
#            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
#        volumes:
#            - 'sailmysql:/var/lib/mysql'
#        networks:
#            - sail
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
    # memcached:
    #     image: 'memcached:alpine'
    #     ports:
    #         - '11211:11211'
    #     networks:
    #         - sail
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - 1025:1025
            - 8025:8025
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
#    sailmysql:
#        driver: local
    sailredis:
        driver: local

我评论了 mysql 的条目。我使用构建命令重建应用程序的容器:

sail build --no-cache

但我收到以下消息:

Step 17/24 : COPY start-container /usr/local/bin/start-container ERROR: Service 'laravel.test' failed to build: COPY failed: file not found in build context or excluded by .dockerignore: stat start-container: file does not exist

我该如何解决? 谢谢。

您的构建环境是 .那里没有启动容器。 例如,我的是:./vendor/laravel/sail/runtimes/8.0,它有启动容器脚本。

我找到了解决方案。

  1. 构建容器:
sail up

在 运行 此命令之前为 sail 创建别名。

  1. 发布 Dockerfile:
sail artisan sail:publish

此命令在应用程序的根目录中创建一个 docker 目录。

  1. 自定义 Dockerfile:在 docker/8.0/ 文件夹中打开 Dockerfile 并为 SQL 服务器的 ODBC 驱动程序和 SQL 服务器的 PHP 驱动程序添加条目. 检查此链接:

https://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-ubuntu?view=sql-server-ver15

https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#ubuntu17

https://www.microsoft.com/en-us/sql-server/developer-get-started/php/ubuntu/step/2.html

你能把我的 Dockerfile 配置放在要点中吗: https://gist.github.com/Sirpyerre/1db05b2ca681ea680646259805aee293

注意:此 Dockerfile 不添加 SQL 服务器容器。仅添加 SQL 服务器的 ODB 驱动程序和 SQL 服务器的 PHP 驱动程序,我使用的是 azure DB

  1. 检查 docker-compose.yml 的上下文并删除或评论 MySQL:
  2. 的条目
 build:
     context: ./docker/8.0
     dockerfile: Dockerfile
     args:
         WWWGROUP: '${WWWGROUP}'

示例: https://gist.github.com/Sirpyerre/b59d7e3c3bd00d9e29adb31cd386730b

  1. 使用构建命令重建应用程序的容器:
sail build --no-cache

查看 Laravel Sail 的文档: https://laravel.com/docs/8.x/sail#introduction