How to solve Laravel cant connect with mysql through docker - php

Im trying to deploy my laravel app using docker. Then I created docker-compose.yml file and Dockerfile like below.
docker-compose.yml
version: "3.7"
services:
app:
build:
args:
user: sammy
uid: 1000
context: ./
dockerfile: Dockerfile
image: travellist
container_name: travellist-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- travellist
db:
image: mysql:8.0
container_name: travellist-db
restart: unless-stopped
environment:
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
networks:
- travellist
nginx:
image: nginx:alpine
container_name: travellist-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- travellist
networks:
travellist:
driver: bridge
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
And when I try docker-compose up, all the containers run with a any error. And also when I run docker-compose ps, it shows like this,
Name Command State Ports
------------------------------------------------------------------------------------------------
travellist-app docker-php-entrypoint php-fpm Up 9000/tcp
travellist-db docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp
travellist-nginx /docker-entrypoint.sh ngin ... Up 0.0.0.0:8000->80/tcp,:::8000->80/tcp
But, when laravel application, tries to connect with mysql, it doesnt happens. And even I cant connect using TablePlus, it shows this.
Lost connection to MySQL server at 'waiting for initial communication packet', system error: 10060
And sometimes, I get like below,
SQLSTATE[HY000] [1045] Access denied for user 'root'#'192.168.48.3' (using password: YES)
How can I fix this ??

Since you're networking the services, you only need to expose the port 3306/tcp (this is the default port mysql serves). This makes it inaccessible from 0.0.0.0 but accessible from your nginx and app container.
I have also added the correct volume to persist data (which I am assuming is what you're trying to do). I moved mysql to latest - feel free to rollback or stick to 8 if you really? need too.
db:
image: 'mysql:latest'
restart: 'unless-stopped'
expose:
- '3306'
environment:
- 'MYSQL_RANDOM_ROOT_PASSWORD=true'
- 'MYSQL_DATABASE=${DB_DATABASE}'
- 'MYSQL_USER=${DB_USER}'
- 'MYSQL_PASSWORD=${DB_PASSWORD}'
- 'MYSQL_ALLOW_EMPTY_PASSWORD=true'
volumes:
- 'laravel-database:/var/lib/mysql/'
networks:
- 'travellist'
volumes:
laravel-database:
Further reading can be found on the Docker hub under mysql.
Inside your Laravel .env, you should connect to the container via its DNS.
DB_HOST=db
If you want to connect to this container externally, you'll need to bind the port to the server. Do so by replacing expose with the following:
ports:
- '3306:3306'

Related

PHP Pest (with Laravel) tests running very slow in OSX Docker

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?

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

MySQL Docker Image is always restarting status

I followed step by step this article ( https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose-on-ubuntu-20-04 ).
I just changed php version (8.1) instead of (7.4) and mysql version (8.0) instead of (5.7.22)
When I run (docker ps) command, digitalocean.com/php and nginx:alpine are fine. But mysql:8.0 is always restarting status.
But I tested firstly it in local development.
When local development, everything is okay.
Buy I deployed it in the droplet, I faced the error that MySQL is always restarting status.
When I run (docker logs -f ) command, I faced the following error.
2022-08-13 06:35:20+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
2022-08-13 06:35:20+00:00 [Note] [Entrypoint]: Switching to dedicated user ‘mysql’
2022-08-13 06:35:20+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
‘/var/lib/mysql/mysql.sock’ -> ‘/var/run/mysqld/mysqld.sock’
When I run (docker run --restart=always mysql:8.0) command, I faced the following error.
2022-08-13 06:57:37+00:00 [Note] [Entrypoint]: Entrypoint script - for MySQL Server 8.0.30-1.el8 started.
2022-08-13 06:57:38+00:00 [Note] [Entrypoint]: Switching to dedicated user ‘mysql’ 2022-08-13 06:57:38+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
2022-08-13 06:57:38+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following:
MYSQL_ROOT_PASSWORD
MYSQL_ALLOW_EMPTY_PASSWORD
MYSQL_RANDOM_ROOT_PASSWORD
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 \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
libzip-dev
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# 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
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
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:8.0
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: 123123
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

Why booting up docker container using blimp throws an error?

When I boot up a container with below configuration using Blimp (Docker Compose in a cloud), it throws an error Exited: OCI runtime create failed: container_linux.go:345: starting container process caused "chdir to cwd (\"/var/www/html\") set in config.json failed: permission denied": unknown. Booting with docker-compose works just fine.
The problem is with volume mounting as far as I know.
docker-compose.yml
version: '3'
services:
app:
container_name: app
build:
context: ./
dockerfile: Dockerfile
args:
user: mk
uid: 1000
tty: true
volumes:
- ./src:/var/www/html
nginx:
container_name: nginx
image: nginx:stable-alpine
ports:
- "8080:80"
volumes:
- ./src:/var/www/html
- ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- app
- mysql
networks:
- default
mysql:
container_name: mysql
image: mysql:5.7
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
volumes:
- ./docker/mysql:/var/lib/mysql
networks:
- default
networks:
default:
driver: bridge
Dockerfile
FROM php:7.4-fpm
LABEL MAINTAINER="Mayur Shingrakhiya <mk.shingrakhiya#gmail.com>"
RUN mkdir -p /var/www/html
ARG user=mk
ARG uid=1000
RUN apt-get update && apt-get install -y git curl libpng-dev libonig-dev libxml2-dev zip unzip
RUN docker-php-ext-install bcmath exif gd mbstring opcache pcntl pdo_mysql
# Get the Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="1"
ENV PHP_OPCACHE_MAX_ACCELERATED_FILES="10000"
ENV PHP_OPCACHE_MEMORY_CONSUMPTION="192"
ENV PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10"
COPY ./docker/php/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
# Create a User
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && chown -R $user:$user /home/$user
RUN chmod -R 775 /var/www/html
WORKDIR /var/www/html
USER $user
You won't find anything related to Blimp as it is not yet publicly released.
Help will be appreciated.
can you try RUN chmod -R 777 /var/www/html and then see if u get this error ?

Docker image pushing but my volume not persist

This message to clear up a problem I am having with the use of docker with my php application.
Indeed, I execute locally my dockers images (nginx, phpmyadmin and php with my application) and everything works fine.
However, I use a volume mounted in my container app with php which allows me to be able to modify hot files (without need to build at each edit).
However when I push this image to a repository and I pull it on another desktop, the volume containing my application is not there.
Have you ever faced this concern?
Please find my docker-compose.yml and Dockerfile :
docker-compose.yml
version: "3.7"
services:
app:
build:
args:
user: web
uid: 1000
context: ./
dockerfile: Dockerfile
image: myblog
container_name: myblog-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- myblog
db:
image: mysql:5.7
container_name: myblog-db
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./.docker/mysql/database.sql:/docker-entrypoint-initdb.d/init.sql
- ./.docker/mysql/data:/var/lib/mysql
networks:
- myblog
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8002:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
networks:
- myblog
nginx:
image: nginx:alpine
container_name: myblog-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./.docker/nginx/conf.d:/etc/nginx/conf.d
networks:
- myblog
networks:
myblog:
driver: bridge
Dockerfile
FROM php:7.3-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 \
libzip-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
RUN docker-php-ext-install zip
RUN docker-php-ext-enable zip
# 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
Thanks in advance
This is expected behavior, because the data inside Volumes are not part of an image. Volumes are used to persist data generated in containers or to pass dynamic data into containers via bind-mounts e.g. configs, credentials or certificates.
https://docs.docker.com/storage/volumes/
Your docker-compose.yml and its services using volumes mounting your local directory via - .:/path/to/dir are only good for local development, because you may see changes of your application instant and without having to rebuild images.
If you want to see your code inside the images on another machine you need to use COPY in your Dockerfile, rebuild the image and push every time you change your code !
You will also need to change your docker-compose.yml by adding volumes.
https://docs.docker.com/compose/compose-file/#volumes
Many thanks for your clear reply.
Now i understand what is wrong with my configuration and what i need to do.
For the people who search a solution to manage in the same Dockerfile the development environment and the production, you can use an Argument :)
Have you tried using a named volume instead of a path based volume?
This will let Docker manage the volume more for you and may give you the behavior you want.
https://nickjanetakis.com/blog/docker-tip-28-named-volumes-vs-path-based-volumes
Docker volume does not persist data

Categories