У меня проблемы с созданием контейнера, содержащего веб-приложение python / bottle, для доступа к mysql серверу / контейнеру.
docker-compose build
:
Step 8/9 : RUN python3 bin/initdb.py
---> Running in 30d963677711
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/pymysql/connections.py", line 583, in connect
**kwargs)
File "/usr/lib/python3.7/socket.py", line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "bin/initdb.py", line 18, in <module>
password=config('MYSQL_PASSWORD', default='tspass')
File "/usr/local/lib/python3.7/dist-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/pymysql/connections.py", line 325, in __init__
self.connect()
File "/usr/local/lib/python3.7/dist-packages/pymysql/connections.py", line 630, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'db' ([Errno -2] Name or service not known)")
ERROR: Service 'web' failed to build: The command '/bin/sh -c python3 bin/initdb.py' returned a non-zero code: 1
docker-compose.yml
:
version: '3'
services:
db:
container_name: mysql
image: mysql
env_file: .env
restart: always
volumes:
- ./init.sql:/app/init.sql
networks:
- "dock"
expose:
- "3306"
# on host it's mapped on 33060
web:
container_name: app
build: .
env_file: .env
restart: always
depends_on:
- db
links:
- db:db
networks:
- "dock"
ports:
- "8888:8888"
networks:
dock:
Dockerfile
:
FROM debian:stable
EXPOSE 8888
# CMD ["./bin/run.sh"]
RUN apt-get update && \
apt-get install -y --no-install-recommends build-essential python3 python3-dev python3-pip python3-setuptools
RUN pip3 install --upgrade pip && \
rm -rf /var/lib/apt/lists/* && rm -rf /var/cache/apt/archives/*.deb
WORKDIR /app
COPY . /app/
RUN pip install --no-cache-dir -r /app/requirements.txt
RUN python3 bin/initdb.py
RUN python3 main.py 0.0.0.0 8888
initdb.py
:
#!/usr/bin/env python
import pymysql
from decouple import Config, RepositoryEnv
...
conn = pymysql.connect(
host=config('DB_HOST', default='db'), # NOT ACCESSIBLE?
port=config('DB_PORT', default=3306, cast=int),
db=config('MYSQL_DATABASE', default='db1'),
user=config('MYSQL_USER', default='user'),
password=config('MYSQL_PASSWORD', default='password')
)
.env
:
# DEPLOYMENT
DB_HOST=db
DB_PORT=3306
# DOCKER MYSQL
MYSQL_HOST="0.0.0.0"
MYSQL_ROOT_HOST="%"
MYSQL_ROOT_USER=root
MYSQL_ROOT_PASSWORD=toor
MYSQL_DATABASE=db1
MYSQL_USER=user
MYSQL_PASSWORD=password
Есть какие-нибудь мысли, как это отладить?
PS: Вышеуказанная установка прекрасно работает без docker, изменив DB_HOST=db
на DB_HOST=localhost
и запустив python main.py