<p>I am fairly new to this, I don't know if I am heading in the right direction or not. I have a custom nginx.conf that works fine, I am now trying to build a docker image with it so that I can run it as a container in kuberentes. </p>

<p>Here is my nginx.conf</p>

<pre><code>user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}


stream {
    #define the various upstreams
    upstream app_1 {
        server 192.168.122.206:5678;
    }

    upstream app_2 {
        server 192.168.122.206:9000;
    }   
    #map source port to upstream
    map $remote_port $backend_svr {
        1234 "app_1";
        1235 "app_2";
    }
    #all udp traffic received on 8000, depending on source it will be redirected
    server {
        listen 8000 udp;
        proxy_pass $backend_svr;
    }

}
</code></pre>

<p>On my virtual machine I have /home/asvilla/docker-files/nginx-udp/Dockerfile  which contains </p>

<pre><code>FROM nginx
RUN chmod +w /etc/nginx/nginx.conf
COPY nginx.conf /etc/nginx/nginx.conf
RUN cat /etc/nginx/nginx.conf
</code></pre>

<p>I build it using </p>

<pre><code>docker build -t "custom_nginx:dockerfile" .
</code></pre>

<p>The nginx container should redirect udp traffic incoming on port 8000 to either port 5678 or port 9000 depending on the source of the udp packet. </p>

<p>I run with docker run 'image-id' but it doesn't function as expected. Running docker ps shows "PORTS   80/tcp "
and "COMMAND  nginx-g daemon of ..."</p>

<p>Any pointers on what these mean. nginx by default binds tcp to port 80 but I have changed the nginx.conf, the cat command I run shows that the file is updated. </p>

<p>I am assuming that I have to expose the ports in the nginx.conf somehow. Any help much appreciated.</p>

alexv9
<p>If your end goal is to run this in Kubernetes, your easiest path will be to put this config file into a <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">ConfigMap</a> and just configure your Deployment to run the standard nginx image.  (In plain Docker, you can use <code>docker run -v</code> to inject the config file into the container at runtime to similar effect.)</p>

<p>It doesn't really matter what port nginx listens on inside the container.  If the stock nginx container expects to listen on the standard HTTP port 80 (and it looks like its <code>Dockerfile</code> has an <code>EXPOSE 80</code> directive) then you can embrace that and <code>listen 80</code> in your nginx config (over TCP, not UDP).  Then in your Kubernetes deployment you can specify that as a container port, and if you want to map it to something else, you can do that in the Service that wraps this.  (In plain Docker, if you want host port 8000 to avoid conflicting with other things, <code>docker run -p8000:80</code>.)</p>

<p>In terms of best practices I'd discourage directly writing IP addresses into config files.  If it's a persistent server outside your cluster, you can set up a DNS server in your network to resolve its hostname, or get a cloud service like Amazon's Route 53 to do it for you.  If it's in Kubernetes, use the service's DNS name, <code>backend.default.svc.cluster.local</code>.  Even if you really have only an IP address, creating an <a href="https://kubernetes.io/docs/concepts/services-networking/service/#externalname" rel="nofollow noreferrer">ExternalName service</a> will help you if the service ever moves.</p>

<p>Assuming you have the config file in a ConfigMap, your Deployment would look very much like <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#creating-a-deployment" rel="nofollow noreferrer">the sample Deployment in the Kubernetes documentation</a> (it even runs an <code>nginx:1.7.9</code> container publishing port 80).</p>
