Access to mysql container from other container

前端 未结 3 1735
执笔经年
执笔经年 2020-12-15 18:29

I have setup docker container with mysql that expose 3306. I\'ve specified database user, database password and create a test db and give the privileges to new user.
In

相关标签:
3条回答
  • 2020-12-15 18:43

    The --link flag is considered a legacy feature, you should use user-defined networks.

    You can run both containers on the same network:

    docker run -d --name php_container --network my_network my_php_image
    
    docker run -d --name mysql_container --network my_network my_mysql_image
    

    Every container on that network will be able to communicate with each other using the container name as hostname.

    0 讨论(0)
  • 2020-12-15 18:43

    In your docker-compose.yml file add a link property to your webserver service: https://docs.docker.com/compose/networking/#links

    Then in your query string, the host parameter's value is your database service name:

    $mysqli = new mysqli("database", "mattia", "prova", "prova");
    
    0 讨论(0)
  • 2020-12-15 18:53

    You need to link your docker containers together with --link flag in docker run command or using link feature in docker-compose. For instance:

    docker run -d -name app-container-name --link mysql-container-name app-image-name
    

    In this way docker will add the IP address of the mysql container into /etc/hosts file of your application container. For a complete document refer to: MySQL Docker Containers: Understanding the basics

    0 讨论(0)
提交回复
热议问题