What Postgres connection string should I use if I run a docker multicontainer app via Dockerrun.aws.js
on AWS ?
I have a Node.JS/Postgres/docker web-application. Postgres runs in its own container and so does the app. Locally, the app runs ok. When I deploy it to AWS via ECR and BeanStalk , the application successfully deploys and runs, but the web-app doesn't connect to Postgres.
In docker-compose.yaml
, the host in the connection string is the name of the container (in my case it would be db ). That doesn't work with AWS . Neither does localhost
or 127.0.0.1
.
Here is my Dockerrun.aws.js
:
{
"AWSEBDockerrunVersion": 2,
"volumes": [
{
"name": "db-data",
"host": {
"sourcePath": "/data/db"
}
}
],
"containerDefinitions": [
{
"name": "db",
"image": "db_image_name",
"essential": true,
"memory": 128,
"environment": [
{
"name": "POSTGRES_USER",
"value": "postgres"
},
{
"name": "POSTGRES_PASSWORD",
"value": "password"
},
{
"name": "PGDATA",
"value": "/data/db/pgdata"
}
],
"portMappings": [
{
"hostPort": 5432,
"containerPort": 5432
}
],
"mountPoints": [
{
"sourceVolume": "db-data",
"containerPath": "/data/db"
}
]
},
{
"name": "app",
"image": "app_image_name",
"essential": true,
"memory": 128,
"environment": [
{
"name": "NODE_ENV",
"value": "production"
},
{
"name": "DB_HOST",
"value": "db"
},
{
"name": "DB_PORT",
"value": "5432"
},
{
"name": "DB_PASSWORD",
"value": "password"
}
],
"links": [
"db"
],
"portMappings": [
{
"hostPort": 80,
"containerPort": 3000
}
]
}
]
}
What should I put in the environment variable DB_HOST ? ( DB_HOST together with DB_PORT are used by the app to construct the connection string.) Thanks a lot.
Beanstalk and Dockerrun.aws.json format offer you a possibility to link containers from the same definition file with the following syntax:
"links": ["some-name"]
In your case you can add a link to "db"
and postgres will be available in your app container under db
. You don't even need to map container ports if you don't want to expose your postgres container to the world.
You can see an example in use in following docs: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker_v2config.html#create_deploy_docker_v2config_dockerrun
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.