Getting net to work inside docker-compose?

I am trying to create a nginx reverse proxy, but when do

docker network create nettest
docker-compose up -d
docker run --rm -it --net=nettest alpine ping -c 1 landing

I get

ping: bad address 'landing'

where this is my docker-compose.yml

landing:
  image: nginx
  expose:
    - "80"
  net: nettest

proxy:
  image: nginx
  ports:
    - "80:80"
  net: nettest

Questions

  • Is it possible to have the network create in the docker-compose file?
  • Why can’t I access landing?

Try using version 2 of the Docker Compose file. You will also need to be on Docker 1.10 and Docker Compose 1.6 for this to work.

version: '2'
networks:
  nettest:
    external: true
services:
  landing:
    image: nginx
    expose:
      - "80"
    networks:
      - nettest

  proxy:
    image: nginx
    ports:
      - "80:80"
    networks:
      - nettest

This expects that the nettest network be created beforehand, and assigns the containers to it when they start. You can also exclude the “external: true” line which will make Docker Compose generate the network for you automatically, but keep in mind when it does that it will prepend the name of the project in front of it (i.e. “pwd_nettest”)

1 Like