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.
Related
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.
My docker-compose file:
version: '3.8'
services:
cache:
image: memcached:latest
restart: always
ports:
- "10001:11211"
server:
build: '.'
tty: true
working_dir: /var/www
environment:
DISABLE_DEFAULT_SERVER: 1
AUTORELOAD_PROGRAMS: "kid_api"
AUTORELOAD_ANY_FILES: 1
volumes:
- ../../Apps/Server:/var/www
- ./php/config/custom.ini:/usr/local/etc/php/conf.d/custom.ini
- ./supervisor/kid_api.conf:/etc/supervisor/service.d/kid_api.conf
ports:
- "9988:9988"
depends_on:
- cache
links:
- cache
My Api.php file into /var/www open socket port for daemon server with Swoole.
When i tried docker-compose up, then ports is ok:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c80bee2baf7 php74_swoole4510_base_server "/entrypoint.sh" 2 hours ago Up 1 second 0.0.0.0:9988->9988/tcp php74_swoole4510_base_server_1
3b0f67cc3295 memcached:latest "docker-entrypoint.s…" 24 hours ago Up 2 hours 0.0.0.0:10001->11211/tcp php74_swoole4510_base_cache_1
but if i tried to run with PhpStorm
then the ports are not open
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a7b534100202 php74_swoole4510_base_server "php /var/www/Api.php" 2 seconds ago Up Less than a second php74_swoole4510_base_server_run_df9a640e18bb
3b0f67cc3295 memcached:latest "docker-entrypoint.s…" 24 hours ago Up 2 minutes 0.0.0.0:10001->11211/tcp php74_swoole4510_base_cache_1
How fix it?
docker-compose up is not equivalent to docker-compose run.
PhpStorm does docker-compose run service_name command.
Docker Compose itself doesn't map ports in this case, you can check it in the terminal.
You can spin up the services with docker-compose up in the terminal manually, and then use PhpStorm in the exec mode (the option is visible on your screenshot).
I am getting the following error
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = testdb and table_name = migrations)
when i run -
php artisan migrate
I am running this command on laradock workspace. I entered the workspace using the following command.
docker-compose exec workspace bash
I am using laravel 5.5. I have laradock inside my project folder in following way.
+testproject
-Laradock
My project .env (testproject/.env) file contains the following settings for mysql.
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=33060
DB_DATABASE=testdb
DB_USERNAME=root
DB_PASSWORD=root
My laradock .env(testproject/laradock/.env) file contains the following settings for mysql.
MYSQL_VERSION=8.0
MYSQL_DATABASE=testdb
MYSQL_USER=default
MYSQL_PASSWORD=secret
MYSQL_PORT=33060
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d
My docker-compose.yml contains the following settings for Mysql Container
mysql:
build:
context: ./mysql
args:
- MYSQL_VERSION=${MYSQL_VERSION}
environment:
- MYSQL_DATABASE={MYSQL_DATABASE}
- MYSQL_USER={MYSQL_USER}
- MYSQL_PASSWORD={MYSQL_PASSWORD}
- MYSQL_ROOT_PASSWORD={MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
volumes:
- ${DATA_SAVE_PATH}/mysql:/var/lib/mysql
- ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
ports:
- "${MYSQL_PORT}:3306"
networks:
- backend
Mysql in docker is installed in port 33060. I am trying laradock for the first time. After trying several configurations now i am calling out for Avengers !!!
solved the problem by having the following mysql settings.
testproject/.env:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=testdb
DB_USERNAME=root
DB_PASSWORD=root
testproject/laradock/.env:
MYSQL_VERSION=8.0
MYSQL_DATABASE=default
MYSQL_USER=default
MYSQL_PASSWORD=secret
MYSQL_PORT=33060
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d
testproject/laradock/docker-compose.yml
mysql:
build:
context: ./mysql
args:
- MYSQL_VERSION=${MYSQL_VERSION}
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
volumes:
- ${DATA_SAVE_PATH}/mysql:/var/lib/mysql
- ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
ports:
- "${MYSQL_PORT}:3306"
networks:
- backend
I also ran the following two commands in laradock folder
docker-compose down
docker-compose up -d mysql
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
I want to make a web development platform on MacOS by using Docker. I installed nginx, and php7-fpm container and they're running and communicating each other. But after installing mysql container, mysql container was exited. I don't know why it exited.
This is docker ps -a output:
2955d2d5c392 nginx "/sbin/my_init" 38 seconds ago Up 36 seconds 0.0.0.0:8080->80/tcp dockertutorial_web_1
ec3c16795f05 php:7-fpm "docker-php-entrypoin" 38 seconds ago Up 37 seconds 9000/tcp dockertutorial_php_1
835e91ba927a mysql:latest "docker-entrypoint.sh" 38 seconds ago Exited (0) 37 seconds ago dockertutorial_mysql_1
As you can see, mysql was exited.
This is my docker-compose.yml file:
web:
image: nginx
ports:
- "8080:80"
volumes:
- ./src/public:/var/www/public
- ./src/vhost.conf:/etc/nginx/sites-enabled/vhost.conf
links:
- php
php:
image: php:7-fpm
volumes:
- ./src/public:/var/www/public
links:
- mysql
mysql:
image: mysql:latest
ports:
- "3306:3306"
volumes:
- /var/lib/mysql
command: "true"
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: project
MYSQL_USER: project
MYSQL_PASSWORD: project
Any suggesstion to solve it?
You are overriding command for MySQL image with true which will be run instead of mysqld.
Remove command: "true" from docker-compose.yml from mysql service and it will start mysqld.
See this Dockerfile for reference.
https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/Dockerfile#L63