Exposing docker container using Dockerfile

i am trying to expose my docker container to a public url (eg https://example.com/dashboard) using traefik but i am not sure why my url is hitting a 404 not found error. i am using a Dockerfile to create my docker image, and most of the stuff online is only for docker-compose files, so i am having much difficulties finding help is this area.

here is my traefik.toml

################################################################
# Global configuration
################################################################
[global]
  checkNewVersion = false
  sendAnonymousUsage = false

################################################################
# Entrypoints configuration
################################################################

# Entrypoints definition
[entryPoints]

# Dashboard and API
  [entryPoints.traefik]
    address = ":8080"
    [entryPoints.traefik.http.tls]

  [entryPoints.ping]
    address = ":8082"

  [entryPoints.web-secure]
    address = ":443"

    # TLS is enabled by having this empty option
    [entryPoints.web-secure.http.tls]

################################################################
# API and dashboard configuration
################################################################

# Enable API and dashboard
[api]
  dashboard = true
  debug = true

################################################################
# File-Based Configuration Backend
################################################################

[providers]
  [providers.file]
    directory = "/config"
    watch =  true

and here is my Dockerfile

FROM docker.odyssey.rowini.net/traefik:2.10.1
COPY traefik.toml /traefik.toml
COPY tls.toml /config/tls.toml
COPY certs /certs
EXPOSE 8080 443 8082
LABEL traefik.enable="true"
LABEL traefik.http.routers.example.rule="Host(`example.com`)"
LABEL traefik.http.routers.example.entrypoints="web-secure"
LABEL traefik.http.routers.example.service="api@internal"
LABEL traefik.http.services.unodux-service.loadbalancer.server.port="443"
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

and this is the config file for the /dashboard route

[http.routers]
  [http.routers.traefik]
    rule = "Host(`example.com`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))"
    middlewares = [] 
    service = "api@internal"
    entryPoints = ["web-secure"]
    [http.routers.traefik.tls]
      options = "default"

im very new to traefik, so i am not very sure how to use traefik on the docker container using Dockerfiles, and not very sure if i am configuring it correctly as well. any help or some explanation would be greatly appreciated

The trailing slash / in /dashboard/ is mandatory

Doc

even with the trailing slash https://example.com/dashboard/ still returns a 404 not found error. is my Dockerfile and traefik.toml config correct? i feel like i might be missing something trivial here

If you don't use default ports 80 (http) or 443 (https), than you should put the port into the URL.

You can compare your config to simple Traefik example.

i have edited my Dockerfile to match the labels on the docker-compose in the link you have sent, and have also changed the web-secure (for https traffic) entrypoint to the listen to the default port 443. however, i am still hitting a 404 not found error when i try to run the same link

i have tried all possible combinations and have no idea how else to proceed from here

Hello,

A Dockerfile is not a good way to deploy traefik or any container: use docker-compose or docker CLI.

By using a Dockerfile you will create an image that embeds unwanted things (passwords, certificates, configuration, etc.)

The LABELs on a Dockerfile are mainly for the description of the image.
Also, you will need to rebuild the image every time you will want to update the configuration.

Why do you want to use a Dockerfile?

hi i am using a Dockerfile as i would like to run some shell commands while the docker container is being created

for the labelling of the container, is the traefik labels necessary for the docker container to be exposed to a external url? is there a way to expose the container without the labels in docker-compose since there are no Dockerfile equivalent for the labels field?

I think you need to look into the Docker basics again. A Dockerfile is used to build a container image.

You still need to use "docker run", "docker compose up" or "docker stack deploy" to run a container image, there you can attach labels.

Alternatively you can use a Traefik dynamic config file to define a router and service with loadbalancer.servers.url. (Doc)