I'm relatively new to Docker so bear with me. I have a Python webapp and a MySQL DB running in the same Docker container.
$ vi Dockerfile
FROM mysql
ENV MYSQL_HOST=127.0.0.1 \
MYSQL_ROOT_PASSWORD=pass
ADD testDB.sql /docker-entrypoint-initdb.d
EXPOSE 3306
FROM python:3.4
RUN pip install Flask
RUN pip install flask_cors
RUN pip install mysql-connector==2.1.6
COPY . /app
WORKDIR /app
ENTRYPOINT ["python"]
CMD ["Api.py"]
Here's a snippet from the API which should connect to the DB:
d_host='127.0.0.1'
d_user='root'
d_password='pass'
d_database='testDB'
@webapp.route('/testGet')
def testGet():
import mysql.connector
conn = mysql.connector.connect(host=d_host,
user=d_user,
password=d_password,
database=d_database, )
cursor = conn.cursor()
SQL = """SELECT text FROM testing ORDER BY testID DESC LIMIT 1;"""
cursor.execute( SQL )
c = cursor.fetchone()
conn.commit()
cursor.close()
conn.close()
return c[0]
However, I keep getting the following error:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (111 Connection refused)
Any help/ideas are greatly appreciated.
EDIT
MySQL wasn't running. Now it is running, but I'm still getting the same error. Additionally, my .sql dump file is not being imported. The updated code:
$ vi Dockerfile:
FROM mysql
ADD dump.sql /docker-entrypoint-initdb.d
FROM python:3.4
RUN pip install Flask
RUN pip install flask_cors
RUN pip install mysql-connector==2.1.6
COPY . /app
WORKDIR /app
ENTRYPOINT ["python"]
CMD ["Api.py"]
I feel like I should be importing the MySQL in this file though:
$ vi docker-compose.yml
version: '2.1'
services:
web:
build: .
ports:
- "5000:5000"
links:
- mysql
container_name: flask_app
mysql:
image: mysql
container_name: db
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- "3306:3306"
The current code is based on this question: Docker Compose mysql import .sql