Home > Enterprise >  Can I set up a docker-compose configuration with no networks?
Can I set up a docker-compose configuration with no networks?

Time:12-23

I use Docker Compose to configure some Dockerised batch jobs. These jobs pull some data from external databases and then save data back to other databases. The jobs never interact directly with each other. They do not expose any ports or run any servers.

I would like to switch off Docker networking.

By default, docker-compose creates a network for each configuration. Is there a way to supress this so that compose starts running the container without bothering to create any network?

CodePudding user response:

You can tell docker-compose to use the host network for the containers. You define it for each container like this

services:
  myapp:
    image: asdf
    network_mode: host

Of course that means that any ports that the container uses need to be available on the host, since you can't do any port mapping.

Normally, the networks created are a nice thing and something you want to do. You may not realise it, but running a single container with docker run also creates a network, unless you use host network mode.

CodePudding user response:

If you bothering the sole create/destroy process of the network you can create a network once and use it every time you spin the compose.

networks:
  default:
    external: true
    name: whatevernameyougaveit

In addition there is a docker built-in network called None, which disables networks completely for a container joining that. Maybe you already have seen it with docker network ls along with host

services:
  service:
   network_mode: none

If all services using a network_mode host or none, compose will suppress the creation of the default network..

Edit: One (including me) might have the idea to implement it like that to get rid of the network_mode setting for each service:

networks:
  default:
    external: true
    name: none

But then compose thinks it is actually a network and tries to set the network aliases witch leads to an error: network-scoped alias is supported only for containers in user defined networks. Same applies if you try to add the other pre-configured networks like that.

  • Related