0

Docker beginner here. I have 3 services (webapp, database, and redis) that need to share the same network.

When I build my container using Docker compose (via VS Code remote containers), sometimes everything's fine. Sometimes, the redis service is not accessible (connecting to 127.0.0.1:6379. Connection refused). And even sometimes both the database and redis are not accessible.

Running ss on debian, I can see the 6379 port is not open. enter image description here

I used the network_mode: service:[foo] strategy, as you can see below in my docker-compose.yml file:

version: "3"

services:
  app:
    build:
      context: ..
      dockerfile: .devcontainer/service_app/Dockerfile

    volumes:
      - ..:/workspace:delegated

    # Overrides default command so things don't shut down after the process ends.
    command: sleep infinity

  db:
    network_mode: service:app
    env_file: service_db/.env.local
    build:
      context: ..
      dockerfile: .devcontainer/service_db/Dockerfile
    restart: unless-stopped
    volumes:
      - "pgdata:/var/lib/postgresql/data"

  rd:
    network_mode: service:app
    image: redis:7
    restart: unless-stopped
    volumes:
      - "rddata:/data"

volumes:
  pgdata:
  rddata:

Note that I tried another strategy which was to not use network_mode and reach my service using rd:6379 rather than 127.0.0.1:6379. It worked but curiously, it hanged 3 seconds before establishing the connection, which is painful.

What did I do wrong? Thanks.

David Dahan
  • 10,576
  • 11
  • 64
  • 137
  • `network_mode: service` is very unusual and I'd recommend the last approach you describe, deleting `network_mode:` and configuring the service to use the Compose service name `rd` as a host name. That shouldn't take multiple seconds to connect, though. – David Maze Jul 04 '22 at 10:47
  • @DavidMaze can you elaborate about why is it unusual/bad? I followed the recommandation here: https://code.visualstudio.com/docs/remote/create-dev-container#_using-localhost-in-docker-compose – David Dahan Jul 04 '22 at 10:52
  • @DavidDahan according to the descr, this network mode is used to expose another container on the localhost of the container, this is not a usual practice, unless you really need this. I recommend trying with a default network mode – vladtkachuk Jul 04 '22 at 11:29

1 Answers1

1

Looks like starting order problem.

app is depending on both rd and db. So you have to start rd and db first and only after that app may be started. You may add dependencies to keep correct order.

Good way:

version: "3"

services:
  app:
    depends_on:
      - db
      - rd
    ...

  db:
    ...

  rd:
    ...

In this case app will be started last.

But! It is still not perfect. After starting db and and rd docker don't wait while internal processes will be ready for requests. So best way is to wait while rd and db will be healthy:

version: "3"

services:
  app:
    depends_on:
      db:
        condition: service_healthy
      rd:
        condition: service_healthy
    ...

  db:
    healthcheck:
      test: <command to check if service healthy>
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s
    ...

  rd:
    healthcheck:
      test: <command to check if service healthy>
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s
    ...

If your app starting longer than db and rd first way should work. But if you not sure about start time implementing of healthcheck will make your infrastructure more stable.

Some examples how to implement healthcheck with redis and... I don't know what DB is used, so try to google by yourself

rzlvmp
  • 7,512
  • 5
  • 16
  • 45
  • Thanks. I should have specify that I use Docker for local development only, so healthcheck is not necessary: since I start commands by hand, my 3 containers will be ready at that time. For the initial problem, as I mentioned at the end of my first post, removing `network_mode` works, but on a random basis, curiously, connecting to the server hangs 3 to 5 seconds before establishing the connection, which is painful. Maybe I should do another post with that specific problem. – David Dahan Jul 04 '22 at 11:48
  • Yep. Looks like that is not docker problem. That will be better to specify 1. is `hangs 3 to 5 seconds` happens only with redis or both redis and db has this problem? 2. I can see `restart: unless-stopped`, so it is possible that redis everytime restarting and you need to wait 3 seconds before it start handle request and fail again. 3. You mentioned that `6379 port is not open`. What happening with container in this time (`docker ps -a | grep rd`)? – rzlvmp Jul 04 '22 at 11:59
  • I wrote another question to avoid mixing topics together: https://stackoverflow.com/questions/72856901/network-hanging-5-seconds-with-vs-code-remote-docker-containers Thanks! – David Dahan Jul 04 '22 at 12:38