Connect to MySQL with Python within Docker

Docker's MySQL is also a memo for beginners

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:

Enter Python container information in Dockerfile

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-MySQL service name defined by compose
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

Connect to MySQL with Python within Docker
Connect python to mysql
Connect to Wikipedia with Python
Connect to MySQL with Python on Raspberry Pi
Build Mysql + Python environment with docker
Connect to mysql
Connect to s3 with AWS Lambda Python
Connect to pepper with PEPPER Mac's python interpreter
How to install python3 with docker centos
Use python with docker
Post to slack with Python 3
Three things I was addicted to when using Python and MySQL with Docker
Connect Vagrant's MySQL with MySQL Workbench
Prepare python3 environment with Docker
Connect to sqlite from python
Switch python to 2.7 with alternatives
Write to csv with Python
Connection pooling with Python + MySQL
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Until API made with Flask + MySQL is converted to Docker
Python: How to use async with
Link to get started with python
Connect to multiple databases with SQLAlchemy
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
Output to csv file with Python
Convert list to DataFrame with python
MP3 to WAV conversion with Python
To do tail recursion with Python2
How to get started with Python
Connect to utf8mb4 database from python
What to do with PYTHON release?
Connect to Bitcoin Testnet with Pycoin
Unable to install Python with pyenv
How to use Mysql in python
How to use FTP with Python
Connect to Elastic MQ with boto
How to calculate date with python
Easily post to twitter with Python 3
I want to debug with Python
Application development with Docker + Python + Flask
Connect to MySQL using Flask SQLAlchemy
How to connect to Cloud Firestore from Google Cloud Functions with python code
Try to reproduce color film with Python
Receive textual data from mysql with python
Connect to Docker's MySQL container from Flask
Try logging in to qiita with Python
Change Python 64bit environment to 32bit environment with Anaconda
INSERT into MySQL with Python [For beginners]
English speech recognition with python [speech to text]
Convert memo at once with Python 2to3
Build Jupyter Lab (Python) environment with Docker
HTML email with image to send with python
Memo to ask for KPI with python
Python to remember only with hello, worlds
Behind the flyer: Using Docker with Python
Output color characters to pretty with python
Output Python log to console with GAE
Convert Excel data to JSON with python