I am sure it’s not a new discovery, but it was new to me and considering I had whined about it in the past I thought I’d set the record straight. In a previous post, while discovering Docker, I had complained about Docker compose files and how I’d rather just use the docker run
command to setup things (hmm, not sure if I actually said the latter or it was more of an implied thing). Since then I had created a bunch of shell scripts for each of the containers I was impelementing as a way of quickly recreating them with the correct volumes, IP addresses, etc. I had single containers anyways and nothing complex that might require orchestration and Docker Compose.
Last week however I chanced upon a video between Scott Hanselman and Chad Metcalf wherein Chad casually mentioned that he uses Docker Compose, even though he has a single container, as that way he can capture all the settings etc. of the container. That was a light-bulb moment for me. Yes, that makes way better sense than having shell scripts to create containers… so since then I have been re-visiting Docker Compose. I made the following for my kea-knot container:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
version: "3.8" services: kea-knot: image: rakheshster/kea-knot:1.8.0-2.9.5-3 container_name: kea-knot networks: my_macvlan_network: ipv4_address: 192.168.17.21 volumes: - "kea-knot_knotconfig:/etc/knot" - "kea-knot_knotzones:/var/lib/knot/zones" - "kea-knot_keaconfig:/etc/kea" - "kea-knot_kealeases:/var/lib/kea" environment: - "TZ=Europe/London" restart: unless-stopped cap_add: - NET_ADMIN networks: # The network is created outside of Docker Compose and don't attempt to create it my_macvlan_network: external: true name: my_macvlan_network # I specify the volume name again via "name" so that compose creates the volume with that name # If I don't do this the volume name before <projectname or foldername>-<the volume name below>. Example: docker-kea-knot_kea-knot_knotconfig volumes: kea-knot_knotconfig: name: kea-knot_knotconfig kea-knot_knotzones: name: kea-knot_knotzones kea-knot_keaconfig: name: kea-knot_keaconfig kea-knot_kealeases: name: kea-knot_kealeases |
Nothing fancy but I found the solution to one of my previous issues – that of networks and volumes having the project name prefixed to it automatically, along with how to deal with macvlan networks correctly (version 3.x of Compose does not support specifiying gateway & IP range when you define the network) – through a bit of Googling today. Turns out since I create the macvlan network separately anyways, I can just define it as an external
network and thus Docker Compose won’t bother with it. And to prevent Docker Compose from prefixing the project or folder name for volumes, I can add a name
property.
I think I will start using Docker Compose files a bit more over shell scripts.