docker for windows & mysql official container - mysql

I'm running MySQL inside the latest official MySQL docker container.
The host machine is Windows 10.
I'm using "docker-compose up mysql" to start the following service:
version: '3.4'
services:
mysql:
image: mysql/mysql-server:latest
container_name: sqlstore
ports:
- '3306:3306'
expose:
- '3306'
env_file:
- ./sqlconfig.env
volumes:
- ./data:/var/lib/mysql
The problem that I'm having is when MySQL database files reside on the host's volume, MySQL fails to run and I keep getting the following error message:
"do you already have another mysqld server running on the socket: "/var/lib/mysql/mysql.sock""
It is worth noting that I did make sure that the port is not being used by other processes, and MySQL's files are indeed being written to the ./data directory on the host.
However, when the MySQL files do not reside on the host but inside the docker itself, everything runs correctly.

I had the same problem. If you're on Linux container, the issue is very likely related to different file-system type that the MySQL container has and the Windows host machine. Using named volumes i.e. some-data:/var/lib/mysql might resolve the issue. Working example:
version: '3.4'
services:
mysql:
   image: mysql/mysql-server:latest
    container_name: sqlstore
    ports:
     - '3306:3306'
    expose:
     - '3306'
    env_file:
     - ./sqlconfig.env
    volumes:
     - datavol:/var/lib/mysql
volumes:
datavol:

Related

'Unknown MySQL server host' when attaching to MySQL container

I am trying to attach to my MySQL container to ensure the data I wanted to transfer to the volume and container is being applied correctly. Ideally I was trying to do this by attaching via CLI through the Docker Desktop software. However trying to run mysql I get a Unknown MySQL server host '127.0.0.1:3306' (-2) error. I have tried changing the MYSQL_HOST variable to 127.0.0.1, 0.0.0.0, and localhost.
Here is a copy of my docker-compose.yml file:
version: "3.7"
services:
mysql:
image: mysql
env_file: compose.env
volumes:
- db-data:/var/lib/mysql
ports:
- 3306:3306
volumes:
db-data:
UPDATE: So I removed the MYSQL_HOST environment variable from compose.env and now I can attach and query data. Not sure why this was a conflict though.

FastAPI-mysql docker. mysql return (2003, "Can't connect to MySQL server on '127.0.0.1')

this is my docker-compose-yml file. first i tried to run the db service it work fined and docker running successfully. but when I run the app service, in the terminal it says connected to database but I get an error as (2003, "Can't connect to MySQL server on '127.0.0.1')
version: '4'
services:
app:
build: .
ports:
- "8000:8000"
env_file:
- .env
depends_on:
- db
links:
- db
db:
environment:
- MYSQL_ROOT_PASSWORD=root
-
image: mysql
ports:
- "3307:3307"
env_file:
- .env
enter image description here
What environment variable is the FastAPI app using to connect to the MySQL host?
If you're using docker-compose networking, services need to use the container name in order to reach each other (i.e. mysql://db:3307/db_name_here) not localhost.
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
More information on networking can be found in their docs here.
Also worth noting, since you're using links you can set aliases as well like so:
version: "3.9"
services:
web:
build: .
links:
- "db:database"
db:
image: postgres
Links Docs Source

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.

Docker django mysql.sock in different location

I'm trying to containerize my django file, and I keep running into the issue:(2006, ’Can\‘t connect to local MySQL server through socket \‘/var/run/mysqld/mysqld.sock\’ (2 “No such file or directory”)
I found out later mysql.sock is in this location:/tmp/mysql.sock instead of /var/run/mysqld/mysqld.sock, how do I change the location for docker to see /tmp/mysql.sock
Here is my docker-composr.yml:
version: '3'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: somepassword
adminer:
image: adminer
restart: always
ports:
- 8080:8080
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
I have followed the instructions on the mysql docker website to link mysql instance to a container
EDIT: I read another stack overflow similar to this, I changed my django code to 'HOST': '127.0.0.1' in DATABASES now I get : (2006, 'Can\'t connect to MySQL server on \'127.0.0.1\' (111 "Connection refused")')
Your host should be db. When using docker-compose, you address different servers by their service name.
So, in settings.py, you should have:
DATABASES = {
'default': {
'HOST': 'db',
...
}
}
If you want to connect to your containerized MySQL server both inside and outside of the container, you'll first need to make sure the port is mapped on the host machine:
services:
db:
image: mysql
ports:
- "3306:3306"
...
That will allow you to access MySQL using localhost or 127.0.0.1 directly on your host machine.
If you want to be able to run Django in both the web container and also on the host, you'll need to override the DATABASES setting depending upon the scenario. The web container will need to use a HOST value of db, whereas your local machine will need a value of localhost.

Local development with docker compose - Using Mysql localhost

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>