Docker Compose - How reference many schemas in one mysql container - mysql

I'm trying to use two schemas into one mysql container. I have two flyway services that connect to two different schemas. The .yml file of Docker Compose looks like:
version: '2'
services:
mysqldb:
image: mysql:5.6.26
environment:
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE:
- my
- my_post
ports:
- "3306:3306"
flyway-service1-i:
image: mik/flyway-service
volumes:
- "../resources/db/migration:/migrations/ro"
depends_on:
- mysqldb
links:
- mysqldb
command: migrate -url=jdbc:mysql://mysqldb:3306/mi -user=user -password=password -baselineOnMigrate=true -locations='filesystem:/migrations'
flyway-service2-i:
image: mialk/flyway-post-service
volumes:
- "../../../service2/src/main/resources/db/migration:/migrations/ro"
depends_on:
- mysqldb
links:
- mysqldb
command: migrate -url=jdbc:mysql://mysqldb:3306/mi_post -user=user -password=password -baselineOnMigrate=true -locations='filesystem:/migrations'
But when I run the command sudo docker-compose up the terminal show this message:
ERROR: The Compose file './docker-compose.yml' is invalid because:
services.mysqldb.environment.MYSQL_DATABASE contains ["mialquiler", "mialquiler_post"], which is an invalid type, it should be a string, number, or a null
I traid without specifying MYSQL_DATABASE property, but it didn't work.
Is there any way to do that?

The MYSQL_DATABASE variable allows a single database to be created, and permissions granted on the database to the MYSQL_USER if specified.
You can use a single database to house multiple schema's.
If you need to create multiple databases you may need to run some custom SQL as flyway can't do database creation for you. The flyway test resources include a mysql example.

Related

Why is docker compose creating mysql container with wrong name?

I have managed to create a MySQL and PHP container and my scripts execute and all my tables are there.
However I have a database that I call "myDb" and a user that is called "someuser" and when the database is created for some reason the name of the database is "somedatabase"
my docker-compose.yaml file:
services:
mysql:
image: mysql:latest
ports:
- 3307:3306
environment:
MYSQL_DATABASE: myDb
MYSQL_ROOT_PASSWORD: SomeRootPassword1!
MYSQL_USER: someuser
MYSQL_PASSWORD: Password1!
volumes:
- ./dbScript/winit_Script2.sql:/docker-entrypoint-initdb.d/winit_Script2.sql
- db_data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: dev_pma
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3307
PMA_ARBITRARY: 1
restart: always
ports:
- 8183:80
volumes:
db_data:
phpAdmin:
Mysqlworkbench:
What have I done wrong here?
A little edit after the comments:
It would seem that when having a volumes section you create volumes in docker
and when you create a volume on a specific port once then it gets reused every time you do docker-compose up. This was the case for me.
More details in accepted answer.
The mysql image does not initialize the database if the volume is not clean.
When you are stopping and starting the database from the same compose file, the volume is always the same, hence you want the data to be persisted even after an app restart.
To force the re-initialization of the data, you can delete that docker volume(only if you no longer need that database! this cannot be undone):
First, stop and delete the containers.
Then list and delete the volume that persists the database:
docker volume ls
DRIVER VOLUME NAME
local <your-deployment-name>_db_data
docker volume rm <your-deployment-name>_db_data
Then run again the docker-compose up command and you'll be able to find the myDb in phpMyAdmin instead of somedb
Edit:
Unless you change yourself the entrypoint and rebuild the image to force it initialize your DB according to the ENV you're passing, even if the volume is not clean, the only option that comes to my mind is to create the new DB manually. Here is the conditional that skips the re-initialization of the DB and here is the script that is invoked if the volume is clean.

Can't migrate existing MySQL Wordpress database to Docker with docker-compose

I'm trying to migrate a pre-existing Wordpress install from an exported SQL file to a local Docker development environment. I'm somewhat new to Docker, but I have gone through some tutorials and documentation. The problem appears to be that the "wordpress" and "phpmyadmin" services cannot access the database.
I did a search & replace in Vim on the SQL file to replace instances of the original URL with "http://localhost:8000". Then I used docker-compose.
# docker-compose.yml
version: "3.7"
services:
db:
image: mysql:5.7.29
volumes:
- ./dbdata-import/:/docker-entrypoint-initdb.d/ # Where my exported SQL file is stored
# I also tried -./dbdata-import/thedata.sql:/docker-entrypoint-initdb.d/thedata.sql
- ./dbdata:/var/lib/mysql # So local database changes persist
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wp_database
MYSQL_USER: wp_username
MYSQL_PASSWORD: wp_password
wordpress:
depends_on:
- db
image: wordpress:php7.3-apache
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wp_username
WORDPRESS_DB_PASSWORD: wp_password
WORDPRESS_DB_NAME: wp_database
WORDPRESS_TABLE_PREFIX: wp_ #Tried without and without this
WORDPRESS_DEBUG: 1
volumes:
- ./wp-vol:/var/www/html
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin:4.9.4
container_name: phpmyadmin
environment:
PMA_HOST: db
PMA_USER: admin
PMA_PASSWORD: phpmyadmin_password
restart: always
ports:
- "8080:80"
It might be worth noting that I use this fix so I can still use OpenVPN. Basically, I created a subnet by running docker network create localdev --subnet 10.0.1.0/24. I also added this file next to my docker-compose.yml:
# docker-compose.override.yml
version: '3.7'
networks:
default:
external:
name: localdev
When I access http://localhost:8000, I don't get anything and the browser times out. When I access http://localhost:8080 for PHPMyAdmin, I get the error:
MySQL said: Documentation
Cannot connect: invalid settings.
mysqli_real_connect(): (HY000/1045): Access denied for user 'admin'#'10.0.1.3' (using password: YES)
phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server.
It seems odd this would be a credential issue. I pulled the Wordpress database information straight from wp-config.php on the host server. I also tested the database, username and password by signing into the MySQL CLI on the host server.
I used docker-compose down -v to delete volumes after I finished and docker volumes ls appears to be empty. So I don't think this is an issue with /docker-entrypoint-initdb.d/ not running because MySQL already initalized. However, I'm not sure.
I've been troubleshooting this for awhile now. I've done an almost identical Docker setup without /docker-entrypoint-initdb.d to create fresh Wordpress installs. That works fine. I could really use some help. I'm currently running Debian 10. Thanks.
UPDATE: I'm still having issues. I verified that I still have the same issues when I shutdown OpenVPN, remove docker-compose.override.yml and remove the localdev network. I get all the same problems. The only difference is that PHPMyAdmin gives me a different IP address after "admin#", which is expected.
I logged into my MySQL container using docker exec -it. Running MySQL CLI with my username and password worked. The tables looked like all the data was imported by docker-entrypoint-initdb.d. So the issue doesn't appear to be with docker-entrypoint-initdb.d, but rather the wordpress and phpmyadmin services can't access the database.
UPDATE 2: I fixed MyPHPAdmin. I didn't realize that PMA_USER and PMA_PASSWORD need to match the Wordpress database. I also needed PMA_HOST to include the port number:
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin:4.9.4
environment:
PMA_HOST: db:3306
PMA_USER: wp_username
PMA_PASSWORD: wp_password
restart: always
ports:
- "8080:80"
I still need help with Wordpress.

Why connection between phpmyadmin and mysql docker container doesn't work

I'm trying to connect a phpmyadmin-container to a mysql-container to view the databases. phpmyadmin web interface return error Cannot log in to the MySQL server with mysqli_real_connect(): (HY000/2002): No route to host.
What am I doing wrong? Thanks.
I use this operations:
docker-compose down && docker-compose up -d
Type to web browser http://localhost:8080
I use login as user root, password test
Get error described above
My docker-compose.yml
version: '2'
services:
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: test
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db
ports:
- "8080:80"
See official guide:
You need to specify some environment in order to link to an external mysql container:
Environment variables summary
PMA_ARBITRARY - when set to 1 connection to the arbitrary server will be allowed
PMA_HOST - define address/host name of the MySQL server
PMA_VERBOSE - define verbose name of the MySQL server
PMA_PORT - define port of the MySQL server
PMA_HOSTS - define comma separated list of address/host names of the MySQL servers
PMA_VERBOSES - define comma separated list of verbose names of the MySQL servers
PMA_PORTS - define comma separated list of ports of the MySQL servers
PMA_USER and PMA_PASSWORD - define username to use for config authentication method
PMA_ABSOLUTE_URI - define user-facing URI
For you, the mysql name is db, so you need to do next:
version: '2'
services:
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: test
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8080:80"
environment:
PMA_HOSTS: db
Also, --link depracated, just delete it, docker-compose will set network for you automatically to let the containers see each other with dns help.

Is there any way to create schema and tables in mysql docker without build a new image for mysql?

I have 2 images. One of them is custom and the other one is mysql. I am using docker-compose. Its database part is given below.
db:
image: mysql
container_name: mysql-docker-test
volumes:
- test-sql:/var/lib/mysql
restart: always
networks:
test-net:
ipv4_address: 172.17.0.5
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: my_secret_pw
There is no problem with communication but I need to run sql script when I run docker-compose up.
Is there any way to run sql script in docker-compose.yml?
you can override the entrypoint or the command in docker-compose.yml like this :
db:
image: mysql
entrypoint: ./path_to_your_script.sh
In your script you must start mysql server and then run your sql scripts. Of course you msut put the shell script in a volume.

How to create Mysql test database in dockerized Rails application?

I managed to dockerize my existing Rails application running on a Mysql database. But I wonder if it is possible to setup docker-compose to create the test database in the same container?
Here is my docker-compose.yml and it wirks fine with the mysql for developing
version: '2'
volumes:
db-data:
services:
db:
image: mysql:5.5
restart: always
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: verysecret
MYSQL_USER: appdb
MYSQL_PASSWORD: secret
MYSQL_DATABASE: appdb
volumes:
- db-data:/var/lib/mysql
web:
build: .
command: bundle exec rails s -p 3000
volumes:
- .:/app
ports:
- "3000:3000"
links:
- db
depends_on:
- db
Can I add a darabase more in environment part somehow?
You can create only one DB per Mysql container with docker compose. In your case, I think you should create a second DB container for the second database (isolated from the "real" DB, which is a good practice).
If you really want to have the 2 databases in the same container, you will have to create a Dockerfile based on the Mysql image, and add your command lines (RUN) to create the second DB.
HTH
Yes, you can, but it won't necessarily be as a single container, but a manager for coupling containers. If this is okay, I've added some steps that will help you configure your project. If not, I've added how to run a single image from a docker-compose file
You will want to start off by creating a docker-compose.yml file in the source directory for your project.
Then you'll want to add something like this inside your yml file.(this was taken from Docker's quick-start documentation. Modified to show mysql instead of postgres)
version: '3'
services:
db:
image: mysql
web:
build: .
command: bundle exec rails s -p 3306 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
Detail on how they create one can be found here:
https://docs.docker.com/compose/rails/#define-the-project.
Things to note:
web is the details about the rails container. So you will want to add an image property if you have already created your rails image.
Also, build: . is expecting your Dockerfile to be in the same location as your project. So if you create this docker-compose.yml somewhere else, you'll have to provide the path.
depends_on allows your app to build the DB before running rails
Once you finished creating the docker-compose.yml file run:
docker-compose build
followed by:
docker-compose up
If separating the containers, isn't what you are looking for: then you might want to look into creating a single image running both applications. Then use something like this to run from docker-compose.
version: '3'
services:
app:
image: {your-app-image}
build: .
volumes:
- .:/myapp
ports:
- "3000:3000"
- "3306:3306"
Note: somethings might vary on how you create your image from the Dockerfile.