无法从同一网络上的容器获得响应,Docker 撰写,Mountebank
Unable to get a response from a container on the same network, Docker compose, Mountebank
我有一个 Go 应用程序和三个 docker 容器,用于应用程序、数据库和 mountebank 以 mock/stub HTTP 响应。
我希望我的江湖骗子容器在test_api(应用程序容器)发出请求时return做出适当的响应。
当我使用 Postman 调用端点时,mountebank 容器 return 是一个正确的响应。
但是,当我在 test_api 容器中的代码发送如下代码的 GET 请求时,它总是收到
dial tcp 127.0.0.1:3000: connect: connection refused
下面是test_api如何向江湖骗子容器发送请求
func getSuper(id string) (*float64, error) {
var url = "http://localhost:3000/ato/employee/?/balance"
url = strings.Replace(url, "?", id, 1)
log.Println("Sending request to this url: ", url)
resp, err := http.Get(url)
if err != nil {
return nil, &errorhandling.RequestError{Context: "getSuper calling ato api", Code: errorhandling.Internal, Message: err.Error()}
}
body, err := resposeToByte(resp)
if err != nil {
log.Println("err in coverting response to byte[]:", err)
return nil, &errorhandling.RequestError{Context: "getsuper resposeToByte", Code: errorhandling.Internal, Message: err.Error()}
}
superData, err := UnmarshalSuperDetails(body)
if err != nil {
return nil, &errorhandling.RequestError{Context: "UnmarshalSuperDetails())", Code: errorhandling.Internal, Message: err.Error()}
}
log.Println("super details ", superData)
return &superData.SuperBalance, nil
}
这是我的 docker-compose
version: '3.7'
services:
db:
container_name: "test_db"
platform: linux/x86_64
build:
context: .
dockerfile: db.Dockerfile
networks:
- default
restart: always
ports:
# <Port exposed> : < MySQL Port running inside container>
- "3306:3306"
# setting some env vars to create the DB
environment:
MYSQL_RANDOM_ROOT_PASSWORD: "secret"
MYSQL_DATABASE: "IGD"
MYSQL_USER: "tester"
MYSQL_PASSWORD: "secret"
# OR if you want to use "root" as the user, just these two lines
# MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
# MYSQL_DATABASE: ${DATABASE_NAME}
# we mount a data volume to make sure we don't lose data
volumes:
- mysql_data:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
api:
container_name: "test_api"
# we want to use the image which is build from our Dockerfile
build:
context: .
dockerfile: api.Dockerfile
ports:
- "8080:8080"
# we are depending on the mysql backend
depends_on:
- db
# We mount the working dir into the container, handy for development
# This is what makes the hot reloading work inside of a Docker container
volumes:
- .:/app/
mountebank:
container_name: mountebank
image: jkris/mountebank:latest
build:
context: .
dockerfile: mb.Dockerfile
volumes:
- ./imposters:/imposters
ports:
- 2525:2525
- 3000:3000
#- 8090:8090
command: --configfile /imposters/imposter.json --allowInjection
networks:
- default
networks:
default:
volumes:
mysql_data:
我的冒名顶替者:
{ "port": 3000,
"protocol": "http",
"stubs": [
{
"predicates": [
{
"equals": {"path":"/ato/employee/a/balance"}
}
],
"responses": [
{
"is": {
"statusCode": 404,
"headers": {
"Content-Type": "application/json"
},
"body": {"error": "value not available"}
}
}
]
},
{
"predicates": [
{
"equals": {"path":"/ato/employee/58957fc7-4e24-44e5-9eb1-a7fa0297613b/balance"}
}
],
"responses": [
{
"is": {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": { "employeeId":"50ccf5d6-2056-4e0c-a160-4e51638410c7",
"superBalance":1050.0}
}
},
continues...
这是我的 docker 江湖骗子文件
FROM alpine:3.14
ENV MOUNTEBANK_VERSION=2.4.0
RUN apk add --update nodejs-lts && \
apk add --update npm
RUN npm install -g mountebank@${MOUNTEBANK_VERSION} --production
EXPOSE 2525
ENTRYPOINT ["mb"]
CMD ["start"]
如何更改我的代码才能使 test_api 收到江湖骗子的正确响应?
您应该更改 test_api;
中的以下行
var url = "http://localhost:3000/ato/employee/?/balance"
跟下面一个;
var url = "http://mountebank:3000/ato/employee/?/balance"
因为这些是不同的容器,您应该在 Docker 环境中指定它们的名称或 IP。你的 test_api 请求到它的本地主机并且没有 3000 的开放端口,你会得到连接被拒绝的错误。您可以查看 Docker Networking 了解更多信息。
我有一个 Go 应用程序和三个 docker 容器,用于应用程序、数据库和 mountebank 以 mock/stub HTTP 响应。
我希望我的江湖骗子容器在test_api(应用程序容器)发出请求时return做出适当的响应。
当我使用 Postman 调用端点时,mountebank 容器 return 是一个正确的响应。
但是,当我在 test_api 容器中的代码发送如下代码的 GET 请求时,它总是收到
dial tcp 127.0.0.1:3000: connect: connection refused
下面是test_api如何向江湖骗子容器发送请求
func getSuper(id string) (*float64, error) {
var url = "http://localhost:3000/ato/employee/?/balance"
url = strings.Replace(url, "?", id, 1)
log.Println("Sending request to this url: ", url)
resp, err := http.Get(url)
if err != nil {
return nil, &errorhandling.RequestError{Context: "getSuper calling ato api", Code: errorhandling.Internal, Message: err.Error()}
}
body, err := resposeToByte(resp)
if err != nil {
log.Println("err in coverting response to byte[]:", err)
return nil, &errorhandling.RequestError{Context: "getsuper resposeToByte", Code: errorhandling.Internal, Message: err.Error()}
}
superData, err := UnmarshalSuperDetails(body)
if err != nil {
return nil, &errorhandling.RequestError{Context: "UnmarshalSuperDetails())", Code: errorhandling.Internal, Message: err.Error()}
}
log.Println("super details ", superData)
return &superData.SuperBalance, nil
}
这是我的 docker-compose
version: '3.7'
services:
db:
container_name: "test_db"
platform: linux/x86_64
build:
context: .
dockerfile: db.Dockerfile
networks:
- default
restart: always
ports:
# <Port exposed> : < MySQL Port running inside container>
- "3306:3306"
# setting some env vars to create the DB
environment:
MYSQL_RANDOM_ROOT_PASSWORD: "secret"
MYSQL_DATABASE: "IGD"
MYSQL_USER: "tester"
MYSQL_PASSWORD: "secret"
# OR if you want to use "root" as the user, just these two lines
# MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
# MYSQL_DATABASE: ${DATABASE_NAME}
# we mount a data volume to make sure we don't lose data
volumes:
- mysql_data:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
api:
container_name: "test_api"
# we want to use the image which is build from our Dockerfile
build:
context: .
dockerfile: api.Dockerfile
ports:
- "8080:8080"
# we are depending on the mysql backend
depends_on:
- db
# We mount the working dir into the container, handy for development
# This is what makes the hot reloading work inside of a Docker container
volumes:
- .:/app/
mountebank:
container_name: mountebank
image: jkris/mountebank:latest
build:
context: .
dockerfile: mb.Dockerfile
volumes:
- ./imposters:/imposters
ports:
- 2525:2525
- 3000:3000
#- 8090:8090
command: --configfile /imposters/imposter.json --allowInjection
networks:
- default
networks:
default:
volumes:
mysql_data:
我的冒名顶替者:
{ "port": 3000,
"protocol": "http",
"stubs": [
{
"predicates": [
{
"equals": {"path":"/ato/employee/a/balance"}
}
],
"responses": [
{
"is": {
"statusCode": 404,
"headers": {
"Content-Type": "application/json"
},
"body": {"error": "value not available"}
}
}
]
},
{
"predicates": [
{
"equals": {"path":"/ato/employee/58957fc7-4e24-44e5-9eb1-a7fa0297613b/balance"}
}
],
"responses": [
{
"is": {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": { "employeeId":"50ccf5d6-2056-4e0c-a160-4e51638410c7",
"superBalance":1050.0}
}
},
continues...
这是我的 docker 江湖骗子文件
FROM alpine:3.14
ENV MOUNTEBANK_VERSION=2.4.0
RUN apk add --update nodejs-lts && \
apk add --update npm
RUN npm install -g mountebank@${MOUNTEBANK_VERSION} --production
EXPOSE 2525
ENTRYPOINT ["mb"]
CMD ["start"]
如何更改我的代码才能使 test_api 收到江湖骗子的正确响应?
您应该更改 test_api;
中的以下行var url = "http://localhost:3000/ato/employee/?/balance"
跟下面一个;
var url = "http://mountebank:3000/ato/employee/?/balance"
因为这些是不同的容器,您应该在 Docker 环境中指定它们的名称或 IP。你的 test_api 请求到它的本地主机并且没有 3000 的开放端口,你会得到连接被拒绝的错误。您可以查看 Docker Networking 了解更多信息。