Docker和PHP。如何向容器发出cURL请求?

时间:2018-09-29 04:06:44

标签: php docker

我试图弄清楚如何从我的API向同一泊坞窗主机上的另一个服务容器发出cURL请求。

这是我的docker-compose文件:

# docker-compose.yml for development only!
version: '3'
services:
  traefik:
    image: traefik:latest
    build: ./loadbalancer
    restart: always
    ports:
      - 80:80
      - 443:443
    networks:
      - web
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./loadbalancer/traefik.toml:/traefik.toml
      - ./loadbalancer/certs:/certs
    container_name: traefik

  api:
    image: richarvey/nginx-php-fpm:latest
    restart: always
    volumes:
      - "./api:/var/www/html"
    networks:
      - web
      - default
    env_file:
      - "./api/dev.env"
    labels:
      - "traefik.enable=true"
      - "traefik.basic.frontend.rule=Host:addmysnap.local;PathPrefixStrip:/api"
      - "traefik.basic.port=80"

  frontend:
    image: nginx:latest
    restart: always
    networks:
      - web
      - default
    volumes:
      - ./frontend/conf/nginx/addmysnap.template:/etc/nginx/conf.d/default.conf
      - ./frontend/public:/var/www/html
    labels:
      - "traefik.enable=true"
      - "traefik.basic.frontend.rule=Host:addmysnap.local"
      - "traefik.basic.port=80"

  thumbor:
    image: minimalcompact/thumbor:latest
    restart: always
    networks:
      - web
      - default
    volumes:
      - ./data:/data
    labels:
      - "traefik.enable=true"
      - "traefik.basic.frontend.rule=Host:snaps.addmysnap.local"
      - "traefik.basic.port=80"
    environment:
      UPLOAD_ENABLED: "true"
      ALLOW_UNSAFE_URL: "false"

  database:
    image: mysql:latest
    command: [ "--default-authentication-plugin=mysql_native_password" ]
    restart: always
    ports:
      - 8083:3306
    environment:
      MYSQL_DATABASE: addmysnap
      MYSQL_ROOT_PASSWORD: changeme
      MYSQL_USER: api
      MYSQL_PASSWORD: changeme

networks:
  web:
    external: true

api服务的PHP文件中,我尝试向thumbor服务发出这样的请求:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://thumbdor/image");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $decodedImage);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  'Content-Type: ' . $decodedImage->getMimeType(),
  'Slug: ' . uniqid(rand(), true) . '.' . $decodedImage->getFormat()
]);
$result = curl_exec ($ch);

但是此请求失败。

是否可以像这样向其他容器发出卷曲请求?

0 个答案:

没有答案