Docker connect phpmyadmin to my MySQL Server - docker

Hello im trying to connect mysql to my phpmyadmin container how can i bind the mysql to this. is it also possible to change the port 8080 to /phpmyadmin im access to http://192.168.99.100:8080 so i get http://192.168.99.100/phpmyadmin instead of the container id
version: '2'
services:
#######################################
# PHP application Docker container
#######################################
app:
build:
context: .
dockerfile: Dockerfile
links:
- mysql
ports:
- "8000:80"
volumes:
- ./app/:/app/
- ./:/docker/
volumes_from:
- storage
networks:
- php-network
#######################################
# MySQL server
#######################################
mysql:
build:
context: docker/mysql/
dockerfile: MySQL-5.7.Dockerfile
restart: always
volumes_from:
- storage
env_file:
- etc/environment.yml
networks:
- php-network
#######################################
# PHP MY ADMIN
#######################################
myphpadmin:
build:
context: docker/myphpadmin
dockerfile: Dockerfile
restart: always
links:
- mysql
ports:
- 8080:80
environment:
- PMA_ARBITRARY=1
networks:
- php-network
storage:
build:
context: docker/storage/
volumes:
- /storage
networks:
php-network:
driver: bridge

You will have to specify 2 more variables inside your docker-compose file under the phpmyadmin service:
environment:
PMA_HOST: mysql
PMA_PORT: 3306
Because even though you have created the link between your phpmyadmin and mysql containers, the phpmyadmin container will look for a database on 'localhost'

Related

dockered mariadb "[1045] Access denied for user" inside docker network

I know that this question has a lot answers on stackoverflow but I didn't found a solution for my case!
I moving the Laravel app to the containers.
I CAN CONNECT TO MARIADB INSTANCE OUTSIDE THE DOCKER NETWORK BUT NOT INSIDE!
(I can connect via MySQL Workbench, locally (via docker exec), I can restore the dump locally from container console and access to DB data outside)
What's wrong?
Why the app is not working (PHP has no access to the mariadb via internal app_network) but in the same time I can get access to DB outside and inside container itself???
OS: CentOS 7.9.2009
Docker: 20.10.12 (e91ed57)
Docker-compose: 1.29.2 (5becea4c)
The same configs works fine on Windows 10.
DOCKER COMPOSE CONFIG:
version: '3.9'
networks:
app_network:
driver: bridge
name: ${NETWORK_NAME}
volumes:
app:
name: ${APP_VOLUME_NAME}
mysql_database:
name: ${MYSQL_DATABASE_VOLUME_NAME}
mysql_dumps:
name: ${MYSQL_DATABASE_DUMPS_VOLUME_NAME}
services:
mariadb:
image: mariadb
env_file:
- ./.env
command: --default-authentication-plugin=mysql_native_password
ports:
- ${MYSQL_EXTERNAL_PORT}:3306
volumes:
- mysql_database:/var/lib/mysql
- mysql_dumps:/var/mysqldump
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
networks:
app_network:
aliases:
- mariadb
profiles:
- dev
- prod
php:
restart: always
env_file:
- ./.env
build:
context: ../../
dockerfile: ./.environment/cs/php/Dockerfile
args:
- USER_ID=${PHP_USER_ID}
- GROUP_ID=${PHP_GROUP_ID}
- DEFAULT_CONFIG_FILE=${PHP_DEFAULT_CONFIG_FILE}
- CUSTOM_CONFIG_FILE=${PHP_CUSTOM_CONFIG_FILE}
- PROJECT_FOLDER=${PHP_PROJECT_FOLDER}
volumes:
- ./php/logs:/var/log
- ../../:${PHP_PROJECT_FOLDER}
networks:
app_network:
aliases:
- php
depends_on:
- memcached
- mariadb
profiles:
- dev
- prod
nginx:
restart: always
env_file:
- ./.env
build:
context: ../../
dockerfile: ./.environment/cs/nginx/Dockerfile
args:
- CONFIG_FILE=${WEB_CONFIG_FILE}
- PROJECT_FOLDER=${WEB_PROJECT_FOLDER}
ports:
- ${WEB_EXTERNAL_PORT}:80
volumes:
- ./nginx/logs:/var/log/nginx
- ../../public:${WEB_PROJECT_FOLDER}:cached
networks:
app_network:
aliases:
- nginx
depends_on:
- php
profiles:
- dev
- prod
Docker .ENV
NETWORK_NAME=CS
APP_VOLUME_NAME=CS_APP_STORAGE
MYSQL_DATABASE_VOLUME_NAME=CS_DATABASE
MYSQL_DATABASE_DUMPS_VOLUME_NAME=CS_DATABASE_DUMPS
MYSQL_EXTERNAL_PORT=3317
MYSQL_ROOT_PASSWORD=root
MYSQL_USER=client
MYSQL_PASSWORD=client
PHP_USER_ID=1000
PHP_GROUP_ID=1000
PHP_DEFAULT_CONFIG_FILE=php.ini-production
PHP_CUSTOM_CONFIG_FILE=./.environment/cs/php/custom.prod.ini
PHP_PROJECT_FOLDER=/var/www/app
WEB_EXTERNAL_PORT=127.0.0.1:8091
WEB_CONFIG_FILE=./.environment/cs/nginx/nginx.dev.conf
WEB_PROJECT_FOLDER=/var/www/app/public
Laravel .ENV
DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306
DB_DATABASE=client
DB_USERNAME=client
DB_PASSWORD=client
try to add the expose config key in the maria db service
mariadb:
//...
expose:
- 3306
This is the issue of the MariaDB container. I connected to the MariaDB via MySQL Workbench, fully remove the user, create a new one and give scheme privileges.
After that all works fine.

Docker: container state exit 0

i have a problem with docker container.
That's my docker-compose file with 5 services
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8088:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- mysql
- php
networks:
- laravel
mysql:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "4306:3306"
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
redis:
image: redis:5.0.0-alpine
restart: always
container_name: redis
ports:
- "6379:6379"
networks:
- laravel
composer:
image: composer:latest
container_name: composer
volumes:
- ./src:/var/www/html
tty: true
working_dir: /var/www/html
networks:
- laravel
then i run
docker-compose up -d
and then
docker-compose ps
to see my container and i always get the composer contaier down with code 0. that's the screenshot
:
can someone explain me why i can't put this container up. Thanks a lot
composer isn't a program that stays alive. It's a program that does specific some work and then exits.
There's not much purpose in keeping it "up", since it's not going to do anything like the other processes do (nginx intercepts web traffic and writes response, mysql accepts database connections and reads/writes from a database, php serves web content, redis can be connected to as a cache).

How To startup Docker with phpmyadmin

Hello guys im setting up my first Docker env im strugleling with the setup for phpmyadmin i would like to use the native phpmyadmin from docker hub and link it to my apache see the code bellow does anybody have few suggestions? how can i handle this issue.
version: '2'
services:
# PHP Docker container
app:
build:
context: .
dockerfile: Dockerfile
links:
- mysql
ports:
- "8000:80"
volumes:
- ./app/:/app/
- ./:/docker/
volumes_from:
- storage
#######################################
# MySQL server
#######################################
mysql:
build:
context: docker/mysql/
dockerfile: MySQL-5.7.Dockerfile
restart: always
volumes_from:
- storage
env_file:
- etc/environment.yml
#######################################
# PHP MY ADMIN
#######################################
phpmyadmin:
image:
links:
ports:
- "8000:80"
environment:
MYSQL_USER: dev
MYSQL_ROOT_PASSWORD: root
storage:
build:
context: docker/storage/
volumes:
- /storage
MySQL Server Setup
MYSQL_ROOT_PASSWORD=dev
MYSQL_USER=dev
MYSQL_PASSWORD=dev
MYSQL_DATABASE=typo3
For Docker-compose file you can have something like
phpmyadmin:
image: phpmyadmin
restart: always
links:
- mysql
ports:
- 8000:80
environment:
- PMA_ARBITRARY=1
networks:
- Your-network
networks:
Your-network:
driver: bridge
You have to add your network to all your services ( mysql, php , .. )
then you can access your phpmyadmin by go to localhost:8000
Kindly find here complete version of docker compose file
version: '2'
services:
# PHP Docker container
app:
build:
context: .
dockerfile: Dockerfile
links:
- mysql
ports:
- "8000:80"
volumes:
- ./app/:/app/
- ./:/docker/
volumes_from:
- storage
networks:
- php-network
#######################################
# MySQL server
#######################################
mysql:
build:
context: docker/mysql/
dockerfile: MySQL-5.7.Dockerfile
restart: always
volumes_from:
- storage
env_file:
- etc/environment.yml
networks:
- php-network
#######################################
# PHP MY ADMIN
#######################################
phpmyadmin:
build:
context: .
dockerfile: PHPMYADMIN.Dockerfile
restart: always
links:
- mysql
ports:
- 8000:80
environment:
- PMA_ARBITRARY=1
networks:
- php-network
networks:
php-network:
driver: bridge
and PHPMYADMIN.Dockerfile will have only 1 line
FROM phpmyadmin/phpmyadmin
and you can access phpmyadmin on 192.168.99.100:8000

connect to mysql container from another container

I'm trying to connect to a mysql container from within another container on the same network specified by a compose file.
version: "2"
services:
web:
build:
context: .
dockerfile: nginx/Dockerfile
ports:
- "8080:80"
volumes:
- ./data:/srv
php:
build:
context: .
dockerfile: php-fpm/Dockerfile
volumes:
- ./data:/srv
mysql:
image: mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_USER=dummy_user
- MYSQL_PASSWORD=12345
I'm not really sure what the connection parameters would be if I'm trying to connect to the mysql container from the php container.
Specifically, what are the host and port for the mysql container from within another container of the same docker-compose network?
I've tried host: mysql port: 3306, but that doesn't seem to work.
You should link the containers. Add ports section to mysql container and links section to php container.
version: "2"
services:
web:
build:
context: .
dockerfile: nginx/Dockerfile
ports:
- "8080:80"
volumes:
- ./data:/srv
php:
build:
context: .
dockerfile: php-fpm/Dockerfile
links:
- mysql:mysql
volumes:
- ./data:/srv
mysql:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_USER=dummy_user
- MYSQL_PASSWORD=12345
With that configuration you'll be able to access mysql container from php with mysql:3306
A bit of documentation: https://docs.docker.com/compose/networking/#updating-containers
You don't share port for mysql
add to your docker-compose.yml to mysql service:
ports:
- "3306:3306"
UPD
try to set environments of your mysql container
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: dev
MYSQL_PASSWORD: 123456
MYSQL_DATABASE: myapp
and
$link = mysqli_connect("mysql", "dev", "123456", "myapp");
should works fine

Docker connect database from a container (laravel) to other container (wordpress)

I have two project using docker, in first project is laravel, and second is wordpress. In laravel I want to connect both database (to convert laravel database to wordpress database).
but I don't know how to connect it:
here is two docker-compose.yml file:
in laravel:
version: '2'
services:
# The Application
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
# The Web Server
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes_from:
- app
ports:
- 8081:80
# The Database
database:
image: mysql:5.6
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=homestead"
- "MYSQL_USER=homestead"
- "MYSQL_PASSWORD=secret"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "33061:3306"
volumes:
dbdata:
and my docker-compose.yml file in wordpress:
version: '2'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wpshop
MYSQL_USER: root
MYSQL_PASSWORD: 123456
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./:/var/www/html
ports:
- "8080:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: 123456
volumes:
db_data:
I cd to each project and run docker-compose up -d
Please help me!
Create an external network and use that network as default network for all your containers. This way you'll be able to reach all container by its name.
Take a look to Docker container networking: https://docs.docker.com/engine/userguide/networking/

Resources