无法使用 docker-compose.yml

时间:2021-07-26 23:57:17

标签: docker docker-compose dockerfile

无法使用 docker-compose.yml 在 docker 之间建立连接

我是 docker 世界的新手,所以如果我的问题不清楚,请提前道歉。所以我想要做的是有两个码头工人,其中一个是休息服务,第二个是休息客户端。我正在尝试建立两者之间的网络连接

docker rest服务路径/Users/gsha/learning/producer 我的休息服务详情

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }
}

package hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SimpleRest {

    @RequestMapping("/")
    public String getHome(){
        return "Hello From Producer";
    }

}

server.port=8085
server.servlet.context-path=/producer-service

FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]

休息客户信息

路径 /Users/gsha/learning/consumer

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

}
   

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class SimpleRestConsumer {

    @RequestMapping("/")
    public String getHello(){
        RestTemplate restTemplate = new RestTemplate();
        String val = restTemplate.getForObject("http://localhost:8085/producer-service", String.class);
        return val;
    }

}

server.port=8086
server.servlet.context-path=/consumer-service
address.service.base-path=http://localhost:8085/producer-service

FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]

yaml 文件的路径:/Users/gsha/learning/docker-compose.yml

version: "3"

services:
  producer-service:
    build:
      context: ../producer-service
      dockerfile: ./Dockerfile
    ports:
      - 8085:8085

  consumer-service:
    build:
      context: ../consumer-service
      dockerfile: ./Dockerfile
    ports:
      - 8086:8086
    depends_on:
      - producer-service

执行部署时出现此错误:

<块引用>

docker-compose up --build

[+] 建筑 0.0s (0/0)
无法准备上下文:找不到路径“../producer-service”

1 个答案:

答案 0 :(得分:0)

您的服务名称无效。看起来您不小心在不属于它的地方添加了 :。你写过:

  consumer:-service:
    build:
    ...

但这应该是:

  consumer-service:
    build:
    ...

您的新错误是:

<块引用>

无法准备上下文:找不到路径“../producer-service”

正如@Don'tPanic 的评论所说,这似乎很清楚:您要求 docker-compose 在包含您的 producer-service 文件的父目录中查找名为 docker-compose.yml 的目录,并且不存在这样的目录。