Flask + MySQL + PHP + Docker-Compose = Pain
im trying to build a simple To do app with docker-compose having 3 Containers: One Flask Rest API with sqlalchemy and marshmallow, one PHP to call my Rest-Api and one MySQL Database. The error im getting is:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003,
"Can't connect to MySQL server on 'db' ([Errno 111] Connection refused)")
(Background on this error at: http://sqlalche.me/e/e3q8)
And i cant call my rest api from my php container.
Here is the code with the important lines:
my docker-compose.yml:
version: '3.1' #compose version
services:
flaskapi-service:
build:
context: ./restapi #relative to docker-compose file directory
dockerfile: DOCKERFILE
volumes:
- ./restapi:/usr/src/app #mounting
ports:
- 5001:5001 #host:container
depends_on:
- db
restart: on-failure
db:
image: mysql:latest
restart: always
environment:
MYSQL_USER: username
MYSQL_PASSWORD: password
MYSQL_DATABASE: todo
MYSQL_ROOT_PASSWORD: password
ports:
- "3306:3306"
php-page:
build:
context: ./frontend
dockerfile: DOCKERFILE
volumes:
- ./frontend:/var/www/html #mount
ports:
- 5002:80 #host:container
depends_on:
- flaskapi-service
my rest-api:
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import pymysql
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] =
'mysql+pymysql://username:password@db:3306/todo'
#order matters: ORM before serialization tool
db = SQLAlchemy(app)
ma = Marshmallow(app)
how im calling the rest-api from php:
<?php
$date_time = date('Y-m-d H:i:s');
$data = array(
'description' => $_POST['description'],
'deadline' => $_POST['deadline'],
'createdAt' => $date_time,
'finished' => 'false'
);
$encodedJSON = json_encode($data);
//Initiate cURL-handle
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://flaskapi-service/todo',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $encodedJSON,
CURLOPT_HTTPHEADER => array('Content-Type: application/json')
));
$result = curl_exec($ch);
echo $result;
curl_close($ch);
header('Location: index.php');
?>
Any help is appreciated and the correct answer marked as correct.
mysql docker flask sqlalchemy docker-compose
add a comment |
im trying to build a simple To do app with docker-compose having 3 Containers: One Flask Rest API with sqlalchemy and marshmallow, one PHP to call my Rest-Api and one MySQL Database. The error im getting is:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003,
"Can't connect to MySQL server on 'db' ([Errno 111] Connection refused)")
(Background on this error at: http://sqlalche.me/e/e3q8)
And i cant call my rest api from my php container.
Here is the code with the important lines:
my docker-compose.yml:
version: '3.1' #compose version
services:
flaskapi-service:
build:
context: ./restapi #relative to docker-compose file directory
dockerfile: DOCKERFILE
volumes:
- ./restapi:/usr/src/app #mounting
ports:
- 5001:5001 #host:container
depends_on:
- db
restart: on-failure
db:
image: mysql:latest
restart: always
environment:
MYSQL_USER: username
MYSQL_PASSWORD: password
MYSQL_DATABASE: todo
MYSQL_ROOT_PASSWORD: password
ports:
- "3306:3306"
php-page:
build:
context: ./frontend
dockerfile: DOCKERFILE
volumes:
- ./frontend:/var/www/html #mount
ports:
- 5002:80 #host:container
depends_on:
- flaskapi-service
my rest-api:
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import pymysql
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] =
'mysql+pymysql://username:password@db:3306/todo'
#order matters: ORM before serialization tool
db = SQLAlchemy(app)
ma = Marshmallow(app)
how im calling the rest-api from php:
<?php
$date_time = date('Y-m-d H:i:s');
$data = array(
'description' => $_POST['description'],
'deadline' => $_POST['deadline'],
'createdAt' => $date_time,
'finished' => 'false'
);
$encodedJSON = json_encode($data);
//Initiate cURL-handle
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://flaskapi-service/todo',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $encodedJSON,
CURLOPT_HTTPHEADER => array('Content-Type: application/json')
));
$result = curl_exec($ch);
echo $result;
curl_close($ch);
header('Location: index.php');
?>
Any help is appreciated and the correct answer marked as correct.
mysql docker flask sqlalchemy docker-compose
add a comment |
im trying to build a simple To do app with docker-compose having 3 Containers: One Flask Rest API with sqlalchemy and marshmallow, one PHP to call my Rest-Api and one MySQL Database. The error im getting is:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003,
"Can't connect to MySQL server on 'db' ([Errno 111] Connection refused)")
(Background on this error at: http://sqlalche.me/e/e3q8)
And i cant call my rest api from my php container.
Here is the code with the important lines:
my docker-compose.yml:
version: '3.1' #compose version
services:
flaskapi-service:
build:
context: ./restapi #relative to docker-compose file directory
dockerfile: DOCKERFILE
volumes:
- ./restapi:/usr/src/app #mounting
ports:
- 5001:5001 #host:container
depends_on:
- db
restart: on-failure
db:
image: mysql:latest
restart: always
environment:
MYSQL_USER: username
MYSQL_PASSWORD: password
MYSQL_DATABASE: todo
MYSQL_ROOT_PASSWORD: password
ports:
- "3306:3306"
php-page:
build:
context: ./frontend
dockerfile: DOCKERFILE
volumes:
- ./frontend:/var/www/html #mount
ports:
- 5002:80 #host:container
depends_on:
- flaskapi-service
my rest-api:
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import pymysql
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] =
'mysql+pymysql://username:password@db:3306/todo'
#order matters: ORM before serialization tool
db = SQLAlchemy(app)
ma = Marshmallow(app)
how im calling the rest-api from php:
<?php
$date_time = date('Y-m-d H:i:s');
$data = array(
'description' => $_POST['description'],
'deadline' => $_POST['deadline'],
'createdAt' => $date_time,
'finished' => 'false'
);
$encodedJSON = json_encode($data);
//Initiate cURL-handle
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://flaskapi-service/todo',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $encodedJSON,
CURLOPT_HTTPHEADER => array('Content-Type: application/json')
));
$result = curl_exec($ch);
echo $result;
curl_close($ch);
header('Location: index.php');
?>
Any help is appreciated and the correct answer marked as correct.
mysql docker flask sqlalchemy docker-compose
im trying to build a simple To do app with docker-compose having 3 Containers: One Flask Rest API with sqlalchemy and marshmallow, one PHP to call my Rest-Api and one MySQL Database. The error im getting is:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003,
"Can't connect to MySQL server on 'db' ([Errno 111] Connection refused)")
(Background on this error at: http://sqlalche.me/e/e3q8)
And i cant call my rest api from my php container.
Here is the code with the important lines:
my docker-compose.yml:
version: '3.1' #compose version
services:
flaskapi-service:
build:
context: ./restapi #relative to docker-compose file directory
dockerfile: DOCKERFILE
volumes:
- ./restapi:/usr/src/app #mounting
ports:
- 5001:5001 #host:container
depends_on:
- db
restart: on-failure
db:
image: mysql:latest
restart: always
environment:
MYSQL_USER: username
MYSQL_PASSWORD: password
MYSQL_DATABASE: todo
MYSQL_ROOT_PASSWORD: password
ports:
- "3306:3306"
php-page:
build:
context: ./frontend
dockerfile: DOCKERFILE
volumes:
- ./frontend:/var/www/html #mount
ports:
- 5002:80 #host:container
depends_on:
- flaskapi-service
my rest-api:
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import pymysql
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] =
'mysql+pymysql://username:password@db:3306/todo'
#order matters: ORM before serialization tool
db = SQLAlchemy(app)
ma = Marshmallow(app)
how im calling the rest-api from php:
<?php
$date_time = date('Y-m-d H:i:s');
$data = array(
'description' => $_POST['description'],
'deadline' => $_POST['deadline'],
'createdAt' => $date_time,
'finished' => 'false'
);
$encodedJSON = json_encode($data);
//Initiate cURL-handle
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://flaskapi-service/todo',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $encodedJSON,
CURLOPT_HTTPHEADER => array('Content-Type: application/json')
));
$result = curl_exec($ch);
echo $result;
curl_close($ch);
header('Location: index.php');
?>
Any help is appreciated and the correct answer marked as correct.
mysql docker flask sqlalchemy docker-compose
mysql docker flask sqlalchemy docker-compose
asked Nov 5 '18 at 19:05
main.cmain.c
6418
6418
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Instead of "@db" in your SQLALCHEMY_DATABASE_URI, you want the actual address at which you can find the db container from your server.
You can, for example, find out what your docker machine ip is using
$ docker-machine ip
And then replace that ip in your URI. e.g.
'mysql+pymysql://username:password@192.168.0.123:3306/todo'
They might even be on the same host if all containers and on the same machine, so give this a shot:
'mysql+pymysql://username:password@localhost:3306/todo'
You can find other ways to determine the IP address of your MySQL instance in this discussion.
2
db
should be resolvable as a DNS hostname (because they’re both on the same non-default Docker bridge network, because Docker Compose creates one for you, and because Compose also creates network aliases matching the service names) and using it is absolutely preferable to trying to figure out any specific IP address. The questioner may not be using Docker Machine at all.
– David Maze
Nov 5 '18 at 19:58
Didn't know this, thanks for letting me know. What's the protocol here, do I delete my solution?
– jfbeltran
Nov 5 '18 at 19:59
Hi thanks for your answer, but if i try it with localhost i get (Errno 99, Cannot assign requested address) and no im not using docker machine at all
– main.c
Nov 5 '18 at 23:01
add a comment |
I solved this by downgrading from latest MySQL Version image to 5.7:
mysql:5.7
I dont have any clue why this should work since the returned error must be something e.g a misconfiguration with my docker and its networking and shouldnt have nothing to do with mysql. Maybe this helps someone.
Edit: allways check if your Database container is up before trying to connect since docker compose ist starting all containers simultaneously.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53160608%2fflask-mysql-php-docker-compose-pain%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of "@db" in your SQLALCHEMY_DATABASE_URI, you want the actual address at which you can find the db container from your server.
You can, for example, find out what your docker machine ip is using
$ docker-machine ip
And then replace that ip in your URI. e.g.
'mysql+pymysql://username:password@192.168.0.123:3306/todo'
They might even be on the same host if all containers and on the same machine, so give this a shot:
'mysql+pymysql://username:password@localhost:3306/todo'
You can find other ways to determine the IP address of your MySQL instance in this discussion.
2
db
should be resolvable as a DNS hostname (because they’re both on the same non-default Docker bridge network, because Docker Compose creates one for you, and because Compose also creates network aliases matching the service names) and using it is absolutely preferable to trying to figure out any specific IP address. The questioner may not be using Docker Machine at all.
– David Maze
Nov 5 '18 at 19:58
Didn't know this, thanks for letting me know. What's the protocol here, do I delete my solution?
– jfbeltran
Nov 5 '18 at 19:59
Hi thanks for your answer, but if i try it with localhost i get (Errno 99, Cannot assign requested address) and no im not using docker machine at all
– main.c
Nov 5 '18 at 23:01
add a comment |
Instead of "@db" in your SQLALCHEMY_DATABASE_URI, you want the actual address at which you can find the db container from your server.
You can, for example, find out what your docker machine ip is using
$ docker-machine ip
And then replace that ip in your URI. e.g.
'mysql+pymysql://username:password@192.168.0.123:3306/todo'
They might even be on the same host if all containers and on the same machine, so give this a shot:
'mysql+pymysql://username:password@localhost:3306/todo'
You can find other ways to determine the IP address of your MySQL instance in this discussion.
2
db
should be resolvable as a DNS hostname (because they’re both on the same non-default Docker bridge network, because Docker Compose creates one for you, and because Compose also creates network aliases matching the service names) and using it is absolutely preferable to trying to figure out any specific IP address. The questioner may not be using Docker Machine at all.
– David Maze
Nov 5 '18 at 19:58
Didn't know this, thanks for letting me know. What's the protocol here, do I delete my solution?
– jfbeltran
Nov 5 '18 at 19:59
Hi thanks for your answer, but if i try it with localhost i get (Errno 99, Cannot assign requested address) and no im not using docker machine at all
– main.c
Nov 5 '18 at 23:01
add a comment |
Instead of "@db" in your SQLALCHEMY_DATABASE_URI, you want the actual address at which you can find the db container from your server.
You can, for example, find out what your docker machine ip is using
$ docker-machine ip
And then replace that ip in your URI. e.g.
'mysql+pymysql://username:password@192.168.0.123:3306/todo'
They might even be on the same host if all containers and on the same machine, so give this a shot:
'mysql+pymysql://username:password@localhost:3306/todo'
You can find other ways to determine the IP address of your MySQL instance in this discussion.
Instead of "@db" in your SQLALCHEMY_DATABASE_URI, you want the actual address at which you can find the db container from your server.
You can, for example, find out what your docker machine ip is using
$ docker-machine ip
And then replace that ip in your URI. e.g.
'mysql+pymysql://username:password@192.168.0.123:3306/todo'
They might even be on the same host if all containers and on the same machine, so give this a shot:
'mysql+pymysql://username:password@localhost:3306/todo'
You can find other ways to determine the IP address of your MySQL instance in this discussion.
answered Nov 5 '18 at 19:21
jfbeltranjfbeltran
9552817
9552817
2
db
should be resolvable as a DNS hostname (because they’re both on the same non-default Docker bridge network, because Docker Compose creates one for you, and because Compose also creates network aliases matching the service names) and using it is absolutely preferable to trying to figure out any specific IP address. The questioner may not be using Docker Machine at all.
– David Maze
Nov 5 '18 at 19:58
Didn't know this, thanks for letting me know. What's the protocol here, do I delete my solution?
– jfbeltran
Nov 5 '18 at 19:59
Hi thanks for your answer, but if i try it with localhost i get (Errno 99, Cannot assign requested address) and no im not using docker machine at all
– main.c
Nov 5 '18 at 23:01
add a comment |
2
db
should be resolvable as a DNS hostname (because they’re both on the same non-default Docker bridge network, because Docker Compose creates one for you, and because Compose also creates network aliases matching the service names) and using it is absolutely preferable to trying to figure out any specific IP address. The questioner may not be using Docker Machine at all.
– David Maze
Nov 5 '18 at 19:58
Didn't know this, thanks for letting me know. What's the protocol here, do I delete my solution?
– jfbeltran
Nov 5 '18 at 19:59
Hi thanks for your answer, but if i try it with localhost i get (Errno 99, Cannot assign requested address) and no im not using docker machine at all
– main.c
Nov 5 '18 at 23:01
2
2
db
should be resolvable as a DNS hostname (because they’re both on the same non-default Docker bridge network, because Docker Compose creates one for you, and because Compose also creates network aliases matching the service names) and using it is absolutely preferable to trying to figure out any specific IP address. The questioner may not be using Docker Machine at all.– David Maze
Nov 5 '18 at 19:58
db
should be resolvable as a DNS hostname (because they’re both on the same non-default Docker bridge network, because Docker Compose creates one for you, and because Compose also creates network aliases matching the service names) and using it is absolutely preferable to trying to figure out any specific IP address. The questioner may not be using Docker Machine at all.– David Maze
Nov 5 '18 at 19:58
Didn't know this, thanks for letting me know. What's the protocol here, do I delete my solution?
– jfbeltran
Nov 5 '18 at 19:59
Didn't know this, thanks for letting me know. What's the protocol here, do I delete my solution?
– jfbeltran
Nov 5 '18 at 19:59
Hi thanks for your answer, but if i try it with localhost i get (Errno 99, Cannot assign requested address) and no im not using docker machine at all
– main.c
Nov 5 '18 at 23:01
Hi thanks for your answer, but if i try it with localhost i get (Errno 99, Cannot assign requested address) and no im not using docker machine at all
– main.c
Nov 5 '18 at 23:01
add a comment |
I solved this by downgrading from latest MySQL Version image to 5.7:
mysql:5.7
I dont have any clue why this should work since the returned error must be something e.g a misconfiguration with my docker and its networking and shouldnt have nothing to do with mysql. Maybe this helps someone.
Edit: allways check if your Database container is up before trying to connect since docker compose ist starting all containers simultaneously.
add a comment |
I solved this by downgrading from latest MySQL Version image to 5.7:
mysql:5.7
I dont have any clue why this should work since the returned error must be something e.g a misconfiguration with my docker and its networking and shouldnt have nothing to do with mysql. Maybe this helps someone.
Edit: allways check if your Database container is up before trying to connect since docker compose ist starting all containers simultaneously.
add a comment |
I solved this by downgrading from latest MySQL Version image to 5.7:
mysql:5.7
I dont have any clue why this should work since the returned error must be something e.g a misconfiguration with my docker and its networking and shouldnt have nothing to do with mysql. Maybe this helps someone.
Edit: allways check if your Database container is up before trying to connect since docker compose ist starting all containers simultaneously.
I solved this by downgrading from latest MySQL Version image to 5.7:
mysql:5.7
I dont have any clue why this should work since the returned error must be something e.g a misconfiguration with my docker and its networking and shouldnt have nothing to do with mysql. Maybe this helps someone.
Edit: allways check if your Database container is up before trying to connect since docker compose ist starting all containers simultaneously.
edited Nov 15 '18 at 23:03
answered Nov 7 '18 at 10:43
main.cmain.c
6418
6418
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53160608%2fflask-mysql-php-docker-compose-pain%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown