Docker Compose Cheatsheet

Docker Compose

...

Networking

By default Docker-Compose will create a new network for the given compose file. You can change the behavior by defining custom networks in your compose file.

Create and assign custom network

... Example:

1networks:
2  custom-network:
3
4services:
5  app:
6    networks:
7      - custom-network

Use existing networks

If you want to use an existing Docker network for your compose files, you can add the external: true parameter in your compose file Example:

1networks:
2  existing-network:
3    external: true

Volumes

Volumes allow Docker containers to use persistent storage. In a compose file, you can create and map volumes like this:

1volumes:
2  my-volume:
3
4services:
5  app:
6    volumes:
7      - my-volume:/path-in-container

These volumes are stored in /var/lib/docker/volumes.