Shajia
Shajia

Reputation: 13

Networking in Docker Compose file

I am writing a docker compose file for my web app.If I use 'link' to connect services with each other do I also need to include 'port'? And is 'depends on' an alternate option of 'links'? What will be best for connection services in a compose file with one another?

Upvotes: 1

Views: 462

Answers (1)

David Maze
David Maze

Reputation: 158705

The core setup for this is described in Networking in Compose. If you do absolutely nothing, then one service can call another using its name in the docker-compose.yml file as a host name, using the port the process inside the container is listening on.

Up to startup-order issues, here's a minimal docker-compose.yml that demonstrates this:

version: '3'
services:
  server:
    image: nginx
  client:
    image: busybox
    command: wget -O- http://server/
    # Hack to make the example actually work:
    # command: sh -c 'sleep 1; wget -O- http://server/'

You shouldn't use links: at all. It was an important part of first-generation Docker networking, but it's not useful on modern Docker. (Similarly, there's no reason to put expose: in a Docker Compose file.)

You always connect to the port the process inside the container is running on. ports: are optional; if you have ports:, cross-container calls always connect to the second port number and the remapping doesn't have any effect. In the example above, the client container always connects to the default HTTP port 80, even if you add ports: ['12345:80'] to the server container to make it externally accessible on a different port.

depends_on: affects two things. Try adding depends_on: [server] to the client container to the example. If you look at the "Starting..." messages that Compose prints out when it starts, this will force server to start starting before client starts starting, but this is not a guarantee that server is up and running and ready to serve requests (this is a very common problem with database containers). If you start only part of the stack with docker-compose up client, this also causes server to start with it.

A more complete typical example might look like:

version: '3'
services:
  server:
    # The Dockerfile COPYs static content into the image
    build: ./server-based-on-nginx
    ports:
      - '12345:80'
  client:
    # The Dockerfile installs
    # https://github.com/vishnubob/wait-for-it
    build: ./client-based-on-busybox
    # ENTRYPOINT and CMD will usually be in the Dockerfile
    entrypoint: wait-for-it.sh server:80 --
    command: wget -O- http://server/
    depends_on:
      - server

SO questions in this space seem to have a number of other unnecessary options. container_name: explicitly sets the name of the container for non-Compose docker commands, rather than letting Compose choose it, and it provides an alternate name for networking purposes, but you don't really need it. hostname: affects the container's internal host name (what you might see in a shell prompt for example) but it has no effect on other containers. You can manually create networks:, but Compose provides a default network for you and there's no reason to not use it.

Upvotes: 2

Related Questions