Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"]
like image 706
Naguib Ihab Avatar asked Mar 08 '18 02:03

Naguib Ihab


People also ask

How do I map a container port to a host port?

To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.

How do I connect a Docker container to the outside of the host?

By default docker containers works in a isolated network. But if you want to connect to your container outside from host machine, you have to expose your container. Means you have to apply NAT/PAT concept to do this task. When you run your command to launch container, you have to use -p flag like -p 8080:80.

Can helm be used with Docker?

No. A helm chart is a templated set of kubernetes manifests. There will usually by a manifest for a Pod, Deployment, or Daemonset. Any of those will have a reference to a docker image (either hard coded or a parameter).


1 Answers

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" . }}
like image 67
Dhia Avatar answered Oct 20 '22 23:10

Dhia