query in controller in dockerized laravel project - php

i dockerized my laravel project (9.25.1) and my problem is my query in tinker totally works and php artisan migrate works too but when i query in controller i get mysql error SQLSTATE[HY000] [2002] Connection refused
Dockerfile:
FROM php:8.1-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql sockets
RUN curl -sS https://getcomposer.org/installer​ | php -- \
--install-dir=/usr/local/bin --filename=composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR /app
COPY . .
RUN composer install
docker-composer.yml:
version: '3.8'
services:
main:
build:
context: .
dockerfile: Dockerfile
command: 'php artisan serve --host=0.0.0.0'
volumes:
- .:/app
ports:
- 8000:8000
db:
platform: linux/x86_64
image: mysql:8.0
environment:
MYSQL_DATABASE: main
MYSQL_USER: admin
MYSQL_ROOT: admin
MYSQL_PASSWORD: admin
MYSQL_ROOT_PASSWORD: root
volumes:
- ./storage/dbdata:/var/lib/mysql
ports:
- 4306:3306
.env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=4306
DB_DATABASE=main
DB_USERNAME=admin
DB_PASSWORD=admin

Check this. If your application will run in a docker container, you need to use internal port. In your scenario it's 3306. If you want to use tinker run it on docker container.
For example:
docker-compose exec main php artisan tinker
For other artisan commands:
docker-compose exec main php artisan migrate

Related

Docker container can't call localhost inside container itself

I have a docker-compose environment setup.
But inside service "Lumen" im trying to make a CURL request to the service itself.
However, the container can't access itself from localhost:8000 OR lumen:8000??
When I call lumen:8000 from the service it just never returns a response and just keeps loading ( And the curl request is to a different url so no infinite loop )
In my Laravel controller I found the protocal, host and port to be: http://lumen:8000
I seems like Laravel can't connect to itself, which I really need for my project.
I can connect to the Laravel from my own computer through localhost, but I need the Laravel to call it self.
Error message from Laravel controller after doing a CURL request:
Failed to connect to localhost port 8000 after 0 ms: Connection refused
Changing host to "lumen" just makes the request load infinite. No matter what page I try to connect to.
Docker-compose file:
version: "3.5"
services:
lumen:
expose:
- "8000"
ports:
- "8000:8000"
volumes:
- ./server:/var/www/html
- ./server/vendor:/var/www/html/vendor/
build:
context: server
dockerfile: Dockerfile
command: php -S lumen:8000 -t public
restart: always
privileged: true
depends_on:
- database
networks:
- database
frontend:
build:
context: client
dockerfile: Dockerfile
volumes:
- ./client/src:/app/src
ports:
- 3000:3000
stdin_open: true
#restart: always
networks:
- database
# Database Service (Mysql)
database:
image: mysql:latest
container_name: blogmoda_mysql
environment:
MYSQL_DATABASE: blogmoda-app
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci','--default-authentication-plugin=mysql_native_password']
ports:
- "127.0.0.1:3306:3306"
volumes:
- db-data:/var/lib/mysql
networks:
- database
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: dev_phpmyadmin
links:
- database
environment:
PMA_HOST: database
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
depends_on:
- database
ports:
- 9001:80
networks:
- database
volumes:
db-data:
# Networks to be created to facilitate communication between containers
networks:
database:
Server dockerfile:
FROM php:8.1-fpm-alpine
RUN apk update && apk add bash
RUN apk add chromium
RUN apk add --no-cache zip libzip-dev
RUN docker-php-ext-configure zip
RUN docker-php-ext-install zip
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install opcache
WORKDIR /var/www/html/
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
COPY . .
RUN composer install --ignore-platform-req=ext-zip --ignore-platform-reqs
I believe localhost should work. Assuming curl is installed in lumen, in your compose file under the lumen service can you try changing your command
command: php -S lumen:8000 -t public
to a direct curl via bash as
command: sh -c "curl -s localhost:8000"
Then check the logs of the lumen container to see whether or not the curl ran successfully.
Try 0.0.0.0:8000 instead localhost:8000. It works for localhost too

How to solve Laravel with sail "start-container": executable file not found

Im trying to deploy my laravel app using docker with laravel sail. Then I created docker-compose.yml file and Dockerfile like below.
docker-compose.yml
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
- '${HMR_PORT:-8080}:8080'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: '${DB_HOST}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sail-mysql:/var/lib/mysql'
- './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
Dockerfile
FROM php:7.4-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# 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
# Set working directory
WORKDIR /var/www
USER $user
But, when I try to run docker-compose up, I get this below error. I have been trying this for now 2 days. I installed sail properly.
ERROR: for 7fd8f64d5ed1_proj_laravel.test_1 Cannot start service laravel.test: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "start-container": executable file not found in $PATH: unknown
ERROR: for laravel.test Cannot start service laravel.test: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "start-container": executable file not found in $PATH: unknown
ERROR: Encountered errors while bringing up the project.
Here is my .env
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=here_goes_db_name
DB_USERNAME=sail
DB_PASSWORD=tyutyu
How can I fix this ??
The default docker container should copy a bash script called start-container from your project root to the container during build. If you for example removed it from your project root, it can not copy it and run it later. So the steps to fix it are:
Restore the start-container script to your project root, you can find it here
Rebuild the image with docker-compose build
And everything should work again. I suspect step 2 is even optional as the entire project gets mounted as a volume.

Configure PHP/Apache (Lumen Project), MySQL ECR containers on AWS EC2 (Fargate)

I have uploaded container images for PHP/Apache (Lumen Project) & MySQl to AWS ECR successfully from my local development environment.
I am using Docker Desktop 4.5.1 (74721) on Windows 10.
But when I create Task definitions & services on AWS EC2, I get errors related to MySQL.
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
I am sharing my Dockerfile & docker-compose.yml files below that I used for the same process.
Dockerfile
FROM php:8.0-apache
RUN docker-php-ext-install pdo_mysql
WORKDIR /var/www/html/
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
COPY . ./Author-API
WORKDIR /var/www/html/Author-API
RUN composer install
COPY mysites.conf /etc/apache2/sites-available/mysites.conf
RUN a2enmod rewrite &&\
a2dissite 000-default.conf &&\
a2ensite mysites.conf &&\
service apache2 restart
EXPOSE 8085
Docker-compose.yml
version: '3.5'
services:
php:
container_name: "php_apache_service"
build:
context: .
dockerfile: Dockerfile
ports:
- "8090:8085"
volumes:
- .:/var/www/html/Author-API
restart: always
depends_on:
- db
links:
- db
db:
image: mysql
container_name: "mysql_service"
command: --default-authentication-plugin=mysql_native_password
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: author_api_db
volumes:
- ./database/mysql-data:/var/lib/mysql:rw
adminer:
image: adminer
container_name: "adminer_service"
restart: always
ports:
- 8080:8080
volumes:
mysql-data:
My Lumen project's .env file:
APP_NAME=Lumen
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
APP_TIMEZONE=UTC
LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=author_api_db
DB_USERNAME=root
DB_PASSWORD=example
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
On my local development machine everything works fine using docker-compose up -d.
But on AWS EC2 I couldn't find a way to configure MySQL properly or am I missing something?
Waiting to hear from you...
Thank you

Cannot not connect to database server within docker-compose (php and MariaDB)

I've built a docker-compose file that creates 2 services (PHP and MariaDB). Somehow I cannot connect to the database from the PHP service: Within the PHP service, a Laravel-app is running.
The error message (redirect is a table within the database):
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from redirects)
All env-variables should be correct.
Here is my docker-compose.yml:
version: "3.7"
services:
faaren_backend:
image: php:alpine
container_name: FAA-Backend
volumes:
- "./:/faaren_backend"
working_dir: /faaren_backend
command: "php artisan serve --host=0.0.0.0 --port=8000"
ports:
- 8000:8000
build:
context: docker/php
dockerfile: dev.Dockerfile
faaren_database:
image: mariadb
container_name: FAA-Database
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_DATABASE: ${DB_DATABASE}
volumes:
- "faa-db-data:/var/lib/mysql/data"
volumes:
faa-db-data: {}
My dev.Dockerfile
FROM php:7.3-fpm
RUN apt-get update
RUN apt-get update && apt-get install -y libzip-dev \
&& docker-php-ext-install zip
RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN pecl install imagick
RUN docker-php-ext-enable imagick
RUN apt-get install $PHPIZE_DEPS && \
pecl install xdebug && docker-php-ext-enable xdebug && \
docker-php-ext-install pdo_mysql pcntl
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
VOLUME /faaren_backend
And finally my .env-file:
DB_CONNECTION=mysql
DB_HOST=faaren_backend
DB_PORT=3306
DB_DATABASE=faaren_db
DB_USERNAME=root
DB_PASSWORD=
Mostly I've followed this tutorial: https://medium.com/swlh/laravel-with-docker-compose-de2190569084
You need change your docker database host:
DB_CONNECTION=mysql
DB_HOST=faaren_database <-- edit
DB_PORT=3306
DB_DATABASE=faaren_db
DB_USERNAME=root
DB_PASSWORD=
And in docker-compose.yml faaren_backend section add:
depends_on:
- faaren_database
Hope it's help.
Looks like you deal with a race condition. You need to use depends_on directive to control startup order and use wait script to make sure that service ready to accept connections:
version: "3.7"
services:
faaren_backend:
image: php:alpine
container_name: FAA-Backend
volumes:
- "./:/faaren_backend"
working_dir: /faaren_backend
command: entrypoint.sh
ports:
- 8000:8000
depends_on:
- faaren_database
build:
context: docker/php
dockerfile: dev.Dockerfile
faaren_database:
image: mariadb
container_name: FAA-Database
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_DATABASE: ${DB_DATABASE}
volumes:
- "faa-db-data:/var/lib/mysql/data"
volumes:
faa-db-data: {}
There are a bunch of ways to check service availability, sometimes wait-for-it can be really handy. In the example below used netcat(you need to install it into the container).
Also, it'a good idea to put a script into a separated file:
entrypoint.sh
#!/usr/bin/env bash
until nc -w 1 -z faaren_database 3306; do
>&2 echo "Mysql is unavailable - sleeping"
sleep 1
done
sleep 10
>&2 echo "Mysql is up - executing command"
php artisan serve --host=0.0.0.0 --port=8000
UPDATE: Specify correct DB_HOST how Dmitry suggested as well.

Fig apparently not using volume_from on mysql service

I'm trying to setup a very simple setup of a mysql database using a data-container as repository using Fig.sh and Docker.
The code below is self-explanatory:
web:
build: .
command: php -S 0.0.0.0:8000 -t /code
ports:
- "8000:8000"
links:
- db
volumes:
- .:/code
dbdata:
image: busybox
command: /bin/sh
volumes:
- /var/lib/mysql
db:
image: mysql
volumes_from:
- dbdata
environment:
MYSQL_DATABASE: database
MYSQL_ROOT_PASSWORD: rootpasswd
For some reason, if I run a command fig run --rm dbdata /bin/sh and then I cd into the directory /var/lib/mysql. The folder is empty. If I run fig run --rm db /bin/sh and cd into /var/lib/mysql the database is being created there.
What am I doing wrong here? And taking advantage of the question, is this the correct setup or I should let the data inside the mysql container?
Thanks.

Categories