I am using docker with docker compose on my server (debian 7.5).
I have 1 mysql container and one postgresql container for 2 applications.
When i want to create a backup of my database (for example for the database "mydb" from my mysql container) I do like this :
docker exec -it <my_container_id> mysqldump --opt mydb > "/backup/mydb$(date +%Y%m%dà%H%M).sql"
It works very well. The backup is saved in my local server.
The thing is I want to create this task everyday. So i created a file "backup.sh" in /etc/cron.daily/. (and then I did chmod +x backup.sh).
But the problem is, the cron doesn't work at all. I can see in the log that the file is executed, but I don't have anything in my backup folder.
Someone has an issue ?
Thank you very much
Cron does not set the PATH unless you specify it.
Try with the docker absolute path (/usr/bin/docker most likely) in your shell script.
Related
Greetings and thanks in advance, I'm actually new to docker and docker-compose, watching a lot of videos and reading a lot of articles so far along with trying things.
I've got a front end container and a back end container that build and run alone as a Dockerfile and in a docker-compose setup.
(I've been building with Dockerfile first and then integrating the containers into docker-compose to make sure i understand things correctly)
I'm at the point where i need my database info, since i'll use docker-compose, as i understand it, it should build under the same network with a react front end and django back end.
I have a backup mysql dump file that I'm working with, what i think i need to do is have a container running mysql server and serving out my tables (like I have it locally working). I haven't been able to figure out how to import the backup into my docker mysql container.
Any help is appreciated.
What I've tried so far is using docker in the command line to outline the pieces i'll need in the Dockerfile and then what to move into the docker-compose as mentioned above:
docker run -d --name root -e MYSQL_ROOT_PASSWORD=root mysql # to create my db container
Then I've tried a bunch of commands and permutations of commands, recently in the CLI, here are some of my most recent trials and errors:
docker exec -i root mysql -uroot -proot --force < /Users/homeImac/Downloads/dump-dev-2020-11-10-22-43-06.dmp
ERROR 1046 (3D000) at line 22: No database selected
docker exec -i f803170ce38b sh -c 'exec mysql -uroot -p"root"' < /Users/homeImac/Downloads/dump-dev-2020-11-10-22-43-06.dmp
ERROR 1046 (3D000) at line 22: No database selected
docker exec -i f803170ce38b sh -c 'exec mysql -uroot -h 192.168.1.51 -p"root"' < /Users/homeImac/Downloads/dump-dev-2020-11-10-22-43-06.dmp
ERROR 1045 (28000): Access denied for user 'root'#'homeimac' (using password: YES)
I've scoured the web so far and i'm not sure where to go next, have I got the right idea? If anyone has an example of how to import a database dump (in dmp or dmp.gz), once i get that working, I'll actually do that in the docker-compose file.
Thinking about it, i just have to create the container and import so I might not even need a Dockerfile.
I'll cross that bridge when i get there. This is what I'm thinking though:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'app'
etc etc
I've learned a lot super fast, maybe too fast. Thanks for any tips!
The answer to your question is given in the docker hub page of MySQL.
Initializing a fresh instance
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint->initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
In your docker-compose.yml use:
volumes:
- ${PWD}/config/start.sql:/docker-entrypoint-initdb.d/start.sql
and that's it.
Here's the answer that worked for me after working with 2 colleagues that know backend better where I work.
It was pretty simple actually. I created a directory in my repo that would be empty.
I added *.sql and *.dmp to my .gitignore so the dump files would not increase the size of my image.
That directory using docker-compose would be used as a volume under the mysql service:
volumes:
- ~/workspace/app:/workspace/app
The dump file is placed there and is imported into the sql service when I run:
mysql -u app -papp app < /path/to/the/dumpfile
I can go in using docker exec and verify not only the database is there but the tables from my dump file are there as well.
For me, I had to create a new superuser also in my backend container through our Django app.
python3 manage.py createsuperuser
With that, then logging in on localhost:8000/api, everything was linked up between the mysql, backend, and frontend containers.
Hope this helps! I'm sure not all the details are the same for others post volumes, but using volumes, I didn't have to copy any dump file in and it ended up automatically imported and served. That was my big issue.
another way:
docker exec -i containername mysql -uroot -ppassword mysql < dump.sql
from the folder where dump.sql resides
I am facing one issue with docker, L am using laradock docker env for laravel. Since it has issue with mysql version I had to run those command:
$ docker-compose exec mysql bash
$ mysql -u root -p
ALTER USER 'root'#'localhost' indentified WITH mysql_native_password BY 'root';
Also imported database through http://localhost:8080 and phpmyadmin
So I am trying to reproduce this issue again, so I deleted everything from docker with
$ docker system prune
but when I rebuild the containers
sudo docker-compose up -d nginx mysql phpmyadmin workspace
My previous database is loaded again.
So my question would be how to delete db and MySQL settings, so I can execute the alter command and import database again.
Overall I am trying to determine if this issue with MySQL will occur on another platform again, so I am trying to reproduce it from scratch and that is why I need to reset completely MySQL env and databases.
So not sure where MySQL settings are stored and how to delete them.
MySQL is storing most of the important information of your container in a volume.
Now, the command:
docker system prune
do not remove the volumes, per default.
If you also want to remove them, you can run:
docker system prune --volumes
If you do want to list or act on those specific volumes:
docker volume --help
would give you all the commands on volumes like rm, ls, ...
Im having some trouble filling my DB when installing a kar on karaf (Im using liquibase).
Im using windows 10, but using linux containers in docker, karaf 4.2.1 and mysql 5.7
There are the steps I did:
1-Went to task manager and stoped the mysql service.
2-Created and run a mysql 5.7 container doing the following:
docker run --name mysql-container -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
Connected using 127.0.0.1 root/root in heidiSql and created the required DB's so when installing the kar liquibase will fill them with data.
3-Copied the kar to the repo folder in my karaf folder, added the folder to .tar, and then to gzip (.tar.gzip)
4-Created my karaf container using:
.\build.sh --from-local-dist --archive apache-karaf-with-kar.tar.gz
5-Did run karaf in the background:
docker run -d -p 127.0.0.1:8101:8101 --name=karaf-container karaf
Now I used putty and ssh to connect to karaf - localhost:8101. enter the user karaf and pass karaf.
All ok, I do the kar:install command, kar install but the bundles are in grace period.
I go to heidiSql and see that the DB's are not filled with data.
I go back to karaf connection, do a feature:refresh and i get erros like:
Error resolving artifact org.ops4j.pax.transx:pax-transx-features:xml:features:0.3.0: [Could not transfer artifact org.ops4j.pax.transx:pax-transx-features:xml:features:0.3.0 from/to central (http://repo1.maven.org/maven2/): Failed to transfer file: http://repo1.maven.org/maven2/org/ops4j/pax/transx/pax-transx-features/0.3.0/pax-transx-features-0.3.0-features.xml. Return code is: 501 , ReasonPhrase:HTTPS Required.]
I suposse the problem here is that somehow my connection to mysql doesn't work.
After some time the ssh connection is lost (I suposse this is normal if I dont do anything for some time there).
I tried to create a mysql-server container instead, but that way im not even able to connect using heidiSql.
Adicional question:
1-Where do I find the link to the kubernetes dashboard? I cant find it on docker.
2-I was having a first error when trying to docker run karaf:
\"karaf\": executable file not found in $PATH": unknown.
I resolved that because I realised that my karaf folder had less files than a downloaded binary one.
Basically the files are the files you see when you open the folder, the build.md, license, etc...
So I did copy paste to my folder of those files and I was able to run it. Im asking, because its still a mystery to me...
Thanks for your attention
edit: tried stoping the mysql-container and started mysql from the task manager and its the same, now im sure that for some reason its not connecting to the DB's. so I guess the problem is when I create a karaf container, maybe im missing some configurations. gonna keep trying
After 2 days I finally found the problem.
First, let me say that I did try too to create a network
docker network create mynet
and used the tag docker run --network=mynet in both mysql-container and karaf-container.
In my hook, I did try to replace localhost with mysql-container:3306 and even after that it didn't work.
Anyway, to resolve, I did what I said above, plus create a new super user:
docker exec -it mysql-container mysql -u root -p
CREATE USER 'user'#'localhost' IDENTIFIED BY 'pass';
GRANT ALL PRIVILEGES ON *.* TO 'user'#'%';
FLUSH PRIVILEGES;
And after that, all worked fine.
I will leave here a usefull link that was kinda hard to find but it has pretty good info:
https://codebeamer.com/cb/wiki/5854748
plus, for the 2 extra questions I asked:
1- Still didnt find the kube dashboard, didn't look much for it yet to be honest.
2- For what I have seen in the build.sh it will search a specific folder name 'apache-karaf', so I think this error here happens if you rename your folder and it doesnt match with the build.sh.
I am using Docker to run someone's app. One of the containers is the web app and the sibling container is mysql. I need to dump a table from the DB locally but I'm not sure how to connect to it. I ran docker inspect [container name] on the mysql container and I see the IP address listed but now what do i do with that?
Try:
docker exec {container} mysqldump {mysqldump option} > dump.sql
Hopefully mysqldump is in the container.
I'm trying to use MySQL docker container in my host system to make installation and configuration processes much easier and faster.
So, I've pulled an image from:
https://hub.docker.com/r/mysql/mysql-server/
Then started container based on this image..
Container started fine, but I was not able to connect to this DB from my host system (everything is ok if I try to connect from container). It failed with a message:
ERROR 1130 (HY000): Host '<here goes my IP>' is not allowed to connect to this MySQL server
So, as I understand, my root user has no enough permissions.
I've entered my container:
docker exec -it mysql bash
Connected to DB:
mysql -uroot -ppassword
Updated permissions for my root user:
use mysql;
UPDATE user SET Host="%" WHERE User='root';
It's updated fine.
Than I decided to save my updated image somehow... I've found this guide:
http://docs.oracle.com/cd/E52668_01/E75728/html/section_c5q_n2z_fp.html
After executing:
docker stop mysql
docker commit -m "Fixed permissions for root user" -a "Few words about author" `docker ps -l -q` myrepo/mysql:v1
docker rm mysql
docker run --name new-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pass -d myrepo/mysql:v1
I've found that my root user hasn't permissions again.
What is wrong here?
How to public my updated image into my Dockerhub?
My original answer is for persisting the change in the MySQL data after it has been initialized. But since you want to do this in the image for every initialization automatically there is a different approach for this. You can use one of the following options:
There is an environment variable called MYSQL_ROOT_HOST for this image where you can set the host (https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/docker-entrypoint.sh#L63-L69). You should be able to set this to % to allow all hosts to connect as root such as -e MYSQL_ROOT_HOST="%".
The image supports adding SQL files to /docker-entrypoint-initdb.d/ to be initialized on startup (https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/docker-entrypoint.sh#L98-L105). You can create your SQL file that has UPDATE mysql.user SET Host="%" WHERE User='root'; in it and then ADD that file to /docker-entrypoint-initdb.d/ in your own image. Then, when starting a container based on that image it will initialize that SQL file.
That image specifies a default volume to hold the MySQL data at https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/Dockerfile#L11. When you start the container, a volume is created for that container. When you update the permissions for the root user, it is saved in this volume (it is actually part of MySQL data for the mysql database). But once you remove the container, that volume is also lost.
There are usually two things you can do in this case to preserve the data between container restarts or even new containers:
Create a named volume and mount the data there. To do this you can run docker volume create mysqldata. Then, when starting the container mount the data with -v mysqldata:/var/lib/mysql. This volume will persist even after you stop or delete your MySQL container.
Bind mount the data to a host folder. Instead of creating a volume, you can just mount a folder such as -v /mnt/mysqldata:/var/lib/mysql. This will persist all your MySQL data on the host at /mnt/mysqldata.
Though, these are not the only ways to persist data, they are two built-in methods. There are also Docker volume plugins that allow you to use other storage mediums (examples might be https://github.com/rancher/convoy for NFS and https://github.com/NetApp/netappdvp for NetApp).
docker exec -it mysql bash
chown -R mysql:mysql /var/lib/mysql
if you change permission of volume in host, above code will correct permission denied for root.