Search code examples
dockerdockerfilekubernetes-helm

How to bind docker container ports to the host using helm charts


I am writing a simple docker file for a golang and I'm still getting familiar with docker so I know what I want to do just don't know how to do it:

What I have right now (below) is exposing port 8080, but I want to expose port 80 but forward that to port 8080.

I know that I can do it via docker run -p but I'm wondering if there's a way I can set it up in Dockerfile or something else. I'm trying to find how I can do that through Helm.

Dockerfile:

FROM scratch

COPY auth-service /auth-service

EXPOSE 8080

CMD ["/auth-service","-logtostderr=true", "-v=-1"]

Solution

  • EXPOSE informs Docker that the container listens on the specified network ports at runtime but does not actually make ports accessible. only -p as you already mentioned will do that:

    docker run -p :$HOSTPORT:$CONTAINERPORT

    Or you can opt for a docker-compose file, extra file but also do the thing for you:

    version: "2"
    services:
      my_service:
        build: .
        name: my_container_name
        ports:
          - 80:8080
        .....
    

    Edit:

    If you are using helm you have just to use the exposed docker port as your targetPort :

    apiVersion: v1
    kind: Service
    metadata:
      name: {{ template "fullname" . }}
      labels:
        chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    spec:
      type: {{ .Values.service.type }}
      ports:
      - port: {{ .Values.service.externalPort }}
        targetPort: {{ .Values.service.internalPort }} #8080
        protocol: TCP
        name: {{ .Values.service.name }}
      selector:
        app: {{ template "fullname" . }}