Docker: Can't connect to mysql under container with docker-compose parameters - docker

I'm trying to run mysql under container with mysql parameters i defined on docker-compose.yml file. But i have an access denied when i run :
mysql -utest -ptest
I'm only able to connect with mysql -uroot -proot.
Help me please.
Thanks.
mysql:
container_name: mysql
image: mysql
restart: always
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_DATABASE: app
MYSQL_ROOT_PASSWORD: test
MYSQL_USER: test
MYSQL_PASSWORD: test

Try to launch with specified database name like this:
mysql -u test -p test app
Explanation:
MYSQL_USER, MYSQL_PASSWORD
These variables are optional, used in conjunction to create a new user and to set that user's password. This user will be granted superuser permissions (see above) for the database specified by the MYSQL_DATABASE variable. Both variables are required for a user to be created.
From MySQL docker hub page
Permissions are granted only for the database specified by environment variable. When you try to log into default database you have no permissions to it only for app database.

My complete docker-compose file.
version: '3.2'
services:
apache:
container_name: apache
build: .docker/apache/
restart: always
volumes:
- .:/var/www/html/app/
ports:
- 80:80
depends_on:
- php
- mysql
links:
- mysql:mysql
php:
container_name: php
build: .docker/php/
restart: always
volumes:
- .:/var/www/html/app/
working_dir: /var/www/html/app/
mysql:
container_name: mysql
image: mysql
restart: always
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_DATABASE: app
MYSQL_ROOT_PASSWORD: test
MYSQL_USER: test
MYSQL_PASSWORD: test

Maybe you could try attaching an interactive bash process to the already running container by following these steps:
Get your container id or name from running docker container ls in your terminal (I'm talking about the mysql container, which should have the mysql name according to your docker-compose.yml file)
Run docker exec -it mysql bash to associate an interactive bash process to the running container
Now, being inside of your container's filesystem, run mysql --user=test --password=test and you should be able to get on with your work

Related

How do Environment variables with different ports work in docker-compose mariadb?

They have sent me to do an exercise in docker and I have no idea how to do it, I find myself super lost.
What is requested is the following:
"Create a docker compose file that launches two Maria db databases, in 3 different environments "
"Depending on the environment, they should run on 3 different ports:
Development: 3306
Production: 3307
Testing: 3308
The environment should be sent as a parameter added to the command
docker compose "
The databases should be interconnected with each other
and greater than this: "With the command:
docker-compose docker-file --dev docker-compose docker-file --pre docker-compose docker-file --pro
passing that parameter, in the first one I would run a production environment, in the other preproduction and in the other development
In each environment there will be variables that change, such as the database port. "
Up to here is all the information that they have given me and everything that has been asked of me.
Could someone help me solve this ???
The only thing I managed to do was create the 2 databases, but I am missing the environment section, which is what I don't understand.
Code I have in a docker-compose.yml file:
version: '3'
services:
mariadb:
image: mariadb
restart: always
environment:
MYSQL_DATABASE: 'test'
MYSQL_USER: 'root'
MYSQL_PASSWORD: 'root'
MYSQL_ROOT_PASSWORD: 'root'
ports:
- 3306:3306
expose:
- "3306"
volumes:
- ./mariadb:/var/lib/mysql
mariadb2:
image: mariadb
environment:
MYSQL_DATABASE: 'test2'
MYSQL_USER: 'root'
MYSQL_PASSWORD: 'root'
MYSQL_ROOT_PASSWORD: 'root'
ports:
- 3305:3305
volumes:
- ./mariadb2:/var/lib/mysql
When you launch a docker-compose you can add the enviroment parameters by a .env file. I think that is what they ment when they asked you to do the exercise.
The .env file is loaded by default when you launch the docker-compose but you can specify it.
You may like to read the documentation first so you can try things out https://docs.docker.com/compose/environment-variables/

Changing SQL Mode on Mariadb Image with docker-compose

I have an issue with the official dockerized image of Mariadb.
When my applications tries to make some queries I got the following error :
DB Error: unknown error QUERY : INSERT INTO
It seems this error comes from the SQL_MODE, which is set as follow in this image :
STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,
NO_ENGINE_SUBSTITUTION
I have a normal Linux Server and with mariadb installed and i don't have this STRICT_TRANS_TABLES value in my SQL_mode. And my application is working without any problem.
How can I remove the STRICT_TRANS_TABLES value in my container when I run docker-compose with my docker-compose file without the need of a custom dockerfile?
In your docker-compose.yml set command: --sql_mode="".
Here is an example:
db-service:
build:
context: .
dockerfile: db.dockerfile
image: example/repo:db
ports:
- "3306:3306"
volumes:
- ./data/db-data:/var/lib/mysql
- ./data/db-init:/docker-entrypoint-initdb.d/
ports:
- "3306:3306"
environment:
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: your_database
command: mysqld --sql_mode="" --character-set-server=utf8 --collation-server=utf8_slovenian_ci --init-connect='SET NAMES UTF8;' --innodb-flush-log-at-trx-commit=0
restart: on-failure
networks:
- yournet
It works fine for me.

docker-compose drupal not accepting my database name and throwing error below

I have the following docker-compose.yml file:
version: '3'
services:
maria_service:
build: ./db_maria
restart: always
environment:
MYSQL_DATABASE: mariadb
MYSQL_USER: joel
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
volumes:
- ./db:/var/lib/mysql
drupal_service:
build: ./website
restart: always
ports:
- 8080:80
volumes:
- /var/www/html/modules
- /var/www/html/profiles
- /var/www/html/themes
# this takes advantage of the feature in Docker that a new anonymous
# volume (which is what we're creating here) will be initialized with the
# existing content of the image at the same location
- /var/www/html/sites
depends_on:
- maria_service
Here's my working directory:
Here's the drupal dockerfile where all I'm doing is to pull the drupal image:
Here's the mariadb dockerfile:
It automatically generate this "db" subfolder seen in the pic below:
My issue is everytime I enter mariadb on the drupal UI at localhost:8080, it throws this error below:
UPDATES:
Based on #Tarun Lalwani answer, my issue was that, in the Drupal UI, I would enter my username, password and db name but if you expand that Advanced Options in that Drupal screenshot, you'll see that the HOSTNAME was pointing to "localhost" when it should be pointing to the actual hostname of the mariadb database server which in DOCKER WORLD, the hostname name of a running container is ITS SERVICE NAME i.e "mariadb_service" as seen in the docker-compose.yml file - see screenshot. Hope I wasn't the only newbie that bumped into that and will help others, thanks Tarun Lalwani!!
You need to set the Host name also for the DB in Drupal. This db host will be maria_service as per the service name from your docker-compose.yml file. This needs to be done by expanding the Advanced options
Using Environment Variables
You could also try setting the environment variables for these settings
version: '3'
services:
maria_service:
build: ./db_maria
restart: always
environment:
MYSQL_DATABASE: mariadb
MYSQL_USER: joel
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
volumes:
- ./db:/var/lib/mysql
drupal_service:
build: ./website
restart: always
ports:
- 8080:80
volumes:
- /var/www/html/modules
- /var/www/html/profiles
- /var/www/html/themes
# this takes advantage of the feature in Docker that a new anonymous
# volume (which is what we're creating here) will be initialized with the
# existing content of the image at the same location
- /var/www/html/sites
depends_on:
- maria_service
environment:
DB_HOST: maria_service
DB_USER: joel
DB_PASSWORD: password
DB_NAME: mariadb
DB_DRIVER: mysql

Drupal website with Docker-Compose

I'm trying to setup a Drupal site template however I have an issue, this is my current docker-compose:
version: '2'
services:
database:
image: mysql
container_name: database
command: mysqld --user=root --verbose
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: "db"
MYSQL_USER: "user"
MYSQL_PASSWORD: "pass"
MYSQL_ROOT_PASSWORD: "root"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
restart: always
site:
image: drupal
container_name: site
ports:
- "555:80"
volumes:
- ./drupal:/var/www/html
links:
- database:database
working_dir: /app
restart: always
volumes:
db:
Now if I do that the site doesn't work, no files are in the /var/www/html directory and the site 404's on everything. However if I remove the volume in the site container, it works perfectly and I can start setting it up as if it was a regular site.
What am I missing?
When you don't map a volume in the site service, that means Drupal is using whatever's already in /var/www/html from the drupal image. When you map the volume, you're overwriting /var/www/html with whatever's in ./drupal on the host machine. The results you're seeing imply there may be something wrong with the contents of ./drupal. To start with, I would run the service without mapping a volume and then copy the exact contents of /var/www/html into your local folder:
docker cp compose_site_1:/var/www/html/ ./drupal
Then try running the service again, this time with the volume mapped and see if that works. If it works, that tells you the problem was with the contents of ./drupal.

Wrong credentials when connecting to Docker MySQL container

New to docker and I'm using laradock to set up my environment with a Craft CMS project. I'm able to get it installed and set up, however I'm have an issue when trying to connect to the MySQL container that laradock creates from the docker-compose.yml file.
Here's the db related portion in my docker-compose.yml
mysql:
build:
context: ./mysql
args:
- MYSQL_DATABASE=homestead
- MYSQL_USER=homestead
- MYSQL_PASSWORD=secret
- MYSQL_ROOT_PASSWORD=root
volumes:
- mysql:/var/lib/mysql
ports:
- "3306:3306"
Within my craft/config/db.php file, I have the following settings:
return array(
'.dev' => array(
'tablePrefix' => 'craft',
'server' => 'mysql',
'database' => 'homestead',
'user' => 'homestead',
'password' => 'secret',
),
);
However, I'm getting a Craft can’t connect to the database with the credentials in craft/config/db.php error.
My question is - when docker creates the MySQL container, I'm assuming it uses the credentials in the docker-compose.yml file to create and allow access to that database. If so, as long as my container is running and my credentials from my db.php file match with the credentials in the docker-compose.yml file, shouldn't it connect?
If I wanted to update the credentials in the MySQL container, can I simply update the values in both files and run docker-compose up -d mysql?
Thanks!
I have run in kind of similar issue recently because the containers were started in a "random" order. Maybe this is your issue too don't know for sure.
In a brief this is my case:
- two containers php-fpm and mysql.
- Running docker-compose up -d --build --no-cache build everything but php-fpm finished first so by then mysql was doing some stuff for get service ready
- php-fpm application couldn't connect to MySQL server because wasn't ready
The solution use the new Docker Compose version and use 2.1 on the docker-compose.yml. Here is my working example:
version: '2.1'
services:
php-fpm:
build:
context: docker/php-fpm
depends_on:
db:
condition: service_healthy
db:
image: mariadb
healthcheck:
test: "exit 0"
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
The trick: depends_on (check here) and condition (check the example).

Resources