0

I am currently beginning experiments with docker-compse. I've created a minimal Flask + MySQL webservice which works perfectly fine without Docker. Now I would like to see how I can make deployment easy with Docker.

My docker-compose.yml looks like this:

version: '3'
services:
  web:
    build: .
    command: app.py
    ports:
     - "8082:8082"
    volumes:
     - .:/code
    links:
     - db
    hostname: myappserver
    environment:
    - MYSQL_ROOT_PASSWORD=p@ssw0rd123
    - MYSQL_DATABASE=flask_db
    - MYSQL_HOST=db
    - MYSQL_PORT=3306
  db:
    image: mysql
    ports:
    - "3307:3306"
    environment:
    - MYSQL_ROOT_PASSWORD=p@ssw0rd123
    - MYSQL_DATABASE=flask_db
    - MYSQL_HOST=mysqlserver

When I run sudo docker-compose up --build I get

web_1  | sqlalchemy.exc.ProgrammingError: (mysql.connector.errors.ProgrammingError) 1146 (42S02): Table 'performance_schema.session_variables' doesn't exist [SQL: "SHOW VARIABLES LIKE 'sql_mode'"]
db_1   | 2017-07-21T22:27:26.553761Z 3 [Note] Aborted connection 3 to db: 'flask_db' user: 'root' host: '172.18.0.3' (Got an error reading communication packets)

What is the problem and how do I fix it?

(The complete project is here: https://github.com/MartinThoma/flask_mysql_dockerized - it's really tiny)

No duplicate of

  • 1: I know if this was on my machine, that I could fix it with mysql_upgrade -u root -p --force and /etc/init.d/mysql start. But the point of using docker compose is to make it really simple to start a web service. One command. If I have to tell the user to login into the container, run those commands, restart the other container.. then I could simply ask him to install it on the main system / run docker containers manually.

So here is what I did to fix it, where I hope that this is possible to be done less manually:

Start the containers:
  # docker-comopse up

Find the container ID of the mysql image:
  # docker container ls

Enter the container
  # sudo docker exec -it 4f7d3f0763ad bash

Upgrade
  # mysql_upgrade -u root -p --force

Exit the container, then restart
  # docker-compose up

Initialize the database:
  # cd /docker-entrypoint-initdb.d/
  # mysql -h localhost -u root -p
  mysql> source flask_db.sql;
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • @Dalton Sure, if this was simply running on my machine I would restart it. But how do I solve this problem with docker-compose? How can I make sure that the user can start the complete web service with one single command? – Martin Thoma Jul 21 '17 at 22:35
  • I can't reproduce, what happens if you pull the latest ubuntu and mysql images and rebuild? – Luis Orduz Jul 22 '17 at 04:50

1 Answers1

1

You need to add database to your mysql service:

volumes:
  - ./flask_db.sql:/docker-entrypoint-initdb.d/flask_db.sql

Tip: It is recommended to use smaller images so that your image has only required basic components and free from extra non-essential stuff.

Ayushya
  • 9,599
  • 6
  • 41
  • 57