Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS ECS using docker and ngnix, how to get my nginx config into the container?

I'm trying to setup a ECS cluster in AWS using Nginx, unicorn and Django.

I have converted my docker-compose.yml into a ECS task, but am wondering how I get my nginx config into the container?

this is my task in json

I have created some mount points for files but am unsure how to get the config there. when I run docker from my local servers the nginx config file is local to the compose file, obviously in aws it is not?

how do I get the nginx config into the contain through ecs?

{
    "containerDefinitions": [
        {
            "volumesFrom": null,
            "memory": 300,
            "extraHosts": null,
            "dnsServers": null,
            "disableNetworking": null,
            "dnsSearchDomains": null,
            "portMappings": [
                {
                    "containerPort": 8000,
                    "protocol": "tcp"
                }
            ],
            "hostname": null,
            "essential": true,
            "entryPoint": null,
            "mountPoints": [
                {
                    "containerPath": "/static",
                    "sourceVolume": "_Static",
                    "readOnly": null
                }
            ],
            "name": "it-app",
            "ulimits": null,
            "dockerSecurityOptions": null,
            "environment": null,
            "links": null,
            "workingDirectory": "/itapp",
            "readonlyRootFilesystem": null,
            "image": "*****.amazonaws.com/itapp",
            "command": [
                "bash",
                "-c",
                "",
                "python manage.py collectstatic --noinput && python manage.py makemigrations && python manage.py migrate && gunicorn itapp.wsgi -b 0.0.0.0:8000"
            ],
            "user": null,
            "dockerLabels": null,
            "logConfiguration": null,
            "cpu": 0,
            "privileged": null
        },
        {
            "volumesFrom": null,
            "memory": 300,
            "extraHosts": null,
            "dnsServers": null,
            "disableNetworking": null,
            "dnsSearchDomains": null,
            "portMappings": [
                {
                    "hostPort": 80,
                    "containerPort": 8000,
                    "protocol": "tcp"
                }
            ],
            "hostname": null,
            "essential": true,
            "entryPoint": null,
            "mountPoints": [
                {
                    "containerPath": "/etc/nginx/conf.d",
                    "sourceVolume": "_ConfigNginx",
                    "readOnly": null
                },
                {
                    "containerPath": "/static",
                    "sourceVolume": "_Static",
                    "readOnly": null
                }
            ],
            "name": "nginx",
            "ulimits": null,
            "dockerSecurityOptions": null,
            "environment": null,
            "links": [
                "it-app"
            ],
            "workingDirectory": null,
            "readonlyRootFilesystem": null,
            "image": "nginx:latest",
            "command": null,
            "user": null,
            "dockerLabels": null,
            "logConfiguration": null,
            "cpu": 0,
            "privileged": null
        }
    ],
    "placementConstraints": [],
    "volumes": [
        {
            "host": {
                "sourcePath": "./config/nginx"
            },
            "name": "_ConfigNginx"
        },
        {
            "host": {
                "sourcePath": "./static"
            },
            "name": "_Static"
        }
    ],
    "family": "it-app-task",
    "networkMode": "bridge"
}

ngnix config

upstream web {  
  ip_hash;
  server web:8000;
}
server {
    location /static/ {    
        autoindex on;    
        alias /static/; 
    }
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_pass http://web/;
    }
    listen 8000;
    server_name localhost;
}
like image 257
AlexW Avatar asked Sep 09 '17 16:09

AlexW


People also ask

How do I connect to a nginx container?

Copy the Docker container's Nginx config file to your local file system. Add proxy_pass entries that point to your backend origin servers. Copy the config file back into the Nginx Docker container. Reload the Nginx configuration and test the setup.

How do I get inside ECS container?

To connect to your container instanceOpen the Amazon ECS console at https://console.aws.amazon.com/ecs/ . Select the cluster that hosts your container instance. On the Cluster page, choose ECS Instances. On the Container Instance column, select the container instance to connect to.

Should nginx be in a Docker container?

If nginx is running in a container then your site is going to be 100% dead to the world while Docker isn't running. Users will get a connection error. When nginx is installed directly on your host you can serve a 503 maintenance page that doesn't depend on Docker or any containers running.


1 Answers

As far as I know (by the time of writing this), there is no "direct" way to inject configurations into containers in ECS other than using environment variables.

Despite that, here are some things you could do:

  1. Use the AWS CLI to get the nginx configuration from S3.

  2. Deploy a distributed key-value store such as etcd or Consul to your ECS cluster and then store and retrieve all configurations you need from it. These tools are usually used for shared configuration and service discovery. If you intend to use ECS for everything in your environment, this may be a good idea. Regarding nginx, for example, you could set up nginx + consul + registrator + consul template to automatically reload nginx configuration inside your container just by updating nginx configuration in Consul. Here is an example.


Well, just saying... I hope someday AWS provide something like ConfigMap and Secrets available on Kubernetes. In Kubernetes, that would be as simples as that:

  1. Add the nginx configuration to the Kubernetes cluster: kubectl create configmap nginx-configmap --from-file=nginx.conf

  2. Define in your Pod definition (like your "ECS Task Definition") that you want Kubernetes to inject the configuration into your container:

volumeMounts: - name: nginx-config-volume mountPath: /etc/nginx ... volumes: - name: nginx-config-volume configMap: name: nginx-configmap

And that's it!

like image 101
Jorge Acetozi Avatar answered Oct 06 '22 02:10

Jorge Acetozi