简体   繁体   中英

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;
}

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM