Dockers MySQL ist auch ein Memo für Anfänger
github url: https://github.com/kenjiSpecial/docker_mysql_python_beginner
docker-compose.yml
docker-compose.yml
version: "3"
services:
mysql_db:
container_name: "mysql_db"
image: mysql:5.7
command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
volumes:
- db_volume:/var/lib/mysql
environment: # Set up mysql database name and password
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: employees
MYSQL_USER: user
MYSQL_PASSWORD: password
networks:
- app-tier
python3:
restart: always
build: ./python
container_name: "python3"
working_dir: "/root/"
tty: true
depends_on:
- mysql_db
networks:
- app-tier
volumes:
- ./python:/root
- pycache_volume:/root/.cache
networks:
app-tier:
driver: bridge
volumes:
db_volume:
pycache_volume:
Geben Sie Python-Containerinformationen in Dockerfile ein
Dockerfile
FROM python:3.7
# ADD . /root
COPY . /opt
WORKDIR /opt
RUN pip install -r requirements.txt
main.py
main.py
import mysql.connector as mysql
user_name = "user"
password = "password"
host = "mysql_db" # docker-Der in compose definierte MySQL-Dienstname
database_name = "employees"
conn = mysql.connect(
host="mysql_db",
user="user",
passwd="password",
port=3306,
database="employees"
)
conn.ping(reconnect=True)
print(conn.is_connected())
Recommended Posts