phpmyadmin 如何从 Docker 到流浪汉?

jm2pwxwz  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(173)

下面是我的docker-compose文件,它运行良好,我想使用Vagrant重现相同的结果:

version: '3.7'
services:
  db:
    image: mysql:5.7.36
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test_db
    ports:
      - "3308:3306"
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    restart: always
    environment:
      PMA_HOST: db
      PMA_USER: root
      PMA_PASSWORD: root
    ports:
      - "8080:80"

执行docker-compose up并访问localhost:8080 phpmyadmin工作正常。当我尝试与vagrant相同时,容器被构建并运行,但phpmyadmin无法与mysql容器通信。以下是我的Vagrantfile


# -*- mode: ruby -*-

# vi: set ft=ruby :

Vagrant.configure("2") do |config|
    config.vm.define "mysql" do |db|
        db.vm.network "forwarded_port", guest: 3306, host: 3308
        db.vm.hostname = "mysqldb"
        db.vm.provider "docker" do |d|
          d.image = "mysql:5.7.36"
          d.env = {
            :MYSQL_ROOT_PASSWORD  => "root",
            :MYSQL_DATBASE        => "test_db"
          }
          d.remains_running = "true"
        end
      end
    config.vm.define "phpmyadmin" do |pa|
        pa.vm.network "forwarded_port", guest: 80, host: 8080
        pa.vm.hostname = "phpmyadmin"
        pa.vm.provider "docker" do |d|
            d.image = "phpmyadmin/phpmyadmin:latest"
            d.env = {
                :PMA_HOST       => "mysqldb",
                :PMA_USER       => "root",
                :PMA_PASSWORD   => "root"
            }
            d.remains_running = "true"
        end
    end
end

如何让phpmyadminMySQLvagrant一起工作?

xytpbqjk

xytpbqjk1#

我有办法了。我需要在顶层定义网络。


# -*- mode: ruby -*-

# vi: set ft=ruby :

# This file works exactly like the docker-compose.yml file.

Vagrant.configure("2") do |config|
    # Define the network, using which the containers can communicate
    config.vm.network :private_network, type: "dhcp"
    config.vm.define "mysql" do |db|
        # Without prot forwardign vagrant entwork doesnt work
        db.vm.network "forwarded_port", guest: 3306, host: 3306
        db.vm.hostname = "mysqldb"
        db.vm.provider "docker" do |d|
          d.image = "mysql:5.7.36"
          d.env = {
            :MYSQL_ROOT_PASSWORD  => "root",
            :MYSQL_DATBASE        => "test_db"
          }
          d.remains_running = "true"
        end
      end
    config.vm.define "phpmyadmin" do |pa|
        pa.vm.network "forwarded_port", guest: 80, host: 8080
        pa.vm.hostname = "phpmyadmin"
        pa.vm.provider "docker" do |d|
            d.image = "phpmyadmin/phpmyadmin:latest"
            d.env = {
                :PMA_HOST       => "mysqldb",
                :PMA_USER       => "root",
                :PMA_PASSWORD   => "root",
                # Without specifying the specific port phpadmin container doesn't work
                :PMA_PORT       => "3306", 
            }
            d.remains_running = "true"
        end
    end
end

我还需要解决另一个问题--如何定义虚拟机之间的依赖关系。只有在mysql启动并运行后才能构建虚拟机phpmyadmin
docker-compose不同的是,不能在vagrant中设置直接依赖关系。但是可以通过设置选项--no-parallel(例如:-vagrant up --no-parallel)来进行顺序构建。
即使docker-compose设置了依赖关系,依赖关系也只是在等待依赖容器构建的程度上,而不是在等待容器中的服务启动和运行。
因此,即使在vagrant中按顺序构建之后,依赖的服务也无法连接。这与vagrantdocker-compose无关。容器中的服务需要等待其依赖的服务启动。
因此,我在定时循环中的依赖容器中添加了一个延迟,依赖服务将尝试连接,失败后将等待30秒再次连接,尝试10次后它将放弃。

相关问题