Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

259
Visualizações
Docker Compose: Mock external services

I have the following situation:

My application consists of a single web service that calls an external API (say, some SaaS service, ElasticSearch or so). For non-unit-testing purposes we want to control the external service and later also inject faults. The application and the "mocked" API are dockerized and now I want to use docker-compose to spin all containers up.

Because the application has several addresses hardcoded (e.g. the hostname of external services) I cannot change them and need to work around.

The service container makes a call to http://external-service.com/getsomestuff.

My idea was to use some features that are provided by docker to reroute all outgoing traffic to the external http://external-service.com/getsomestuff to the mock container without changing the URL.

My docker-compose.yaml looks like:

version: '2'
services:
  service:
    build: ./service
    container_name: my-service1
    ports:
      - "5000:5000"
    command: /bin/sh -c "python3 app.py"

  api:
    build: ./api-mock
    container_name: my-api-mock
    ports:
      - "5001:5000"
    command: /bin/sh -c "python3 app.py"

Finally, I have a driver that just does the following:

curl -XGET localhost:5000/
curl -XPUT localhost:5001/configure?delay=10
curl -XGET localhost:5000/

where the second curl just sets the delay in the mock to 10 seconds.

There are several options I have considered:

  • Using iptables-fu (would require modifying Dockerfiles to install it)
  • Using docker networks (this is really unclear to me)

Is there any simple option to achieve what I want?

Edit:

For clarity, here is the relevant part of the service code:

import requests

@app.route('/')
def do_stuff():
    r = requests.get('http://external-service.com/getsomestuff')
    return process_api_response(r.text())
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Docker runs an internal DNS server for user defined networks. Any unknown host lookups are forwarded to you normal DNS servers.

Version 2+ compose files will automatically create a network for compose to use so there's a number of ways to control the hostnames it resolves.

The simplest way is to name your container with the hostname:

version: "2"
services:
  external-service.com:
    image: busybox
    command: sleep 100
  ping:
    image: busybox
    command: ping external-service.com
    depends_on:
      - external-service.com

If you want to keep container names you can use links

version: "2"
services:
  api:
    image: busybox
    command: sleep 100
  ping:
    image: busybox
    links:
      - api:external-service.com
    command: ping external-service.com
    depends_on:
      - api

Or network aliases

version: "2"
services:
  api:
    image: busybox
    command: sleep 100
    networks:
      pingnet:
        aliases:
        - external-service.com
  ping:
    image: busybox
    command: ping external-service.com
    depends_on:
      - api
    networks:
      - pingnet
networks:
  pingnet:
about 4 years ago · Santiago Trujillo Relatório

0

I was able to solve this with -links, is there a way to do networks in docker-compose?

version: '3'

services:

MOCK:
 image: api-mock:latest
 container_name: api-mock-container
 ports:
    - "8081:80"

api: 
 image: my-service1:latest
 links:
   - MOCK:external-service.com
about 4 years ago · Santiago Trujillo Relatório

0

I'm not entirely clear what the problem is you're trying to solve, but if you're trying to make external-service.com inside the container direct traffic to your "mock" service, I think you should be able to do that using the extra_hosts directive in your docker-compose.yml file. For example, if I have this:

version: "2"

services:
  example:
    image: myimage
    extra_hosts:
      - google.com:172.23.254.1

That will result in /etc/hosts in the container containing:

172.23.254.1    google.com

And attempts to access http://google.com will hit my web server at 172.23.254.1.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda