Search code examples
sql-serverdockerlaravel-sail

Laravel Sail and SQL Driver


I want to add SQL Driver to Docker file, I copied the file from vendor/laravel/runtimes/8.0/Dockerfile to root project and modified for add the dependencies for SQL Server Driver. This is my Dockerfile in my root project:

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"]

And my docker-compose.yml is

# 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

I commented the entries for mysql. I rebuild my application's containers using the build command:

sail build --no-cache

But I have the below message:

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

How I can fix it? Thank you.


Solution

  • I found the solution.

    1. Build the container with:
    sail up
    

    Create the alias to sail before run this command.

    1. Publish the Dockerfile:
    sail artisan sail:publish
    

    This command create a docker directory in your application's root directory.

    1. Custumize the Dockerfile: in docker/8.0/ folder open the Dockerfile and add the entries for ODBC Driver for SQL Server and PHP Driver for SQL Server. Check this links:

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

    https://learn.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

    Can you take my Dockerfile configuration in the gist: https://gist.github.com/Sirpyerre/1db05b2ca681ea680646259805aee293

    Note: this Dockerfile don't add SQL Server container. Only add ODB Driver for SQL Server and PHP Driver for SQL Server, I use an azure DB

    1. Check the context for docker-compose.yml and remove or comment the entries for MySQL:
     build:
         context: ./docker/8.0
         dockerfile: Dockerfile
         args:
             WWWGROUP: '${WWWGROUP}'
    
    

    Example in: https://gist.github.com/Sirpyerre/b59d7e3c3bd00d9e29adb31cd386730b

    1. Rebuild your application's containers using the build command:
    sail build --no-cache
    

    Check the documentation for Laravel Sail: https://laravel.com/docs/8.x/sail#introduction