Deploy mysql during build from Dockerfile - mysql

I am building an application which has parent and child dependency and to build of my application which is the final stage of build i need to connect to mysql for it during build stage itself.
In this i am getting the error:
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
I have mentioned my docker file code i am using and for mysql i have pulled image from dockerhub following instructions from below link:
https://dev.mysql.com/doc/mysql-installation-excerpt/5.5/en/docker-mysql-getting-started.html
And i was planning to run this as a separate container using bridge to communicate with my above container using below command:
docker run -d -name app-container-name --link mysql-container-name app-image-name
FROM maven:3.5.4-jdk-8 as maven
COPY ZP ZP
COPY CommonApp CommonApp
RUN cd ZP && mvn clean install
RUN cd CommonApp && mvn clean install package -U && mvn install:install-file -Dfile=/CommonApp/target/commonapp-0.0.1-SNAPSHOT.jar -DgroupId=com.z -DartifactId=commonapp -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar;
FROM mysql:5.7
# ROOT PASSWORD
ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_USER=root
ENV MYSQL_PASSWORD=root
ENV MYSQL_DATA_DIR=/var/lib/mysql \
MYSQL_RUN_DIR=/run/mysqld \
MYSQL_LOG_DIR=/var/log/mysql
RUN /etc/init.d/mysql start && \
mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'root';FLUSH PRIVILEGES;"
#PORT
EXPOSE 3306
FROM maven:3.5.4-jdk-8
COPY ZCApp ZCApp
RUN cd ZCApp && mvn clean package -U
How should i approach this problem. How can i build mysql along with the application itself using dockerfile.?

Had the same issue when built maven project. What makes this different from similar requests is that here you don't link two running containers but instead you link docker daemon, preforming build process, to running container.
For Docker to get access to database during build you have to expose ports of database. Using --link will have no effect because it links containers (and you dont have second container at the moment) and btw is considered as obsolete technique.
You have to explicitly start database container before build process and somehow expose its ports for docker daemon to access them.
Option 1 - using host networking.
First start database:
docker run -d --network=host mysql
Then build:
docker built -t foo .
Docker will see database on localhost during build process because database uses host's network and doesn't need any port exposion.
Option 2 - Expose ports
First start database:
docker run -d -p 3306:3306 mysql
Then build:
docker built -t foo .
Docker will again see database on localhost during build process because port is exposed.
What you have to double check is your connection string in mvn. It has to use localhost and default tcp port 3306

Related

Is it possible to restore removed mysql docker container sql data? [duplicate]

I was running mariadb instance on docker windows toolkit. I did a env vaiable change on the mariaDB container using kitematic. Now it has recreated an instance loosing all my database. Is there a way to recover from this ?
Checked if threre are dangling volumes, and there are few
docker volume ls -f dangling=true
Got the data recovered using the dangling volumes.
Approach is as following.
First get the list of dangling volumes.
$ docker volume ls -f dangling=true
DRIVER VOLUME NAME
local 6f79b6329d98379495a284287e32a7a57605483dd7bf7fa19924fb2a98fb1d19
local 47bb077ef6f6df9f56bd30c35eeb45f35e62213d2c50db6f078bfdeeee6698ec
Then mounted it on to a Ubuntu container (so that you can go inside the directory and check what is there, as there is no other way to do this when you are using Docker Tool Box on windows)
$ docker run --name tempContainer1-UBUNTU -v 6f79b6329d98379495a284287e32a7a57605483dd7bf7fa19924fb2a98fb1d19:/var/lib/backup -t -i ubuntu /bin/bash
Then you will be inside the bash of newly created contianer. Go to newly mounted directory and check content
$cd /var/lib/backup
$ls
$aria_log.00000001 aria_log_control ib_buffer_pool ib_logfile0 ib_logfile1 ibdata1 ibtmp1 multi-master.info mysql performance_schema
-- once you are sure directory data is what you require, make a zip file of the folder
$apt-get update
$apt-get install zip
$cd ..
$zip -r backup.zip backup
On another terminal from host copy the content of container backup.zip to host
$docker cp tempContainer1-UBUNTU:/var/lib/backup.zip .
Then create a docker compose file like following and mount the backup folder as data directory. Run this on linux host as this mounting will not work as expected for mysql on windows.
version: "3.2"
services:
mysql:
image: mariadb:10.4.12
restart: always
ports:
- "3306:3306"
command: mysqld --innodb-flush-method=littlesync --innodb-use-native-aio=ON --log_bin=ON
volumes:
- ./backup_data_folder:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: somepassword
TZ: Asia/Singapore
networks:
- frontend
container_name: maria
networks:
frontend:
Start
$docker-compose up
Once it is up, from another terminal go inside newly created container
$docker exec -t -i maria /bin/bash
-- Take dump of all the DBS
$mysqldump -u root -p --all-databases > alldb.sql
Copy content of the dump to host from another terminal from host
$docker cp maria:/alldb.sql .
Now this sql file is a full dump, restore it as usual on your mysql DB or contianer.
mysql -u root -p < alldb.sql
Recently I had to face the same problem for a lost wordpress container and I've followed the instructions from Don. However, as there were lots of dangling volumes, I had to optimize the process. I've managed a way to do it simpler, in the same terminal, resulting in the following steps:
docker volume ls -f dangling=true
DRIVER VOLUME NAME
local 43277666c8bc3da0b585d90952c2303226c92c6c6a561007c0c7ee00b6da817e
local 4fde3ea412e54a1b8b42fce6dae5de5135b9fd12933b020b43bd482cd5fd2225
local 52074ccfd62fb83b8b40cff5f8024215b34f79ad09b630e067ec732f811f798c
...
Then, for each container execute the following instruction, replacing 43277666c8bc3d... with each VOLUME NAME found. This instruction will remove previously maria-restore containers if they exist, create a new one and attach to it:
docker container ls -a -q --filter "name=maria-restore" && docker container rm -f maria-restore; docker run --name maria-restore -v 43277666c8bc3da0b585d90952c2303226c92c6c6a561007c0c7ee00b6da817e:/var/lib/mysql -d mariadb:10.4.12 mysqld --innodb-flush-method=littlesync --innodb-use-native-aio=ON --log_bin=ON && docker exec -it maria-restore bash
If it wasn't a mysql volume, it will fail and exit immediately. If it is a mysql volume, you'll be inside the mariadb container. The database will be already started. You can then connect to the database to see if it is the right one and backup it:
root#8b35c8e2c474:/# mysql -uadmin -p
root#8b35c8e2c474:/# mysqldump -uadmin -p --all-databases > alldb.sql
root#8b35c8e2c474:/# exit
Copy the backed up database:
docker cp mysql-restore:/alldb.sql .
Finally you'll have to clean up the maria-restore container:
docker container ls -a -q --filter "name=maria-restore" && docker container rm -f maria-restore

Are possible docker run migrate with only one specific migration_file?

I tried to migrate using docker at my golang app with mysql database. But i just want to do one specific migration_file not whole migration_files.
Source:
https://github.com/golang-migrate/migrate
What i found:
docker run -v {{ migration dir }}:/migrations --network host migrate/migrate
-path=/migrations/ -database "mysql://user:password#tcp(host:port)/database up 2
note: 2 mean's the first 2 migration_files
What i used:
sudo docker run -v /home/user/app/schema:/migrations --network host migrate/migrate
-path=/migrations/ -database "mysql://root:root#tcp(localhost:8080)/my_db" up
I want docker run migrate the last/newest migration_file/specific migration_file, but i don't know how.

using docker images for mySQL and redmine, how do I resolve "Unknown MySQL server host"?

I am using the docker images supplied at https://hub.docker.com/_/redmine
I have chosen to use MySQL as my database backend. So I have 2 docker containers: MySQL and Redmine, as downloaded from dockerhub.
Following the instructions on the docker/redmine link above, I ran through the commands and found that the redmine docker would not start. Inspecting the docker logs, I see:
rake aborted!
Mysql2::Error::ConnectionError: Unknown MySQL server host redmine (-5)
I thought the 2 dockers were having difficulty talking to each other, so I setup a new docker network for both containers to use:
docker network create --driver bridge redmine-net
Adapting the instructions, on the docker/redmine link above, I run
docker run -d name our-mysql --network redmine-net -e MYSQL_USER=redmine -e MYSQL_PASSWORD=todays-password -e MYSQL_DATABASE=redmine -e MYSQL_RANDOM_ROOT_PASSWORD=1 -p 3306:3306 mysql:5.7
docker run -d name our-redmine --network redmine-net -e REDMINE_DB_MYSQL=redmine -e REDMINE_DB_USERNAME=redmine -e REDMINE_DB_PASSWORD=todays-password redmine:latest
However, the redmine contain still falls over instantly, with the same error.
EDIT Using the *.yml file as provided in the dockerhub redmine instructions works pretty faultlessly.
So the question is: what is the docker-compose method doing that docker run isn't handling?
Thank you.
The REDMINE_DB_MYSQL arg of the redmine container do reference to the mysql container, so, if you define the database service like our-mysql, then set REDMINE_DB_MYSQL=our-mysql

How to install wordpress by docker?

Question:
I follow some guides to install the wordpress + mysql by docker, but found that not work... I tried to test by curl command, found no any output, I need your help for the issue...
(I just transfer my wordpress hosting to VPS)
docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
docker run --name wordpress --link mysql:mysql -e WORDPRESS_DB_PASSWORD=123456 -d wordpress:4.8.2-apache
[root#vps ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6bd3954390e0 wordpress:4.8.2-apache "docker-entrypoint..." 11 seconds ago Up 10 seconds 80/tcp wordpress
eaa1f6a2fb96 mysql "docker-entrypoint..." 25 seconds ago Up 24 seconds 3306/tcp mysql
Follow troubleshooting and test wordpress:
[root#vps ~]# curl localhost:80
curl: (7) Failed connect to localhost:80; Connection refused
[root#vps ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' 6bd3954390e0
172.17.0.3
[root#vps ~]# curl 172.17.0.3:80
[root#vps ~]#
Resolved:
Thanks #junius(who in docker forums), #VladoDemcak, #yamenk and #user4860092! The issue was resolved!
If I do “docker run xxxx” that should not work for me, that maybe was caused by command incorrect. Then I tried to do docker-compose, curl no any output, but Wordpress should work normal. So curl no any output should normal.
Now, I completed task that transfer my Wordpress to new VPS, share follow tips and experiences:
Suggest follow docker official guide if you want to install WP by docker.
If you want to mapping the mysql and wordpress, can add follow
config in compose:
If you not config port part in compose, that mean not expose any
port to outside of container, so you couldn't access the port from
outside, as follow:
When you change “docker-compose.yml”, please not only use
“docker-compose down” that will not delete all config/file, suggest
you do “docker-compose down --volumes” that as install guide.
If you change database name, please add “WORDPRESS_DB_NAME: xxx” in
environment part of Wordpress(docker-compose.yml), that should no
this config in official install guide. So wordpress default connect
database name is “wordpress”.
If you want to debug wordpress/mysql and check log, you can not add
“-d”, use this “docker-compose up”
In order to restore mysql database, you can install phpmyadmin by
docker, then add follow config to “docker-compose.yml”, and follow
guide by “https://hub.docker.com/r/phpmyadmin/phpmyadmin/”
If you want to add some software in docker of Wordpress, e.g: zip,
mailx, you can do follow:
You don't have exposed ports, so you are not able to access wordpress (from host) which is running on the port 80 in docker container.
Probably you will need to expose port to some other port (not 80). So try to change docker run command for wordpress as follows:
docker run --name wordpress --link mysql:mysql -e WORDPRESS_DB_PASSWORD=123456 -p 81:80 -d wordpress:4.8.2-apache
Please notice -p 81:80 parameter in the command - Docker documentation expose-incoming-ports.
After that wordpress should be available on localhost:81.
I would suggest you to create docker-compose for your services rather than maintain linking and with docker-compose you are also able to run both services with one command.
There is a very detailed explanation in the official docker docs to how to do this. Follow the link below, and you shall get wordpress up and running.
https://docs.docker.com/compose/wordpress/

unable to connect to dockerized mysql container locally

I am still a beginner with docker, trying to use docker to help in my development prototyping. My environment is Mac using boot2docker, version as below
Client version: 1.3.1
Client API version: 1.15
Go version (client): go1.3.3
Git commit (client): 4e9bbfa
OS/Arch (client): darwin/amd64
Server version: 1.3.2
Server API version: 1.15
Go version (server): go1.3.3
Git commit (server): 39fa2fa
I ran the command as below:
docker run --name mymysql -e MYSQL_ROOT_PASSWORD=mypw -e MYSQL_DATABASE=bullshit -d mysql -p 3306:3306
docker start mymysql
I can see the process running as below:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22d3f780c270 mysql:5 "/entrypoint.sh -p 3 2 minutes ago Up 2 seconds 3306/tcp mymysql
However I still could not connect to the mysql instance running in the docker. I tried connect to the ip retrieved by :
$ boot2docker ip
The VM's Host only interface IP address is: 192.168.59.103
Please give me a pointer on how to solve this issue, I went through the tutorial but I am not sure what went wrong.
The command you used should give an error. The syntax for docker run is as follow:
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
You have to submit the options to docker run before specifying the image used (mysql in your case), and if it's the case, the command and possible argument(s) to that command.
Not specifying a command will run the default command in the image.
Before running again the container you should stop and remove the old one:
docker kill mymysql
docker rm mymysql
And, following your example you should run:
docker run --name mymysql -e MYSQL_ROOT_PASSWORD=mypw -e MYSQL_DATABASE=bullshit -p 3306:3306 -d mysql
As you set manually a port mapping from container's port 3306 to the same port of your Boot2docker VM, you should can access to MySQL using the IP of the Boot2docker instance, typically 192.168.59.103, and connecting to port 3306.