问题描述
我一直在尝试部署由Django,Postgresql和Nginx组成的docker.当我
I've been trying to deploy my docker consisted of Django, Postgresql and Nginx. It works fine when I do
sudo docker-compose up
时,它可以很好地工作,但是在AWS EB上部署它时,它会给我
However when deploy it on AWS EB, it gives me
could not translate host name "db" to address: Name or service not known
我所做的是我使用
sudo docker build -t myname/dockername -f Dockerfile .
我只是做了
eb deploy
文件结构
myproject
myproject
settings.py
urls.py
...
Dockerfile
Dockerrun.aws.json
manage.py
requirements.txt
...
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
EXPOSE 8000
CMD ["sh", "on-container-start.sh"]
Dockerrun.aws.json
{
"AWSEBDockerrunVersion": "1",
"Image": {
"Name": "myname/dockername:latest",
"Update": "true"
},
"Ports": [
{
"ContainerPort": "8000"
}
]
}
docker-compose.yml
version: '3'
services:
db:
image: postgres
hostname: db
networks:
- some_network
web:
restart: always
build: .
volumes:
- .:/code
hostname: web
expose:
- "8000"
depends_on:
- db
links:
- db:db
networks:
- some_network
nginx:
image: nginx
hostname: nginx
ports:
- "8000:8000"
volumes:
- ./config/nginx:/etc/nginx/conf.d
depends_on:
- web
networks:
- some_network
networks:
some_network:
我意识到的一件事是,当我在机器上使用docker-compose时,我会运行3个不同的容器.但是在EB上,我只看到一个容器在运行.
One thing I realize is that when I use docker-compose up on my machine, I get 3 different containers running. However on EB, I see only one container running.
我认为这是因为我正在从使用这些文件构建的docker hub中获取图像,并且以某种方式导致这3个容器成为一个容器,并且搞乱了主机名的识别?我还是不太确定.帮助将不胜感激.谢谢!
I think it's because I'm fetching the image from docker hub that I built with those files and that somehow caused these 3 containers to be one and it's messing up with recognizing host names? I am quite not sure still. Help will be greatly appreciated. Thanks!
推荐答案
Dockerrun.aws.json
应该与 docker-compose.yml
相关无法将主机名"db"转换为地址的原因是 docker-compose.yml
和 Dockerrun.aws.json
文件描述了一个不同的架构:
Dockerrun.aws.json
should correlate with docker-compose.yml
The reason of issue that host name "db" could not be translated to address is that the docker-compose.yml
and Dockerrun.aws.json
files describe a different architecture:
-
docker-compose.yml
中有3个容器 -
Dockerrun.aws.json
中只有1个容器
- There are 3 containers in
docker-compose.yml
- There is only 1 container in
Dockerrun.aws.json
因此,应用程序尝试解析 db
主机名,但找不到它,因为在 Dockerrun.aws.json
中未声明的 db
Therefore, the application tries to resolve the db
hostname and cannot find it, because db
not declared in Dockerrun.aws.json
因此,更新您的 Dockerrun.aws.json
.您可以手动执行此操作,也可以使用便捷的工具 micahhausler/container-transform :
So, update your Dockerrun.aws.json
. You can do it either manually or using convenient tool micahhausler/container-transform:
您可以使用示例,例如:
You can use samples, such as:
您可以尝试 micahhausler/container-transform :
这是您的案例输出的结果:
Here is what it outputs for your case:
$ container-transform docker-compose.yml > Dockerrun.aws.json
Dockerrun.aws.json
{
"containerDefinitions": [
{
"essential": true,
"image": "postgres",
"name": "db"
},
{
"essential": true,
"image": "nginx",
"mountPoints": [
{
"containerPath": "/etc/nginx/conf.d",
"sourceVolume": "_ConfigNginx"
}
],
"name": "nginx",
"portMappings": [
{
"containerPort": 8000,
"hostPort": 8000
}
]
},
{
"essential": true,
"links": [
"db:db"
],
"mountPoints": [
{
"containerPath": "/code",
"sourceVolume": "_"
}
],
"name": "web"
}
],
"family": "",
"volumes": [
{
"host": {
"sourcePath": "."
},
"name": "_"
},
{
"host": {
"sourcePath": "./config/nginx"
},
"name": "_ConfigNginx"
}
]
}
注意::当然,您应该修复丢失的设置,例如 db
和 nginx
容器的内存
Note:: Of course, you should fix missing settings such as memory
for db
and nginx
containers.
docker-compose.yml
version: "3"
services:
web:
build: .
ports:
- "8000:8000"
db:
image: postgres
ports:
- "8001:5432"
- 创建了一个名为
myapp_default
的网络. - 使用
web
的配置创建了一个容器.它以名称web
加入网络myapp_default
. - 使用
db
的配置创建了一个容器.它以名称db
加入网络myapp_default
.
- A network called
myapp_default
is created. - A container is created using
web
’s configuration. It joins the networkmyapp_default
under the nameweb
. - A container is created using
db
’s configuration. It joins the networkmyapp_default
under the namedb
.
因此,由于您所有的容器都链接到相同的 some_network
,因此您可以忽略它.
So, since all your containers linked to the same some_network
, you can omit it.
docker-compose.yml
version: '3'
services:
db:
image: postgres
hostname: db
web:
restart: always
build: .
volumes:
- .:/code
hostname: web
expose:
- "8000"
depends_on:
- db
links:
- db:db
nginx:
image: nginx
hostname: nginx
ports:
- "8000:8000"
volumes:
- ./config/nginx:/etc/nginx/conf.d
depends_on:
- web
和 $ container-transform docker-compose.yml>Dockerrun.aws.json
将产生:
Dockerrun.aws.json
{
"containerDefinitions": [
{
"essential": true,
"image": "postgres",
"name": "db"
},
{
"essential": true,
"image": "nginx",
"mountPoints": [
{
"containerPath": "/etc/nginx/conf.d",
"sourceVolume": "_ConfigNginx"
}
],
"name": "nginx",
"portMappings": [
{
"containerPort": 8000,
"hostPort": 8000
}
]
},
{
"essential": true,
"links": [
"db:db"
],
"mountPoints": [
{
"containerPath": "/code",
"sourceVolume": "_"
}
],
"name": "web"
}
],
"family": "",
"volumes": [
{
"host": {
"sourcePath": "."
},
"name": "_"
},
{
"host": {
"sourcePath": "./config/nginx"
},
"name": "_ConfigNginx"
}
]
}
这篇关于AWS Elastic Beanstalk给出“无法翻译主机名" db".解决“错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!