Maroder
Maroder

Reputation: 115

docker-compose, migrate from links. (Flask app + mysql)

I have followed the flask mega tutorial currently done the Docker part. I tried to make a docker-compose.yml which wokrs great, but I understand that I shouldnt use "links" anymore.

https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xix-deployment-on-docker-containers/page/0#comments

I've tried using "depends_on" -mysql, but that doesn't work. Also adding mysql:dbserver, which restults in "undefined" when I run docker compose up.

version: '3'
services:
    web:
      build: .
      ports:
      - 8000:5000

      environment:
        SECRET_KEY: my-secret-key
        DATABASE_URL: mysql+pymysql://microblog:pw@dbserver/microblog
      links:
      - mysql:dbserver

    mysql:
      image: mysql/mysql-server:latest
      environment:
        MYSQL_RANDOM_ROOT_PASSWORD: random
        MYSQL_DATABASE: microblog
        MYSQL_USER: microblog
        MYSQL_PASSWORD: pw

Would really appreciate if someone could help me "rewriting" the "links" part to something not being depreciated as links now are. Also other comments on the yml file is appreciated.

Upvotes: 1

Views: 218

Answers (1)

David Maze
David Maze

Reputation: 158908

The names of the various services: blocks are directly usable as hostnames. In your example, remove links:, and change the database URL to be mysql+pymysql://microblog:pw@mysql/microblog (changing the hostname part).

You do not need to specify hostname:, container_name:, expose:, or networks:. depends_on: ensures that a docker-compose up web will also start the database, but isn't necessary for networking to work. If either container specifies network_mode: host connection scheme doesn't work (and you can usually remove this option).

ports: is also optional, though it's helpful if you're trying to access a given service from outside Docker space. If you're trying to remap ports using ports: (or if you don't have ports: at all) you need to connect to the port that the container is using internally, not the published port number (in the case of ports:, the second port number; if a third service was involved it could reach http://web:5000).

Networking in Compose in the Docker documentation has extended details.

Upvotes: 2

Related Questions