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;
}
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.
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.
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.
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:
Use the AWS CLI to get the nginx configuration from S3.
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:
Add the nginx configuration to the Kubernetes cluster: kubectl create configmap nginx-configmap --from-file=nginx.conf
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With