How to migrate to db on docker (Symfony)?

Viewed 1133
version: '3'

services:
    db:
        image: mysql:8.0.20
        command: --default-authentication-plugin=mysql_native_password
        volumes:
            - "db_app:/var/lib/mysql"
        environment:
            MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
            MYSQL_DATABASE: ${MYSQL_DATABASE}
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}
        ports:
            - 3306:3306
        networks:
            - symfony
    php:
        build:
            context: .
            dockerfile: docker/php/Dockerfile
            args:
                TIMEZONE: ${TIMEZONE}
        volumes:
            - ./symfony/:/Users/admin/Downloads/symfony-docker-master/symfony
        networks:
            - symfony
    nginx:
        build:
            context: .
            dockerfile: docker/nginx/Dockerfile
        volumes:
            - ./symfony/:/Users/admin/Downloads/symfony-docker-master/symfony
        ports:
            - 8050:8050
        networks:
            - symfony

volumes:
    db_app:

networks:
    symfony:

The application starts, connects to the database via

DATABASE_URL=mysql://user:123@db:3306/db

But when performing migrations through the console, I get the error

An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddr  
  esses: getaddrinfo failed: nodename nor servname provided, or not known

As I understand it, due to the fact that the address is specified in the connection db.

How to make migrations?

2 Answers

You need to run console commands from inside of your php container

docker-compose exec php bin/console doctrine:migrations:migrate

You should connect to the container name.

These can be found by running docker container ls on your console.

It's easier when you specify the name in your compose file like so:

services:
    db:
        container_name: db # This can be anything
        image: mysql:8.0.20
Related