Database connection refused for Laravel artisan command with Docker - php

I am trying to run Laravel using Docker locally. Everything works fine expect the database. This is how my docker-compose looks like:
services:
app:
build:
context: ./
dockerfile: ./docker/Dockerfile
container_name: my-app
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- my-network
db:
image: mysql:8.0
container_name: my-db
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
ports:
- 3307:3306
volumes:
- ./docker/mysql:/docker-entrypoint-initdb.d
networks:
- my-network
nginx:
image: nginx:alpine
container_name: my-nginx
ports:
- 8001:80
volumes:
- ./:/var/www
- ./docker/nginx/nginx.conf:/etc/nginx/conf.d
networks:
- my-network
My .env file looks like this:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=admin
DB_PASSWORD=
When I run php artisan migrate:fresh within the app container, I am getting the following message:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
Can someone see what is the problem here?
UPDATE: When I run docker-compose ps I can see
NAME COMMAND SERVICE STATUS PORTS
my-app "docker-php-entrypoi…" app running 9000/tcp
my-db "docker-entrypoint.s…" db restarting
my-nginx "/docker-entrypoint.…" nginx running 0.0.0.0:8001->80/tcp, :::8001->80/tcp
This is the log when I run docker-compose logs db
my-db | 2021-07-29T20:18:37.340769Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.20) initializing of server in progress as process 104
my-db | 2021-07-29T20:18:37.354423Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
my-db | 2021-07-29T20:18:37.354605Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
my-db | 2021-07-29T20:18:37.356184Z 0 [ERROR] [MY-010119] [Server] Aborting
my-db | 2021-07-29T20:18:37.358746Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.20) MySQL Community Server - GPL.

Related

PHP-FPM docker container got different behavior depending engine (docker-compose and swarm)

One PHP-FPM container (7.2), member of classical LAPP stack, correctly exposes port 9000 when launched with docker-compose but nothing when started by Docker Swarm. Should I mention that container is running (see output below) and I can launch a TTY bash on it.
So with docker-compose, web application is fully functionnal, but with Swarm, Apache issues a 503 Service Unavailable. Nothing is changed between tests with different engines. Do you have any clue about this behaviour ?
docker-compose.yml
version: '3.5'
services:
postgres:
env_file: ./.env
image: "postgres:9.5.24"
volumes:
- database:/var/lib/postgresql/data
networks:
- internal
adminer:
image: adminer
networks:
- internal
- traefik-public
deploy:
labels:
- "traefik.enable=true"
- "traefik.http.routers.adminer.rule=Host(`adminer.example.com`)"
- "traefik.http.routers.adminer.entrypoints=websecure"
- "traefik.http.routers.adminer.tls.certresolver=letsencryptresolver"
- "traefik.http.services.adminer.loadbalancer.server.port=8080"
- "traefik.port=80"
apache:
env_file: ./.env
image: visio_rdv_apache:latest
build:
context: docker/images/apache2.4
dockerfile: Dockerfile
args:
UID: ${HOST_UID}
ROOT_URL: ${ROOT_URL}
ports:
- 8000:80
environment:
FPM_HOST: php:9000
ROOT_URL: ${ROOT_URL}
volumes:
- ${LOGS_DIR}/apache/:/var/log/httpd/
- ${RELATIVE_APP_PATH}:/var/www/html
links:
- postgres
- php
networks:
- internal
- traefik-public
deploy:
labels:
- "traefik.enable=true"
- "traefik.http.routers.visio_rdv.rule=Host(`test.example.com`)"
- "traefik.http.routers.visio_rdv.entrypoints=websecure"
- "traefik.http.routers.visio_rdv.tls.certresolver=letsencryptresolver"
- "traefik.http.services.visio_rdv.loadbalancer.server.port=80"
- "traefik.port=80"
php:
env_file: .env
links:
- ftp
image: visio_rdv_php:latest
build:
context: docker/images/php
dockerfile: Dockerfile
args:
UID: ${HOST_UID}
TIMEZONE: ${TIMEZONE}
PROXY: ${http_proxy}
volumes:
- ftp_data:/var/www/ftp:rw
- ${RELATIVE_APP_PATH}:/var/www/html
networks:
- internal
ftp:
env_file: .env
image: "fauria/vsftpd:latest"
ports:
- "9520:20"
- "9521:21"
- "21100-21110:21100-21110"
environment:
FTP_USER: ${FTP_USER}
FTP_PASS: ${FTP_PASS}
PASV_MIN_PORT: 21100
PASV_MAX_PORT: 21100
volumes:
- ftp_data:/home/vsftpd/${AUTOMATION_CLIENT_NAME}:rw
networks:
- internal
volumes:
ftp_data:
database:
networks:
traefik-public:
external: true
internal:
external: false
With Docker Swarm
Apache logs :
[Wed Aug 04 07:35:16.382246 2021] [proxy:error] [pid 11] (111)Connection refused: AH00957: FCGI: attempt to connect to 10.0.4.93:9000 (*) failed
[Wed Aug 04 07:35:16.382314 2021] [proxy_fcgi:error] [pid 11] [client 10.0.0.2:1035] AH01079: failed to make connection to backend: php, referer: http://test.example.com/
Swarm services list :
docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
um2txtpz8534 traefik_reverse-proxy replicated 1/1 traefik:v2.4
ahf7uukapxbr my_stack_adminer replicated 1/1 adminer:latest
ue8qekmg0ff0 my_stack_apache replicated 1/1 my_stack_apache:latest my_stack*:8000->80/tcp
x9igaslhsx11 my_stack_ftp replicated 1/1 fauria/vsftpd:latest *:9520-9521->20-21/tcp, *:21100-21110->21100-21110/tcp
jpk4qzqs1tfx my_stack_php replicated 1/1 my_stack_php:latest
ih67rh82vp9e my_stack_postgres replicated 1/1 postgres:9.5.24
I tried to add net-tools to the PHP image, in order to manually test the connection with nc. Same result, php container instance IP is correctly resolved, but any TCP connection to port 9000 is refused.
I use a lot environment vars, and in order to palliate missing feature with Swarm, I use the docker-compose config command output to generate complete docker-compose.yml file.
With docker-compose
Name Command State Ports
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
my_stack_adminer_1 entrypoint.sh docker-php-e ... Up 8080/tcp
my_stack_apache_1 /bin/sh -c apachectl -D FO ... Up 0.0.0.0:8000->80/tcp
my_stack_ftp_1 /usr/sbin/run-vsftpd.sh Up 0.0.0.0:9520->20/tcp, 0.0.0.0:9521->21/tcp, 0.0.0.0:21100->21100/tcp, 0.0.0.0:21101->21101/tcp, 0.0.0.0:21102->21102/tcp,
0.0.0.0:21103->21103/tcp, 0.0.0.0:21104->21104/tcp, 0.0.0.0:21105->21105/tcp, 0.0.0.0:21106->21106/tcp, 0.0.0.0:21107->21107/tcp,
0.0.0.0:21108->21108/tcp, 0.0.0.0:21109->21109/tcp, 0.0.0.0:21110->21110/tcp
my_stack_php_1 php-fpm -R -F Up 9000/tcp
my_stack_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp
Here are a few things I'd try:
Use docker service inspect to see if the configuration matches what you expect it to be.
Jump into the php container and see what port it's actually listening on.
Did you intend to expose the apache server directly? I would imagine you'd use traefik in front of that since you already have in the mix.

Docker swarm php-fpm container keeps restarting

I created a basic LAPP stack which runs flawlessly on localhost with docker-compose. When I try to make it run with Swarm on production server (1 manager only, no workers), all services go up and get replicated (1/1) but the php-fpm one which keeps restarting with no apparent error.
docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
p0rdrdfmso6x traefik_reverse-proxy replicated 1/1 traefik:v2.4 *:80->80/tcp, *:443->443/tcp
e6vwlo9iw2ny my_stack_apache replicated 1/1 apache:latest *:8000->80/tcp
qy5yigbcjryr my_stack_ftp replicated 1/1 fauria/vsftpd:latest *:20-21->20-21/tcp, *:22100-22110->22100-22110/tcp
n5f9v6bd2854 my_stack_php replicated 0/1 php:latest
rcnbq4vnoz1j my_stack_postgres replicated 1/1 postgres:9.5.24
If we focus on php-fpm container :
docker service ps my_stack_php
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
j6mp3ka40cyo my_stack_php.1 php:latest node.address Ready Ready 2 seconds ago
ezztpsjoglwy \_ my_stack_php.1 php:latest node.address Shutdown Complete 3 seconds ago
gnqjhwpi5y72 \_ my_stack_php.1 php:latest node.address Shutdown Complete 9 seconds ago
0agr3tw0bb9g \_ my_stack_php.1 php:latest node.address Shutdown Complete 15 seconds ago
9a6wsdp4tqqn \_ my_stack_php.1 php:latest node.address Shutdown Complete 21 seconds ago
If I look to the logs :
docker service logs my_stack_php
my_stack_php.1.igd8x7a6ysdi#node.address | Interactive shell
my_stack_php.1.57td5iuk1wwy#node.address | Interactive shell
my_stack_php.1.r03jn931l1uf#node.address | Interactive shell
my_stack_php.1.igd8x7a6ysdi#node.address |
my_stack_php.1.r03jn931l1uf#node.address |
my_stack_php.1.1huf2pdd0bq0#node.address | Interactive shell
my_stack_php.1.57td5iuk1wwy#node.address |
my_stack_php.1.1huf2pdd0bq0#node.address |
It behaves like a container running a command which would end with succes in a few seconds. Swarm launches then an new container to keep the restart contract. However, my php-fpm Dockerfile provide the -F argument that should keep the process running :
PHP Dockerfile
FROM centos:7.4
# ... all installs from Centos to add PHP 7.2 from Remi Collet repositories
RUN mkdir -p /run/php-fpm
RUN usermod -a -G ftp apache
WORKDIR /var/www/html
EXPOSE 9000
# Run in foreground as root (in container POV)
CMD ["php-fpm", "-R", "-F"]
docker-compose.yaml
version: '3.9'
services:
postgres:
image: "postgres:9.5.24"
environment:
POSTGRES_DB: /run/secret/postgres_db
POSTGRES_USER: /run/secret/postgres_user
POSTGRES_PASSWORD: /run/secret/postgres_password
volumes:
- database:/var/lib/postgresql/data
secrets:
- postgres_db
- postgres_user
- postgres_password
deploy:
resources:
limits:
cpus: '0.15'
memory: 128m
networks:
- internal
apache:
env_file: .env
image: apache:latest
build:
context: ./docker/images/apache2.4
dockerfile: prod.Dockerfile
ports:
- 8000:80
environment:
FPM_HOST: php:9000
volumes:
- ./docker/logs/apache/:/var/log/httpd/
networks:
- traefik-public
- internal
deploy:
labels:
- "traefik.enable=true"
- "traefik.http.routers.my_stack.rule=Host(`my-host.com`)"
- "traefik.http.routers.my_stack.entrypoints=websecure"
- "traefik.http.routers.my_stack.tls.certresolver=letsencryptresolver"
- "traefik.http.services.my_stack.loadbalancer.server.port=80"
- "traefik.port=80"
resources:
limits:
cpus: '0.15'
memory: 128m
php:
env_file: .env
image: php:latest
# links:
# - ftp
# - apache
build:
context: ./docker/images/php
dockerfile: prod.Dockerfile
# args:
# TIMEZONE: 'Europe/Paris'
volumes:
- ftp_data:/var/www/ftp:rw
networks:
- internal
deploy:
resources:
limits:
cpus: '0.20'
memory: 512m
ftp:
env_file: .env
image: "fauria/vsftpd:latest"
ports:
- "20:20"
- "21:21"
- "22100-22110:22100-22110"
environment:
FTP_USER: apache
FTP_PASS: /run/secret/automation_client_password
PASV_ADDRESS: 127.0.0.1
PASV_MIN_PORT: 22100
PASV_MAX_PORT: 22110
volumes:
- ftp_data:/home/vsftpd/apache:rw
networks:
- traefik-public
- internal
deploy:
resources:
limits:
cpus: '0.15'
memory: 128m
volumes:
ftp_data:
database:
secrets:
postgres_db:
external: true
postgres_user:
external: true
postgres_password:
external: true
automation_client_password:
external: true
networks:
traefik-public:
external: true
internal:
external: false
Anyone got a clue about this? Any helps/tips will be appreciated.
I don't like this kind of answer, but replica's all went live after complete reboot.

Docker - Nginx, PHP, MySQL - Laravel artisan migrate connection refused

I have Laravel running in Docker with 3 containers - Nginx, MySQL 8 And PHP 8.
I have the following docker-compose.yaml
version: '3.8'
services:
server:
build:
context: .
dockerfile: dockerfiles/nginx.dockerfile
ports:
- '8000:80'
volumes:
- ./src:/var/www/html
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
container_name: server
depends_on:
- app
- db
app:
build:
context: .
dockerfile: dockerfiles/php.dockerfile
volumes:
- ./src:/var/www/html:delegated
container_name: app
db:
image: mysql:8.0
env_file:
- ./env/mysql.env
container_name: db
This command:
docker-compose up -d server
Launches 3 containers:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ff5cbefd12e toolkit_server "/docker-entrypoint.…" 9 seconds ago Up 3 seconds 0.0.0.0:8000->80/tcp server
c7f3c9753909 toolkit_app "docker-php-entrypoi…" 14 seconds ago Up 8 seconds 9000/tcp app
d7b421bd3c3b mysql:8.0 "docker-entrypoint.s…" 14 seconds ago Up 9 seconds 3306/tcp, 33060/tcp db
I want to run php artisan migrate, so I try it like so:
docker-compose exec app php artisan migrate
This gives me the following error:
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = toolkit and table_name = migrations and table_type = 'BASE TABLE')
My mysql.env file is like so:
MYSQL_DATABASE=toolkit
MYSQL_USER=root
MYSQL_PASSWORD=password
MYSQL_ROOT_PASSWORD=password
AND .env:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=toolkit
DB_USERNAME=root
DB_PASSWORD=password
I've tried changing ports, host, but I just can't get access - I must have messed something up somewhere.
please try this in the app Section of the docker-compose.yml
links:
- db
In my haste, I was trying to run commands before mysql was ready. It can take 90 seconds after a rebuild for the container to accept connections.
docker logs db
Once it was ready to connect, my commands would run.

Laravel cannot connect to dockerise database

I created three containers (PhP-NGINX-MySql) to support default laravel project located on host machine.
When I try to connect to DB from laravel I get error:
Route::get('/', function () {
dd(App\User::all());
return view('welcome');
});
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed:
Name or service not known (SQL: select * from users)
Here is my .env in laravel-5.3.16
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33061
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
and ansible-playbook:
---
- hosts: localhost
environment:
PYTHONPATH: /usr/local/lib/python2.7/site-packages/
tasks:
- name: get currernt location
command: pwd
register: my_way
- set_fact: host_dir="{{my_way.stdout}}"
- name: create image with nginx
docker_image:
path: /home/demaunt/Jun/dock_click/engie
dockerfile: engie.dockerfile
name: engie_image
- name: create image with php
docker_image:
path: /home/demaunt/Jun/dock_click/piha
dockerfile: piha.dockerfile
name: piha_image
- name: run piha container
docker_container:
name: piha_cont
image: piha_image
volumes:
- "/home/demaunt/Dockjun/laravel-5.3.16:/var/www/wapclick"
links:
- diba_cont:db
env:
DB_PORT: 3306
DB_HOST: database
- name: run engie container
docker_container:
name: engie_cont
image: engie_image
volumes_from:
- piha_cont
ports:
- "8080:80"
links:
- piha_cont:app
- name: run diba container
docker_container:
name: diba_cont
image: mysql:5.6
env:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
ports:
- 33061:3306
What is more strange is that when I run php artisan migrate I get succesfull mesage:
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
Here are containers running:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e926382db347 engie_image "nginx -g 'daemon ..." 19 seconds ago Up 18 seconds 443/tcp, 0.0.0.0:8080->80/tcp engie_cont
c9563c839e45 piha_image "docker-php-entryp..." 19 seconds ago Up 18 seconds 9000/tcp piha_cont
5de541049da2 mysql:5.6 "docker-entrypoint..." 20 seconds ago Up 19 seconds 0.0.0.0:33061->3306/tcp diba_cont
Try "diba_cont" as DB_HOST that should do the job. Docker declare automatically a DNS rule with the name of the container to contact the container

MySQL Connection in Docker container

this is driving me crazy ...
I am building a simple LEMP stack with Docker on my Mac.
This is my docker-compose file:
nginx:
image: tutum/nginx
ports:
- "3333:80"
links:
- phpfpm
- db
volumes:
- ./nginx/default.conf:/etc/nginx/sites-available/default
- ./nginx/default.conf:/etc/nginx/sites-enabled/default
- ./logs/nginx-error.log:/var/log/nginx/error.log
- ./logs/nginx-access.log:/var/log/nginx/access.log
phpfpm:
build: ./phpfpm
ports:
- "9000:9000"
volumes:
- ./code:/usr/share/nginx/html
db:
image: mysql
tty: true
restart: always
volumes:
- /Users/mauo/local/mysql/data:/var/lib/mysql
ports:
- "3308:3306"
environment:
MYSQL_DATABASE: 'docker'
MYSQL_ROOT_PASSWORD: 'root'
MYSQL_USER: 'root'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
Everything works fine, with exception on mysql container.
This refuses me connection.
This is the message on my php file inside code folder:
(HY000/2002): Connection refused in /usr/share/nginx/html/index.php on line 2
When I enter in mysql container with exec -it bash and I type 'mysql', the same thing, access denied for root#localhost.
From within the container, I tried to launch 'mysqld' and it tells me
2016-07-16T09:56:02.777893Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2016-07-16T09:56:02.778010Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2016-07-16T09:56:03.779967Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2016-07-16T09:56:03.780182Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
Any help would be appreciated!!!!
Thank you
M.

Categories