Local development with docker compose - Using Mysql localhost - mysql

I am a beginner with Docker and docker-compose for local development on my Mac. Currently, if I restart my containers, the MySql data is wiped out.
I would like to enable persistent storage for MySql local development.
Is there a way I can connect my application running inside docker container to the MySql running on my Mac Host machine?
My docker-compose.yaml file:
mysql:
build: ./mysql
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: ******
restart: on-failure
ports:
- 3306:3306
Apart from connecting docker application to MySql on local machine, is there any other way to achieve this?

you can solve this problem by using volumes in docker-compose.yaml file. please refer the following links
link1,
link2. whenever a docker container runs, it will map the volume to the container. Syntax for the volumes in docker-compose.yaml is as follows:
volumes:
- <path in host>:<path in container>

Related

How can I use local mysql data in a mysql container created through docker-compose

My company has been working with local setups of our mysql database for years. We have recently decided to adopting a containerized approach to local development, and we want to add the database into being run in a container. The issue is, because all of our data is already set up locally, we want to be able to just use the same data in the mysql container. I have tried using volumes to mount the directory storing all the mysql data into the container to no avail. Has anyone had success with doing this?
db part of docker-compose.yml:
db:
image: mysql:5.6
container_name: mysql
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- /usr/local/mysql/data:/var/lib/mysql
I am able to get mysql running fine and am able to connect to it easily from my local machine, but when I connect, none of the local databases that already exist are there. Is there something that I'm overlooking?
#yourknightmares,
So I just ran a test and it worked for me. Here is what I did:
docker-compose.yml
version: "3.9"
services:
nginx:
image: nginx:latest
ports:
- "9999:9999"
command: tail -f /dev/null
volumes:
- "/etc/nginx/nginx.conf:/opt/nginx/nginx.conf"
In my host machine, I have the file at /etc/nginx/nginx.conf
, then:
$ docker-compose up -d
$ docker exec -it 02ba7032d699 bash
$ root#02ba7032d699:/# cat /opt/nginx/nginx.conf
#hello
The file was mounted just fine from the host to the container. I would suggest you to do the same exercise just for troubleshooting purposes. Also, have you looked at the container logs with docker logs container_id?

Docker MySQL - can't connect from Spring Boot app to MySQL database

What I'm trying to do is, connect from my spring-boot app to mysql database in Docker. Each in their own container.
But I must be having something wrong because I can't do it.
To keep it simple :
application-properties :
# URL for the mysql db
spring.datasource.url=jdbc:mysql://workaround-mysql:3308/workaround?serverTimezone=UTC&max_allowed_packet=15728640
# User name in mysql
spring.datasource.username=springuser
# Password for mysql
spring.datasource.password=admin
#Port at which application runs
server.port=8080
docker-compose for MySQL:
version: '3'
services:
workaround-mysql:
container_name: workaround-mysql
image: mysql
environment:
MYSQL_DATABASE: workaround
MYSQL_USER: springuser
MYSQL_PASSWORD: admin
MYSQL_ROOT_PASSWORD: admin
MYSQL_ROOT_HOST: '%'
ports:
- "3308:3306"
restart: always
So pretty simple right ? Database I start with docker-compose up:
All seems to be working fine so far.
Now that I have db started, to the application, this is its docker-compose.yml:
version: '3'
services:
workaround:
restart: always
# will build ./docker/workaround/Dockerfile
build: ./docker/workaround
working_dir: /workaround
volumes:
- ./:/workaround
- ~/.m2:/root/.m2
expose:
- "8080"
command: "mvn clean spring-boot:run"
For its Dockerfile I use Linux Alpine and Java.
FROM alpine:3.9
....add java...
RUN apk update
RUN apk add dos2unix --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/community/ --allow-untrusted
RUN apk add bash
RUN apk add maven
Super simple. Now let's start the application :
Unknown host, so let's try the IP then :
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' workaround-mysql
# URL for the mysql db
spring.datasource.url=jdbc:mysql://172.20.0.2:3308/workaround?serverTimezone=UTC&max_allowed_packet=15728640
Now I get timeout:
As you can see I get error. What is wrong with my setup and how to fix
this? Either I have unknown host exception or Refused to connect or connection timeout.
I have tried:
Using ip of a container in my application.properties, didn't work
Different ports for MySQL and application
Different images and versions of MySQL
Having everything in one docker compose with wait
timer for database.
Minimal setup with
https://github.com/hellokoding/hellokoding-courses/tree/master/docker-examples/dockercompose-springboot-mysql-nginx
Also resulted in communication link failure, Site was accessible but I
doubt that db was connected properly.
Notes:
I run this all on one computer I use port 3308 because I have local
MySQL db at 3306.
Here is docker ps -a
#Vusal ANSWER output :
Only thing different from code in answer I did wait for database to be ready 30 seconds
command: /bin/bash -c "sleep 30;mvn clean spring-boot:run;"
Try this docker-compose.yml:
version: '3'
services:
workaround-mysql:
container_name: workaround-mysql
image: mysql
environment:
MYSQL_DATABASE: workaround
MYSQL_USER: springuser
MYSQL_PASSWORD: admin
MYSQL_ROOT_PASSWORD: admin
MYSQL_ROOT_HOST: '%'
ports:
- "3308:3306"
restart: always
workaround:
depends_on:
- workaround-mysql
restart: always
# will build ./docker/workaround/Dockerfile
build: ./docker/workaround
working_dir: /workaround
volumes:
- ./:/workaround
- ~/.m2:/root/.m2
expose:
- "8080"
command: "mvn clean spring-boot:run"
And update your application.properties to use the next JDBC connection url:
spring.datasource.url=jdbc:mysql://workaround-mysql:3306/workaround?serverTimezone=UTC&max_allowed_packet=15728640
It should work when both containers in the same docker-compose file, because docker-compose creates default network for containers, so they can resolve each other by name.
What you haven't tried so far is running both containers on the same Docker network.
First, forget about IP addressing - using it should be avoided by all means.
Second, launch both compose instances with the same Docker network.
Third, do not expose ports - inside bridge network all ports are accessible to running containers.
Create global network
docker network create foo
Modify both compose files so that they use this network instead of creating each one its own:
version: '3.5'
services:
....
networks:
default:
external: true
name: foo
Remove expose directives from compose files - inside one network all ports are exposed by default
Modify connection strings to use default 3306 port instead of 3308
Enjoy
In order for the service to connect with MySql through docker it has to be in same network, look into Docker network
But for better solution I would suggest you to write a single docker compose file for MySql and Spring boot.The reason is it will easily be linked when you do that.No need any other configuration.
version: "3"
services:
mysql-service:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_DATABASE=db
- MYSQL_USER=root
- MYSQL_PASSWORD=pass
- MYSQL_ROOT_PASSWORD=pass
spring-service:
image: springservce:latest
ports:
- "8080:8080"
depends_on:
- mysql-service
Before you try to connect to the Docker container you should stop mysql in your computer then go to the application.properties and type:
spring.datasource.url=jdbc:mysql://localhost:3306/NAME_OF_YOUR_DB_HERE?useSSL=false&allowPublicKeyRetrieval=true
Regarding localhost, you should inspect the mysql container and pick the IP address and use it instead. most likely is 172.17.0.2. If it did not work then use localhost.

connection laravel with mysql container on docker

On Docker I already have a Laravel container and Container MySQL, how to connect the MySQL container and container Laravel on Docker?
This is what docker-compose was made for!
Check out this tutorial: https://docs.docker.com/compose/wordpress/
It's trying to do something similar: connect wordpress to mysql. The key is that both the docker containers defined in docker-compose.yml share the same network - and you can refer to each container by using their logical name. See how the WORDPRESS_DB_HOST environment variable is set to db:3306 - that will resolve to the IP of the mysql container within the docker network.
Basically, all containers must run in the same network.
# create your network
$ docker network create laravel
# start your container and link it to your network
$ docker run -d --network="laravel" --name="mysql01" mysql:8.0
# after your mysql is up and running, connect your second and third container like this
$ docker run -d --network="laravel" --name="latihananakit_web" yourimage:yourtag
$ docker run -d --network="laravel" --name="latihananakit_app" yourimage:yourtag
I'd recommend to use docker-compose for this scenario, because it makes the whole docker run-thing a lot easier.
See here for reference:
https://docs.docker.com/compose/
https://github.com/bitnami/bitnami-docker-laravel/blob/master/docker-compose.yml
TL;DR:
Create your docker-compose.yml like this (you may change environment-variables or other configuration upon your need):
version: '2'
services:
mariadb:
image: 'bitnami/mariadb:latest'
environment:
- ALLOW_EMPTY_PASSWORD=yes
- MARIADB_USER=my_user
- MARIADB_DATABASE=my_database
- MARIADB_PASSWORD=my_password
myapp:
tty: true
image: bitnami/laravel:5-debian-9
environment:
- DB_HOST=mariadb
- DB_USERNAME=my_user
- DB_DATABASE=my_database
- DB_PASSWORD=my_password
depends_on:
- mariadb
ports:
- 3000:3000
volumes:
- ./:/app
And get everything up and running by executing docker-compose up -d in the same directory.

can't find the mysql database in docker-compose

I specify a volume for myself in the docker-compose.yaml but nothing appears in this directory. I can connect to the db from mysql workbench and via nodejs
npm package "mysql": "^2.15.0".
I can stop the docker containers and restart them and the db is persisted. If I restart my PC, the db disappears and it needs to be recreated.
Q: What is the correct way to specify a location for the mysql db in a docker-compose.yaml file so the db is persisted?
My environment is a windows pc running docker Version 18.03.0-ce-win59 (16762).
My docker-compose.yaml is as follows:
mysql-purple:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: purple
ports:
- "3308:3306" # externaly use 3308 example mysql workbench localhost: 3308)
volumes:
- "C:/Users/john_/Documents/mySQLdatabases:/var/lib/mysql:rw"
This is a known issue with docker on windows, check Volumes from docker-compose file no longer working after a reboot

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