How to communicate mysql & mysqladmin docker containers [duplicate] - docker

This question already has answers here:
[Docker]: Connecting PHPMyAdmin to MySQL doesnt work
(6 answers)
Closed 3 years ago.
I want to spawn MySQL & PHPMyAdmin docker containers. Mysql container can be accessed via 3306 port & PHPMyAdmin can be accessed through 8280 port.
My question is, how a PHP application can be configured to access the MySQL docker container on 3306 port
and the PHPMyAdmin can be configured for MySQL.
Thanks.

Start MySQL server
docker run --name mysql -e MYSQL_ROOT_PASSWORD=password -d mysql
Start phpAdmin with link to mysql container
docker run --name myadmin -d --link mysql:db -p 8080:80 phpmyadmin/phpmyadmin
PHPAdmin application will be serving on localhost:80. MySQL credentials will be root/password.
We can now use docker-compose for this solution which is more portable.

You can use the offical image for MySQL and PHPMyAdmin.
version: '3.1'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: example
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8081:80
To access it from php container, just add your php container in the docker-compose, so the connection string should be like
host:db
user: root
pass: example

You can use Adminer a database management tool.
You need the below configuration in docker-compose.yml for Mysql and adminer.
mysqldb:
container_name: mysqldb
image: mysql
restart: always
environment:
- MYSQL_USER= 'xyz'
- MYSQL_PASSWORD='pwd0123456789'
- MYSQL_DB= 'testdb'
networks:
- main
adminer:
container_name: adminer
image: adminer
restart: always
ports:
- 4041:8080
networks:
- main-network
depends_on:
- mysqldb

Related

Why can't I access to my local docker-compose from Browser?

I've run my docker-compose file trying to dockerize pgadmin for Postgres but my browser cannot connect to pgadmin on url localhost:8080.
This is the docker-compose file that I am running
version: '3'
services:
db:
container_name: postgres_container
image: postgres
restart: always
environment:
POSTGRES_DB: postgres_db
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
PGDATA: /var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
- db-data:/var/lib/postgresql/data
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4:5.5
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: secret
PGADMIN_LISTEN_PORT: 80
ports:
- "8080:80"
volumes:
- pgadmin-data:/var/lib/pgadmin
volumes:
db-data:
pgadmin-data:
This is my docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c6a6a588f639 dpage/pgadmin4:5.5 "/entrypoint.sh" 3 hours ago Up 9 minutes 443/tcp, 0.0.0.0:8080->80/tcp pgadmin4_container
ad6fe3349717 postgres "docker-entrypoint.s" 3 hours ago Up 9 minutes 0.0.0.0:5432->5432/tcp postgres_container
When I try to connect to it from browser to localhost:8080 it says Connection attempt failed
I am using almost the same docker compose file on Windows 10 with WSL 2 and can connect immediately using Firefox build 100 browser to localhost:8080.
My difference is that the image for pgadmin4 in the compose file is image: dpage/pgadmin4
the latest not v5.5
I solved this problem. Turns out that DOCKER_HOST variable was set to 192.168.99.100:2376. You can see it by running command echo $DOCKER_HOST
I just ran my docker container into this port instead of localhost and everything worked fine.
docker run -d -p 192.168.99.100:9411:9411 openzipkin/zipkin
And I was able to access my docker container through the browser by url http://192.168.99.100:9411/ Thank you very much everyone

Can't connect to Docker Container on Windows

I have a docker-compose file that looks like this
version: "3"
services:
db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: michael
POSTGRES_PASSWORD: pass123
admin:
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
PGADMIN_CONFIG_SERVER_MODE: 'False'
ports:
- "5050:5050"
I run docker-compose up -d and I can see my apps running from Docker Desktop. I cannot however connect to my pgadmin instance at port 5050 using localhost. Any ideas?
Docker container of pgAdmin by default runs on port 80 as per the documentation here https://www.pgadmin.org/docs/pgadmin4/latest/container_deployment.html
You are exposing port 5050 through the mapping. Either add a environment variable PGADMIN_LISTEN_PORT to the docker_compose to make pgAdmin run on port 5050
OR
change port mapping to 5050:80 for the pgAdmin service
Check the docker inspect or docker ps results to ensure that you have your port exposed correctly
Try to connect to it using the public IP

Docker compose doesn't expose port 80 for a Linked Service setup

Here's the docker compose file
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- blog-vol:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: mysqlpass
MYSQL_DATABASE: wordpress
MYSQL_USER: wpuser
MYSQL_PASSWORD: mysqluserpass
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wpuser
WORDPRESS_DB_PASSWORD: mysqluserpass
volumes:
blog-vol:
I bring it up doing the usual
docker-compose up -d
For some reason http://localhost (port 80) or http://my_private_ip (port 80) doesn't resolve when it's up.
However when I change the "ports" config in the above to
ports:
-"8000:80"
http://localhost:8000 and http://my_private_ip:8000 do work. Not sure what's going on with port 80. I do see that it's available. I tested port 80 by running an nginx instance by doing
docker run -p 80:80 -d nginx
This nicely exposes the service at port 80. I'm not sure what's going on with the above docker-compose config. Am I missing something here?
The issue is with the default Wordpress configuration in the official Docker image. I exposed the port for the above MySQL instance and connected to the Database Server. I inspected the WP configuration to realize that it has a fixed config for where the server is running including the port. I updated this and everything started to work as expected!

Mapping ports in docker-compose file doesn't work. Network unreachable

I'm trying to map a port from my container, to a port on the host following the docs but it doesn't appear to be working.
After I run docker-compose -f development.yml up --force-recreate I get no errors. But if I try to reach the frontend service using localhost:8081 the network is unreachable.
I used docker inspect to view the IP and tried to ping that and still nothing.
Here is the docker-compose file I am using. And I doing anything wrong?
development.yml
version: '3'
services:
frontend:
image: nginx:latest
ports:
- "8081:80"
volumes:
- ./frontend/public:/var/www/html
api:
image: richarvey/nginx-php-fpm:latest
ports:
- "8080:80"
restart: always
volumes:
- ./api:/var/www/html
environment:
APPLICATION_ENV: development
ERRORS: 1
REMOVE_FILES: 0
links:
- db
- mq
db:
image: mariadb
restart: always
volumes:
- ./data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: dEvE10pMeNtMoDeBr0
mq:
image: rabbitmq:latest
restart: always
environment:
RABBITMQ_DEFAULT_USER: developer
RABBITMQ_DEFAULT_PASS: dEvE10pMeNtMoDeBr0
You are using docker toolbox. Docker toolbox uses docker machine. In Windows with docker toolbox, you are running under a virtualbox with its own IP, so localhost is not where your containers live. You will need to go 192.168.99.100:8081 to find your frontend.
As per the documentation on docker machine(https://docs.docker.com/machine/get-started/#run-containers-and-experiment-with-machine-commands):
$ docker-machine ip default
192.168.99.100

connect to mysql database from docker container

I have this docker file and it is working as expected. I have php application that connects to mysql on localhost.
# cat Dockerfile
FROM tutum/lamp:latest
RUN rm -fr /app
ADD crm_220 /app/
ADD crmbox.sql /
ADD mysql-setup.sh /mysql-setup.sh
EXPOSE 80 3306
CMD ["/run.sh"]
When I tried to run the database as separate container, my php application is still pointing to localhost. When I connect to the "web" container, I am not able to connect to "mysql1" container.
# cat docker-compose.yml
web:
build: .
restart: always
volumes:
- .:/app/
ports:
- "8000:8000"
- "80:80"
links:
- mysql1:mysql
mysql1:
image: mysql:latest
volumes:
- "/var/lib/mysql:/var/lib/mysql"
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: secretpass
How does my php application connect to mysql from another container?
This is similar to the question asked here...
Connect to mysql in a docker container from the host
I do not want to connect to mysql from host machine, I need to connect from another container.
At first you shouldn't expose mysql 3306 port if you not want to call it from host machine. At second links are deprecated now. You can use network instead. I not sure about compose v.1 but in v.2 all containers in common docker-compose file are in one network (more about networks) and can be resolved by name each other. Example of docker-compose v.2 file:
version: '2'
services:
web:
build: .
restart: always
volumes:
- .:/app/
ports:
- "8000:8000"
- "80:80"
mysql1:
image: mysql:latest
volumes:
- "/var/lib/mysql:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: secretpass
With such configuration you can resolve mysql container by name mysql1 inside web container.
For me, the name resolutions is never happening. Here is my docker file, and I was hoping to connect from app host to mysql, where the name is mysql and passed as an env variable to the other container - DB_HOST=mysql
version: "2"
services:
app:
build:
context: ./
dockerfile: /src/main/docker/Dockerfile
image: crossblogs
environment:
- DB_HOST=mysql
- DB_PORT=3306
ports:
- 8080:8080
depends_on:
- mysql
mysql:
image: mysql:5.7.20
environment:
- MYSQL_USER=root
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_DATABASE=crossblogs
ports:
- 3306:3306
command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8 --explicit_defaults_for_timestamp

Resources