{"id":11684,"date":"2022-09-21T12:30:44","date_gmt":"2022-09-21T12:30:44","guid":{"rendered":"https:\/\/predictly.se\/traefik-in-docker-compose\/"},"modified":"2024-05-08T11:46:42","modified_gmt":"2024-05-08T11:46:42","slug":"traefik-in-docker-compose","status":"publish","type":"post","link":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/","title":{"rendered":"Traefik in Docker Compose"},"content":{"rendered":"<p>In my current project we use Kubernetes with ingress and services using the same hostname but different paths. Testing locally we ran into difficulties of testing the full setup. Since we already use docker compose in general we watned to get a quick start this way, rather than starting to use Kubernetes or Minikube.<\/p>\n<p>For simplicity our structure looks like this:<\/p>\n<ul>\n<li><strong>Frontend javascript service<\/strong> being served on root path \/<br \/>\nExample: https:\/\/our-application.company.com\/<\/li>\n<li><strong>Backend service<\/strong> being served on the same hostname and the path \/api where the \/api is not part of the backend service itself so requests routed to \/api is served by the backend at \/.<br \/>\nExample: https:\/\/our-application.company.com\/api\/<\/li>\n<\/ul>\n<p>Snippet below is our ingress helm configuration for our backend using the <a href=\"https:\/\/kubernetes.github.io\/ingress-nginx\/\" target=\"_blank\" rel=\"noopener\">nginx-ingress<\/a> controller in our Kubernetes environment.<\/p>\n<p>The snippet is more to get an idea, than to provide working code.<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\ningress:\n  enabled: true\n  annotations:\n  nginx.ingress.kubernetes.io\/rewrite-target: \/$2\n  hosts:\n  - host: &lt;HOST_URL&gt;\n  paths:\n  - \/api(\/|$)(.*)\n<\/pre>\n<p>To solve this we used the <a href=\"https:\/\/github.com\/containous\/traefik-library-image\" target=\"_blank\" rel=\"noopener\">traefik docker image<\/a> in our compose file, which lead me to write this article since I couldn&#8217;t find a working example in one place.<\/p>\n<p>The docker compose file to showcase the use of traefik would be this in its condensed form with just two <a href=\"https:\/\/hub.docker.com\/r\/nginxdemos\/hello\/\" target=\"_blank\" rel=\"noopener\">nginx demos<\/a> docker images.<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\nversion: \"3.3\"\nservices:\n  # Traefik to mimic the kubernetes ingress\n  traefik:\n  image: \"traefik:v2.4\"\n  container_name: \"traefik\"\n  command:\n  # - \"--log.level=DEBUG\"\n  - \"--api.insecure=true\"\n  - \"--providers.docker=true\"\n  - \"--providers.docker.exposedbydefault=false\"\n  - \"--entrypoints.web.address=:80\"\n  ports:\n  - \"80:80\"\n  volumes:\n  - \"\/var\/run\/docker.sock:\/var\/run\/docker.sock:ro\"\n  front end:\n  image: nginxdemos\/hello\n  container_name: \"frontend\"\n  labels:\n  - \"traefik.enable=true\"\n  - \"traefik.http.routers.frontend.rule=PathPrefix(`\/`)\"\n  backend:\n  image: nginxdemos\/hello\n  container_name: \"backend\"\n  labels:\n  - \"traefik.enable=true\"\n  - \"traefik.http.routers.backend.rule=PathPrefix(`\/api\/`)\"\n  - \"traefik.http.middlewares.backend.stripprefix.prefixes=\/api\"\n  - \"traefik.http.routers.backend.middlewares=backend\"\n<\/pre>\n<p>After starting this simple docker compose environment you will have two different nginx responding to either:<\/p>\n<ul>\n<li>http:\/\/localhost\/<\/li>\n<li>http:\/\/localhost\/api\/<\/li>\n<\/ul>\n<p>Both respond with the path \/ but different server id&#8217;s.<\/p>\n<h6>Challenges<\/h6>\n<p>Two things that was a little bit troublesome getting correct was:<\/p>\n<ol>\n<li>The trailing slash on the path prefix needing to be there.<\/li>\n<li>Including the last line in compose above with the middlewares configuration to include stripprefix configuration.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>An easy way to cut costs and increase performance in Data Warehouses &#8211; learn how to implement Partitioning with Dataform!<\/p>\n","protected":false},"author":9,"featured_media":11455,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"cybocfi_hide_featured_image":"","footnotes":""},"categories":[102,124,108,107,125],"tags":[],"class_list":["post-11684","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-delivery","category-docker-en","category-helm-en","category-kubernetes-en","category-traefik-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Traefik in Docker Compose &#8211; Predictly<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Traefik in Docker Compose &#8211; Predictly\" \/>\n<meta property=\"og:description\" content=\"An easy way to cut costs and increase performance in Data Warehouses - learn how to implement Partitioning with Dataform!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/\" \/>\n<meta property=\"og:site_name\" content=\"Predictly\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/predictly.se\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-21T12:30:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-08T11:46:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/predictly.se\/wp-content\/uploads\/2022\/09\/traffic.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1429\" \/>\n\t<meta property=\"og:image:height\" content=\"888\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Peter Larsson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@predictly_se\" \/>\n<meta name=\"twitter:site\" content=\"@predictly_se\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Peter Larsson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/\"},\"author\":{\"name\":\"Peter Larsson\",\"@id\":\"https:\/\/predictly.se\/en\/#\/schema\/person\/d8507a6150cd55bdade9201e2e9ac21b\"},\"headline\":\"Traefik in Docker Compose\",\"datePublished\":\"2022-09-21T12:30:44+00:00\",\"dateModified\":\"2024-05-08T11:46:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/\"},\"wordCount\":401,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/predictly.se\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/predictly.se\/wp-content\/uploads\/2022\/09\/traffic.jpg\",\"articleSection\":[\"Delivery\",\"Docker\",\"Helm\",\"Kubernetes\",\"Traefik\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/\",\"url\":\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/\",\"name\":\"Traefik in Docker Compose &#8211; Predictly\",\"isPartOf\":{\"@id\":\"https:\/\/predictly.se\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/predictly.se\/wp-content\/uploads\/2022\/09\/traffic.jpg\",\"datePublished\":\"2022-09-21T12:30:44+00:00\",\"dateModified\":\"2024-05-08T11:46:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#primaryimage\",\"url\":\"https:\/\/predictly.se\/wp-content\/uploads\/2022\/09\/traffic.jpg\",\"contentUrl\":\"https:\/\/predictly.se\/wp-content\/uploads\/2022\/09\/traffic.jpg\",\"width\":1429,\"height\":888},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/predictly.se\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Delivery\",\"item\":\"https:\/\/predictly.se\/en\/insikter\/delivery\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Traefik in Docker Compose\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/predictly.se\/en\/#website\",\"url\":\"https:\/\/predictly.se\/en\/\",\"name\":\"Predictly\",\"description\":\"Professional IT services\",\"publisher\":{\"@id\":\"https:\/\/predictly.se\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/predictly.se\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/predictly.se\/en\/#organization\",\"name\":\"Predictly\",\"url\":\"https:\/\/predictly.se\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/predictly.se\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/predictly.se\/wp-content\/uploads\/2022\/10\/Logotype1-mobil.svg\",\"contentUrl\":\"https:\/\/predictly.se\/wp-content\/uploads\/2022\/10\/Logotype1-mobil.svg\",\"width\":532,\"height\":96,\"caption\":\"Predictly\"},\"image\":{\"@id\":\"https:\/\/predictly.se\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/predictly.se\",\"https:\/\/x.com\/predictly_se\",\"https:\/\/www.linkedin.com\/company\/predictly\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/predictly.se\/en\/#\/schema\/person\/d8507a6150cd55bdade9201e2e9ac21b\",\"name\":\"Peter Larsson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/predictly.se\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e2e4fd5ab3fe625392983e1350bf6c6219685af385fd50af327f0676897ec58b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e2e4fd5ab3fe625392983e1350bf6c6219685af385fd50af327f0676897ec58b?s=96&d=mm&r=g\",\"caption\":\"Peter Larsson\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Traefik in Docker Compose &#8211; Predictly","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/","og_locale":"en_US","og_type":"article","og_title":"Traefik in Docker Compose &#8211; Predictly","og_description":"An easy way to cut costs and increase performance in Data Warehouses - learn how to implement Partitioning with Dataform!","og_url":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/","og_site_name":"Predictly","article_publisher":"https:\/\/www.facebook.com\/predictly.se","article_published_time":"2022-09-21T12:30:44+00:00","article_modified_time":"2024-05-08T11:46:42+00:00","og_image":[{"width":1429,"height":888,"url":"https:\/\/predictly.se\/wp-content\/uploads\/2022\/09\/traffic.jpg","type":"image\/jpeg"}],"author":"Peter Larsson","twitter_card":"summary_large_image","twitter_creator":"@predictly_se","twitter_site":"@predictly_se","twitter_misc":{"Written by":"Peter Larsson","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#article","isPartOf":{"@id":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/"},"author":{"name":"Peter Larsson","@id":"https:\/\/predictly.se\/en\/#\/schema\/person\/d8507a6150cd55bdade9201e2e9ac21b"},"headline":"Traefik in Docker Compose","datePublished":"2022-09-21T12:30:44+00:00","dateModified":"2024-05-08T11:46:42+00:00","mainEntityOfPage":{"@id":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/"},"wordCount":401,"commentCount":0,"publisher":{"@id":"https:\/\/predictly.se\/en\/#organization"},"image":{"@id":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#primaryimage"},"thumbnailUrl":"https:\/\/predictly.se\/wp-content\/uploads\/2022\/09\/traffic.jpg","articleSection":["Delivery","Docker","Helm","Kubernetes","Traefik"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/","url":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/","name":"Traefik in Docker Compose &#8211; Predictly","isPartOf":{"@id":"https:\/\/predictly.se\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#primaryimage"},"image":{"@id":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#primaryimage"},"thumbnailUrl":"https:\/\/predictly.se\/wp-content\/uploads\/2022\/09\/traffic.jpg","datePublished":"2022-09-21T12:30:44+00:00","dateModified":"2024-05-08T11:46:42+00:00","breadcrumb":{"@id":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/predictly.se\/en\/traefik-in-docker-compose\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#primaryimage","url":"https:\/\/predictly.se\/wp-content\/uploads\/2022\/09\/traffic.jpg","contentUrl":"https:\/\/predictly.se\/wp-content\/uploads\/2022\/09\/traffic.jpg","width":1429,"height":888},{"@type":"BreadcrumbList","@id":"https:\/\/predictly.se\/en\/traefik-in-docker-compose\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/predictly.se\/en\/"},{"@type":"ListItem","position":2,"name":"Delivery","item":"https:\/\/predictly.se\/en\/insikter\/delivery\/"},{"@type":"ListItem","position":3,"name":"Traefik in Docker Compose"}]},{"@type":"WebSite","@id":"https:\/\/predictly.se\/en\/#website","url":"https:\/\/predictly.se\/en\/","name":"Predictly","description":"Professional IT services","publisher":{"@id":"https:\/\/predictly.se\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/predictly.se\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/predictly.se\/en\/#organization","name":"Predictly","url":"https:\/\/predictly.se\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/predictly.se\/en\/#\/schema\/logo\/image\/","url":"https:\/\/predictly.se\/wp-content\/uploads\/2022\/10\/Logotype1-mobil.svg","contentUrl":"https:\/\/predictly.se\/wp-content\/uploads\/2022\/10\/Logotype1-mobil.svg","width":532,"height":96,"caption":"Predictly"},"image":{"@id":"https:\/\/predictly.se\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/predictly.se","https:\/\/x.com\/predictly_se","https:\/\/www.linkedin.com\/company\/predictly\/"]},{"@type":"Person","@id":"https:\/\/predictly.se\/en\/#\/schema\/person\/d8507a6150cd55bdade9201e2e9ac21b","name":"Peter Larsson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/predictly.se\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e2e4fd5ab3fe625392983e1350bf6c6219685af385fd50af327f0676897ec58b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e2e4fd5ab3fe625392983e1350bf6c6219685af385fd50af327f0676897ec58b?s=96&d=mm&r=g","caption":"Peter Larsson"}}]}},"_links":{"self":[{"href":"https:\/\/predictly.se\/en\/wp-json\/wp\/v2\/posts\/11684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/predictly.se\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/predictly.se\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/predictly.se\/en\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/predictly.se\/en\/wp-json\/wp\/v2\/comments?post=11684"}],"version-history":[{"count":1,"href":"https:\/\/predictly.se\/en\/wp-json\/wp\/v2\/posts\/11684\/revisions"}],"predecessor-version":[{"id":11696,"href":"https:\/\/predictly.se\/en\/wp-json\/wp\/v2\/posts\/11684\/revisions\/11696"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/predictly.se\/en\/wp-json\/wp\/v2\/media\/11455"}],"wp:attachment":[{"href":"https:\/\/predictly.se\/en\/wp-json\/wp\/v2\/media?parent=11684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/predictly.se\/en\/wp-json\/wp\/v2\/categories?post=11684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/predictly.se\/en\/wp-json\/wp\/v2\/tags?post=11684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}