{"id":1260,"date":"2022-10-06T07:43:52","date_gmt":"2022-10-06T07:43:52","guid":{"rendered":"https:\/\/mklasen.com\/?p=1260"},"modified":"2022-10-06T07:44:07","modified_gmt":"2022-10-06T07:44:07","slug":"make-a-service-available-on-a-different-port-in-docker-compose-with-traefik","status":"publish","type":"post","link":"https:\/\/mklasen.com\/make-a-service-available-on-a-different-port-in-docker-compose-with-traefik\/","title":{"rendered":"Make a service available on a different port in docker-compose with Traefik"},"content":{"rendered":"\n
This is one of these things that I broke my head around a couple of times. And every time I need to do this, it takes me a fair amount of trial and error before solving it. Let’s get that over once and for all.<\/strong><\/p>\n\n\n\n (seriously, i’ll probably Google for this issue in about 6 months and find my own blog article)<\/em><\/p>\n\n\n\n Alright, so in this specific situation, I want to make my mailhog service available with a custom domain, that way, I can just type ‘mail.c7’ in the address bar and see e-mails of all my docker containers coming in. Great for testing purposes!<\/p>\n\n\n\n There’s a couple of rules we need to set for this, but let’s first have a look a the full docker-compose file:<\/p>\n\n\n\n You can probably guess which rules are of importance here, it’s these ones:<\/p>\n\n\n\n But, most importantly, it’s the loadbalancer one. So, Mailhog makes the web interface available via 8025. By setting the loadbalancer rule and defining port 8025, we make the service available for http\/https traffic.<\/p>\n\n\n\n Mind that it’s important to use the same name for the routers and services, we’re using mail<\/strong> in the example above.<\/p>\n\n\n\n The blog wouldn’t be complete with an example of the main Traefik docker-compose file, you’ll find it below:<\/p>\n\n\n\n Alright, hope that makes sense!<\/p>\n\n\n\n Happy developing!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":" This is one of these things that I broke my head around a couple of times. And every time I need to do this, it takes me a fair amount of trial and error before solving it. Let’s get that over once and for all. (seriously, i’ll probably Google for this issue in about 6 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[1],"tags":[],"acf":[],"yoast_head":"\nversion: \"3\"\n\nservices:\n catchall:\n container_name: catchall\n image: mailhog\/mailhog:latest\n user: \"1000:1000\"\n logging:\n driver: 'none' # disable saving logs\n expose:\n - 8025\n - 1025\n labels:\n - \"traefik.enable=true\"\n - \"traefik.docker.network=traefik_proxy\"\n - \"traefik.http.routers.mail.rule=Host(`mail.c7`)\"\n - \"traefik.http.routers.mail.tls=true\"\n - \"traefik.http.services.mail.loadbalancer.server.port=8025\"\n restart: always\n\nnetworks:\n default:\n external:\n name: traefik_proxy<\/code><\/pre>\n\n\n\n
- \"traefik.enable=true\"\n - \"traefik.docker.network=traefik_proxy\"\n - \"traefik.http.routers.mail.rule=Host(`mail.c7`)\"\n - \"traefik.http.routers.mail.tls=true\"\n - \"traefik.http.services.mail.loadbalancer.server.port=8025\"<\/code><\/pre>\n\n\n\n
The Traefik docker-compose file<\/h2>\n\n\n\n
version: \"3.3\"\n\nservices:\n\n traefik:\n image: \"traefik:v2.8\"\n container_name: \"traefik\"\n restart: always\n command:\n #- \"--log.level=DEBUG\"\n - \"--api.insecure=true\"\n - \"--providers.docker=true\"\n - \"--providers.docker.exposedbydefault=false\"\n - '--providers.docker.watch=true'\n - \"--providers.file.directory=\/etc\/traefik\"\n - \"--entrypoints.websecure.address=:443\"\n - \"--entrypoints.web.address=:80\"\n - \"--entrypoints.web.http.redirections.entryPoint.to=websecure\"\n - \"--entrypoints.web.http.redirections.entryPoint.scheme=https\"\n - \"--entrypoints.web.http.redirections.entrypoint.permanent=true\"\n ports:\n - \"80:80\"\n - \"443:443\"\n - \"8080:8080\"\n volumes:\n - \"\/var\/run\/docker.sock:\/var\/run\/docker.sock:ro\"\n - \".\/certs:\/etc\/certs:ro\"\n - \".\/config\/:\/etc\/traefik\/\"\n networks:\n default:\n ipv4_address: 172.24.0.19\n\nnetworks:\n default:\n external:\n name: traefik_proxy<\/code><\/pre>\n\n\n\n