How to get docker compose to read changes to docker-compose.yml? - mysql

I have a docker-compose.yml file, and part of it sets up a MySQL docker container.
Below is the part of the file:
version: "2.2"
services:
mysql:
image: mysql:5.7
hostname: mysql
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: slurm_acct_db
MYSQL_USER: slurm
MYSQL_PASSWORD: password
MYSQL_ROOT_HOST: "%"
MYSQL_HOST: "%"
ports:
- '3306:3306'
volumes:
- var_lib_mysql:/var/lib/mysql
I can log into the MySQL server from the container with:
mysql -u slurm -ppassword
But if I remove the volumes and make changes to the docker-compose.yml e.g. change MYSQL_PASSWORD in the docker-compose.yml file it doesn't seem to have an effect and the old password is still used.
It is probably very obvious, but I can't seem to find a way for the changes to take effect. Can someone point me in the right direction?

look to make your compose file appears immediately then you have to run the command:
docker-compose up -d
this will have the update done for you.
or you have another solution that you create another environment variable outside the compose file and then you will need to update the run again to have an effect as it reads the variables immediately from the current file and any effects have to be applied first.

So I found the issue.
I had started this app using docker-compose in one directory, whilst playing around. I then started the project in a different directory, and docker-compose continued to read the .yml file from the first directory, not the current directory I was working in.
Deleted the old directory, and started with docker-compose up -d from the second directory I was currently working in, and now the changes have been read.

Related

Change MYSQL_DATABASE etc. in docker compose

I am having some issues getting docker to create a completely new image with a new database.
I first started my containers, but had entered wrong credentials. I now want to build the image over with the right credentials.
This is my service
mysql:
build: ./mysql
image: custom-mysql
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: dsadjkasjd
MYSQL_DATABASE: correct_db
MYSQL_USER: correct_user
MYSQL_PASSWORD: correct_pass
networks:
main:
aliases:
- database
ports:
- "3306:3306"
volumes:
- ./data/mysql:/var/lib/mysql
I have tried removing all containers and images manually as well as
docker-compose down --rmi 'all'
docker-compose build --no-cache --pull
But not matter what I try it will only work with the passwords I used the first time around.
Is there some clever way of completely removing the container+image and building it again with a new database/users ?
The script to generate the database from scratch (which evaluates the given environment variables) is only run on the first generation for the volume, as you can see in https://github.com/docker-library/mysql/blob/master/5.7/docker-entrypoint.sh.
You have two possibilites now: either stop the container, remove the volume, and restart the container; or start a bash in the running container, remove the data dir /var/lib/mysql, and restart the container
You have to delete the volume (or make its content empty) so on new startup it will generate the new database files with the new configuration.

Moving Wordpress site to Docker: Error establishing DB connection

Ive been making new sites with Wordpress & Docker recently and have a reasonable grasp of how it all works and Im now looking to move some established sites into Docker.
Ive been following this guide:
https://stephenafamo.com/blog/moving-wordpress-docker-container/
I have everything setup as it should be but when I go to my domain.com:1234 I get the error message 'Error establishing a database connection'. I have changed 'DB HOST' to 'mysql' in wp-config.php as advised and all the DB details from the site Im bringing in are correct.
I have attached to the mysql container and checked that the db is there and with the right user and also made sure the pw is correct via mysql CLI too.
SELinux is set to permissive and I havent changed any dir/file ownership nor permissions and for the latter dirs are all 755 and files 644 as they should be.
Edit: I should mention that database/data and everything under that seem to be owned by user/group 'polkitd input' instead of root.
Docker logs aren't really telling me much either apart from the 500 error messages for the WP container when I browse the site on port 1234 (as expected though).
This is the docker-compose file:
version: '2'
services:
example_db:
image: mysql:latest
container_name: example_db
volumes:
- ./database/data:/var/lib/mysql
- ./database/initdb.d:/docker-entrypoint-initdb.d
restart: always
environment:
MYSQL_ROOT_PASSWORD: password123 # any random string will do
MYSQL_DATABASE: mydomin_db # the name of your mysql database
MYSQL_USER: my domain_me # the name of the database user
MYSQL_PASSWORD: password123 # the password of the mysql user
example:
depends_on:
- example_db
image: wordpress:php7.1 # we're using the image with php7.1
container_name: example
ports:
- "1234:80"
restart: always
links:
- example_db:mysql
volumes:
- ./src:/var/www/html
Suggestions most welcome as Im out of ideas!
With the new version of docker-compose it will look like this (if you don't want to use PhpMyAdmin you can leave it out):
version: '3.7'
volumes:
wp-data:
networks:
wp-back:
services:
db:
image: mysql:5.7
volumes:
- wp-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: rootPassword
MYSQL_DATABASE: wordpress
MYSQL_USER: wp-user
MYSQL_PASSWORD: wp-pass
ports:
- 8889:3306
networks:
- wp-back
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
MYSQL_USER: wp-user
MYSQL_PASSWORD: wp-pass
MYSQL_ROOT_PASSWORD: rootPassword
ports:
- 3001:80
networks:
- wp-back
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- 8888:80
- 443:443
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wp-user
WORDPRESS_DB_PASSWORD: wp-pass
volumes:
- ./wordpress-files:/var/www/html
container_name: wordpress-site
networks:
- wp-back
The database volume is a named volume wp-data, while the wordpress html is a bind-mount to your current directory ./wordpress-files .
make sure that the wp-config.php file has same credentials defined for db_user, db_password as in docker-composer yml file. I too had similar problem i deleted all the files and re-installed and saw that docker-composer up -d would start everything but the wp-config.php file contents for mysql settings were not defined as in docker. so i changed it accordingly and started working eventually
Please take a look at the following compose script. I tried and tested. It works fine.
version: '2'
services:
db:
image: mysql:latest
container_name: db_server
volumes:
- ./database/data:/var/lib/mysql
- ./database/initdb.d:/docker-entrypoint-initdb.d
restart: always
environment:
MYSQL_ROOT_PASSWORD: password123 # any random string will do
MYSQL_DATABASE: udb_test # the name of your mysql database
MYSQL_USER: me_prname # the name of the database user
MYSQL_PASSWORD: password123 # the password of the mysql user
example:
depends_on:
- db
image: wordpress:php7.1 # we're using the image with php7.1
container_name: wp-web
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: me_prname
WORDPRESS_DB_PASSWORD: password123
WORDPRESS_DB_NAME: udb_test
ports:
- "1234:80"
restart: always
volumes:
- ./src:/var/www/html
Let me know if you encounter further issues.
if you want it all in one container you can refer this repo here,
https://github.com/akshayshikre/lamp-alpine/tree/development
Here from lamp-alpine image is used
Then mysql, php, apache2 (lamp stack) is installed and copied local wordpress demosite and db for demo purpose
if you do not want any kind of continuous integration part ignore .circleci folder
Check docker-compose file and Dockerfile, Environment variables are in .env file
I share with you my approach
Show running version, question to see if all is well on your side!
$ docker --version && docker-compose --version
run Docker Copose file
$ docker-compose -f docker-compose.yml up -d
after you wait fast forward
show running containers and name of the Wordpress Container is listening on port 8000
$ docker ps
you will see the name of your WordPress container on the table as follows if you have followed the steps listed on their site
https://hub.docker.com/_/wordpress
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
xxxxxxxxxxxx wordpress:latest "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:8000->80/tcp cms_wordpress_1
xxxxxxxxxxxx mysql:5.7 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 3306/tcp, 33060/tcp cms_db_1
and if you check your browser with the address : localhost:8000
you will get the message "error establishing DB connection"
launch bash inside the Wordpress container
$ docker exec -it cms_wordpress_1 bash
apt update fails as there is no connectivity
$ apt update
open up new terminal and show current Firewalld configuration
$ sudo cat /etc/firewalld/firewalld-workstation.conf | greb 'FirewallBackend'
currently set to 'nftables'
set value to 'iptables'
$ sudo sed -i 's/FirewallBackend=nftables/FirewallBackend=iptables/g' /etc/firewalld/firewalld-workstation.conf
confirme new value
$ sudo cat /etc/firewalld/firewalld-workstation.conf | grep 'FirewallBackend'
restart Firwalld service to apply change
$ sudo systemctl restart firewalld.service
Refresh the running Wordpress session in your browser and that's good.
good work.
In some cases a probable cause of this issue could be, you have made volumes using docker compose up and then when you did docker compose down you expected the volumes to be deleted as well as the docker images, but this is not how it works.
From the doc you could read this:
For data that needs to persist between updates, use host or named volumes.
It implicitly means that named volumes will not get deleted with down, so what happens is, when you do an up and then add a row to a table and then do a subsequent down, then on the next up you will get the same old volume and so querying the same table would give you the same row you created previously!
What does this have to do with the error Error establishing DB connection, you may ask. To answer your question, let's assume one scenario: What if you changed some MYSQL passwords in the docker compose file in between running the down command and the second up command?
MYSQL keeps its own data just like any other data in its tables, so when you do the second up, Docker loads the old volume (the one created by the first up) and thus the old credential information will be used by MYSQL and Docker will not even have the opportunity to insert your new information (the ones you changed in the docker compose file) in the administration tables. So obviously, you will be rejected.
The solution thus now would be very simple. To fix it, either do:
docker-compose down -v
to remove the named volumes as well as the images when running the down, or do:
docker volume rm [volname]
if you've done the down before, and now you want to delete the named volumes.
If you follow this tutorials ,https://stephenafamo.com/blog/moving-wordpress-docker-container/, your site wil not work properly. Coz It doesn't restore database and you need to restore manually .sql dump file existed in initdb.d dir by using this command.
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
I also stuck in this and my CSS are not working properly.
Please let me know when you have new idea .

Docker on Windows Data Persistence - Host Mapping vs Data Volume

I'm very new to Docker and after reading about data volumes I'm still somewhat confused by the behaviour I'm seeing.
In my compose file I had an entry for mysql like this:
db:
image: mysql
restart: always
volumes:
- ./database:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: p4ssw0rd!
networks:
- back
This mapped the /database directory to /var/lib/mysql. The database files where created and I could start Wordpress, install, add a post. The problem as it never persisted any created data. If I restarted Docker and executed:
docker-compose up -d
The database was empty.
Changing this to:
db:
image: mysql
restart: always
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: p4ssw0rd!
networks:
- back
And adding in a volume like this:
volumes:
db_data:
Now persists the data in the Docker data volume and restarting works. Any data created during the last run is still present.
How would I get this to work using the host mapped directory?
Am I right in thinking the second example using volumes is the way to go?
Docker volumes on windows work a bit different way than Linux. Basically on Windows, docker runs a VM and the docker is setup inside the VM. So it seems to you that you run docker commands locally on Windows but the actual stuff happens in background inside a VM.
docker run -v d:/data:/data alpine ls /data
First you need to make share the D: in docker settings. You can find a detailed article explaining the steps for doing so
https://rominirani.com/docker-on-windows-mounting-host-directories-d96f3f056a2c

Docker mysql data not reflecting chages

I am having some issues trying to run mysql with docker. I used this example
https://github.com/gpuenteallott/golang-mysql-docker-setup/blob/master/docker-compose.yml
Which worked like a charm. SO I could then log into sequel pro host 127.0.0.1:3306. username, password = gotest. Worked perfect. I then wanted to change the db name and login in details to I changed my docker-compose.yaml file to look like this
app_database:
build:
context: .
dockerfile: docker/db/Dockerfile
ports:
- "3306:3306"
environment:
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: big-website-2014
volumes:
- ../../../../_local_mysql_data:/var/lib/mysql
I then run every thing but I cannot seem to log in. I can still log into with the old details and old database is still there. But not any of the new data.
I have tried stoping and removing all containers and images. Rebuild , every thing still no joy.
I have also tried using the docker ip address aswell.
Can some one please educate me on this matter please and thank you.
Your MySQL Data is in host directory ../../../../_local_mysql_data. Remove that directory and then recreate your container with docker-compose up -d

Seeding a database using Docker Mysql COPY fails

Using docker-compose and a build file, I am unable to COPY files to a mysql container. Here's the compose.yml:
version: '3'
services:
db:
#image: mysql:5.7
build:
context: .
dockerfile: Dockerfile-mysql
environment:
MYSQL_ROOT_PASSWORD: drupal
MYSQL_DATABASE: drupal
MYSQL_USER: drupal
MYSQL_PASSWORD: drupal
#volumes:
# - /var/lib/mysql
ports:
- "3300:3306"
Then the Dockerfile-mysql:
FROM mysql:5.7
COPY ./drupal.sql /docker-entrypoint-initdb.d/
I see no errors, and the file isn't there. The container starts up, mysql is good, all that, but NO FILE! Can someone point out what I've missed? I'm assuming after the .sql file is transferred to that directory, the .sql file will run as well?
Thanks!
Figured it out. You have to run docker-compose build first to get docker to run any kind of COPY commands. If I understand it all correctly docker-compose build will create the containers, and docker-compose up will launch said containers.