I've been trying to run my new REST api calls but i get this error on postman:
Illuminate\Database\QueryException: could not find driver (SQL: insert into "companies" ("name", "establishment", "updated_at", "created_at") values (Tekmon, 2021-08-10, 2021-08-31 19:29:31, 2021-08-31 19:29:31) returning "id") in file /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 671
I have succesfully run my migrations to create the database tables.
phpinfo on docker container does not have pdo_pgsql extension on the list.
Dockerfile:
FROM php:8.0.7-fpm
COPY composer.lock composer.json /var/www/
#Set working directory
WORKDIR /var/www
#Install dependecies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
libzip-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
postgresql-dev
#Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
#Install Extensions
RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql
RUN docker-php-ext-install pdo pdo_pgsql pgsql zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd
#Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
#Add user for laravel app
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
#Copy existing application directory contents
COPY . /var/www
#Copy existing application directory permissions
COPY --chown=www:www . /var/www/
#Change current user to www
USER www
#Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
Docker-compose file:
version: '3'
services:
#POSTGRES Service
db:
image: postgres
container_name: db
restart: unless-stopped
tty: true
ports:
- "5432:5432"
environment:
POSTGRES_DB: tekmondb
POSTGRES_PASSWORD: tekmonpass
POSTGRES_USER: tekmon
volumes:
- dbdata:/var/lib/postgresql/data
networks:
- app-network
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#Docker networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
Laravel db environment: DB_CONNECTION=pgsql
config/database.php: 'default' => env('DB_CONNECTION', 'pgsql'),
I've searched many questions for some hours now but nothing worked.
Thanks.
As Sammitch below mentioned, and a video on YouTube by BPKodes, I needed to rebuild the container, which I didn't in the beginning, with libpq-dev instead of postgresql-dev. Hope this helps anyone.
Related
I have a docker compose and Dockerfile, when building the project builds and the containers run but gives the site gives the error:
The stream or file "/var/www/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: The stream or file "/var/www/storage/logs/laravel.log" could not be opened in append mode
docker-compose.yml:
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
Dockerfile:
FROM php:8.1-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
mysql-client \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
nano \
curl \
libzip-dev
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
COPY . /var/www/
COPY --chown=www:www . /var/www
USER www
EXPOSE 9000
CMD ["php-fpm"]
"docker-compose exec app chown -R www-data:www-data /var/www"
solved my problem
I was developing an app locally using Laravel. For testing I was using Pest and tests were running fast, really fast. That was until I decided to Dockerize my app, now tests are running pretty slow.
What it used to run in 3 seconds now it's running in over a minute.
Here's my Dockerfile:
FROM php:8.1.12-fpm
ARG uid=1000
ARG user=macgiver
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Install and enable xDebug
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install php modules required by laravel.
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Create system user to run Composer and Artisan commands.
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
# Install composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /var/www
USER $user
and this is my docker-compose:
version: "3.9"
services:
app:
build:
context: ./
dockerfile: Dockerfile
image: dmc
container_name: dmc-app
restart: unless-stopped
working_dir: /var/www/
depends_on:
- db
- nginx
volumes:
- ./:/var/www/
- ./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
- ./docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
expose:
- "9003"
networks:
- dmc-net
nginx:
image: nginx:1.23.2-alpine
container_name: dmc-nginx
restart: unless-stopped
ports:
- "8000:80"
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d
networks:
- dmc-net
db:
image: mysql:8.0.31
container_name: dmc-db
restart: unless-stopped
# using 3307 on the host machine to avoid collisions in case there's a local MySQL instance installed already.
ports:
- "3307:3306"
# use the variables declared in .env file
environment:
MYSQL_HOST: ${DB_HOST}
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_ROOT_PASSWORD: abcd1234
MYSQL_USER: ${DB_USERNAME}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
- mysql-data:/var/lib/mysql
networks:
- dmc-net
networks:
dmc-net:
driver: bridge
volumes:
mysql-data:
Any ideas?
Ubuntu 20.04
Docker version 19.03.14
docker-compose version 1.25.0
I'm trying to figure out why i'm getting an error related to mysql. The steps i did:
Clone my laravel project
docker-compose build
docker-compose up -d
docker-compose exec app php artisan key:generate
docker-compose exec app php artisan config:cache
docker-compose exec app php artisan migrate
in the last one i get the following error:
Migration table created successfully.
Loading stored database schema: /var/www/database/schema/mysql-schema.dump
Symfony\Component\Process\Exception\ProcessFailedException
The command "mysql --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --database="${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"" failed.
Exit Code: 127(Command not found)
Working directory: /var/www
Output:
================
Error Output:
================
sh: 1: mysql: not found
at vendor/symfony/process/Process.php:257
253▕ */
254▕ public function mustRun(callable $callback = null, array $env = []): self
255▕ {
256▕ if (0 !== $this->run($callback, $env)) {
➜ 257▕ throw new ProcessFailedException($this);
258▕ }
259▕
260▕ return $this;
261▕ }
+19 vendor frames
20 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
My files:
docker-compose.yml
version: '3.7'
services:
# The Application
app:
build:
context: .
dockerfile: docker-files/app.dockerfile
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
volumes:
- ./:/var/www
- ./docker-files/php/local.ini:/usr/local/etc/php/conf.d/local.ini
env_file: '.env'
networks:
- app-network
# The Web Server
nginx:
container_name: nginx
restart: unless-stopped
tty: true
build:
context: .
dockerfile: docker-files/web.dockerfile
volumes:
- ./:/var/www
- ./docker-files/nginx/conf.d/:/etc/nginx/conf.d/
ports:
- "8990:80"
- "443:443"
networks:
- app-network
# The Database
db:
container_name: db
restart: unless-stopped
tty: true
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql/
- ./docker-files/mysql/my.cnf:/etc/mysql/my.cnf
environment:
MYSQL_DATABASE: maindbs
MYSQL_USER: root
MYSQL_PASSWORD: 123456
MYSQL_ROOT_PASSWORD: 123456
ports:
- "3306:3306"
networks:
- app-network
#Volumes
volumes:
dbdata:
driver: local
#Docker Networks
networks:
app-network:
driver: bridge
app.dockerfile
FROM php:7.4-fpm
COPY composer.lock composer.json /var/www/
COPY database /var/www/database
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libzip-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Copy application folder
COPY . /var/www
RUN chown -R www-data:www-data \
/var/www/storage \
/var/www/bootstrap/cache
RUN php artisan optimize
EXPOSE 9000
CMD ["php-fpm"]
web.dockerfile
FROM nginx:alpine
ADD docker-files/nginx/conf.d/app.conf /etc/nginx/conf.d/default.conf
COPY public /var/www/public
.env
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=maindbs
DB_USERNAME=root
DB_PASSWORD=123456
I can connect to laravel app using 127.0.0.1:8990
You don't have mysql client in the container that tries to execute mysql command.
Add mysql via the Dockerfile.
RUN apt-get update && apt-get install -y mysql-client && rm -rf /var/lib/apt
I have the following DockerFile and docker-compose.yml for an Laravel application. I have just added Redis today and this appeared in my terminal in the end of docker-compose up when I tried to connect and use Redis from PHP.
zinc_app | [23-Dec-2019 11:31:27] ALERT: oops, unknown child (32) exited on signal 2 (SIGINT). Please open a bug report (https://bugs.php.net).
zinc_app | [23-Dec-2019 11:31:27] ALERT: oops, unknown child (31) exited on signal 2 (SIGINT). Please open a bug report (https://bugs.php.net).
zinc_app | [23-Dec-2019 11:32:09] ALERT: oops, unknown child (20) exited on signal 9 (SIGKILL). Please open a bug report (https://bugs.php.net).
I am pretty sure the error came from the PHP Redis extension, because it didn't have the problem before. Have anyone encountered this problem before? Have I configured anything wrong?
I have tested this on another computer, but it didn't exhibit the same behavior. I have tried docker system prune -a and build and up again, but my computer still has the same problem.
As for the code I have run. I have run this in artisan tinker.
$redis = new Redis();
$redis->connect('zinc_redis', 6379);
$redis->get('1'); // The Oops message originated from this line.
Docker Compose:
version: '3.6'
services:
#PHP Service
zinc_app:
build:
context: .
dockerfile: Dockerfile
image: zinc/php
container_name: zinc_app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: zinc_app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- /tmp:/tmp #For CS Fixer
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
zinc_webserver:
image: nginx:alpine
container_name: zinc_webserver
restart: unless-stopped
tty: true
ports:
- "8080:80"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#Postgres Service
zinc_db:
image: postgres:12-alpine
container_name: zinc_db
restart: unless-stopped
tty: true
ports:
- "5432:5432"
environment:
POSTGRES_DB: zinc
POSTGRES_PASSWORD: admin
SERVICE_TAGS: dev
SERVICE_NAME: postgres
volumes:
- dbdata:/var/lib/postgresql
- ./postgres/init:/docker-entrypoint-initdb.d
networks:
- app-network
zinc_redis:
image: redis:5.0-alpine
container_name: zinc_redis
restart: unless-stopped
tty: true
ports:
- "6379:6379"
volumes:
- ./redis/redis.conf:/usr/local/etc/redis/redis.conf
- redisdata:/data
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
redisdata:
driver: local
FROM php:7.4-fpm
# Switch to root user
USER root
# Set working directory
WORKDIR /var/www
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
RUN chown www:www /var/www
# Copy composer
COPY --chown=www:www composer.json composer.lock /var/www/
# Install dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \
build-essential \
libpq-dev \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
libzip-dev \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pgsql pdo_pgsql zip exif pcntl
# Enable igbinary & PHP Redis ext using igbinary
RUN pecl install -o -f igbinary && y | pecl install -o -f redis && docker-php-ext-enable redis igbinary
RUN rm -rf /tmp/pear
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
COPY --chown=www:www . /var/www/
# Change current user to www
USER www
# Install packages
RUN composer global require hirak/prestissimo && composer install
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
I am setting up a development environment with the docker. In it I need
NGINX (webservice)
PHP 7.2 (app)
MARIADB 10.3 (database)
In the app container I must install composer, I will work using laravel.
Using RUN composer install andCOPY .env.example .env in dockerfile to install vendor and configure.env, I did not get any error messages but no files were created either.
docker-compose.yml
version: '3'
services:
app:
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
build:
context: .
dockerfile: Dockerfile
image: php:7.2
container_name: app
restart: unless-stopped
tty: true
working_dir: /var/www
networks:
- app-network
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
networks:
- app-network
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
db:
image: mariadb:10.3
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: db
MYSQL_ROOT_PASSWORD: pass
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- app-network
volumes:
- dbdata:/var/lib/mysql
networks:
app-network:
driver: bridge
volumes:
dbdata:
driver: local
dockerfile
FROM php:7.2-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
mariadb-client-10.3 \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Run composer install
RUN composer install
# Create the .env from the .env.example
COPY .env.example .env
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
I have created a Setup to deploy a Laravel Application with the help of NGINX Web Server and MYSQL Database at Github.
It should be pretty easy to set up. If you encounter any issues, please feel free to raise an issue at Github, or ask me here.