我一直在尝试部署由Django,Postgresql和Nginx组成的docker。
sudo docker-compose up可以很好地工作,但是在AWS EB上部署它时,它会给我
could not translate host name "db" to address: Name or service not known
我所做的是我使用
sudo docker build -t myname/dockername -f Dockerfile .将docker推送到docker hub,我只是做了
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上,我只看到一个容器在运行。
我认为这是因为我正在从使用这些文件构建的docker hub中获取图像,并且以某种方式导致这3个容器成为一个容器,并且搞乱了主机名的识别?我还是不太确定。帮助将不胜感激。谢谢!
答案 0 :(得分:4)
Dockerrun.aws.json
应该与docker-compose.yml
无法将主机名“ db”转换为地址的原因是docker-compose.yml
和Dockerrun.aws.json
文件描述了不同的体系结构:
docker-compose.yml
中有3个容器Dockerrun.aws.json
中只有1个容器因此,应用程序尝试解析db
主机名,但找不到它,因为db
中未声明Dockerrun.aws.json
Dockerrun.aws.json
因此,更新您的Dockerrun.aws.json
。您可以手动执行此操作,也可以使用便捷的工具micahhausler/container-transform:
您可以使用示例,例如:
micahhausler/container-transform
您可以尝试micahhausler/container-transform:
转换docker-compose,ECS和Marathon配置 转换docker-compose,ECS和Marathon配置
这是您的案例输出的结果:
$ 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"
}
]
}
注意::当然,您应该修复memory
和db
容器缺少的设置,例如nginx
。
networks
根据Networking in Compose | Docker Documentation:
例如,假设您的应用程序位于名为
myapp
的目录中,并且您的docker-compose.yml
如下所示:
docker-compose.yml
version: "3"
services:
web:
build: .
ports:
- "8000:8000"
db:
image: postgres
ports:
- "8001:5432"
运行
docker-compose up
时,会发生以下情况:
- 创建了一个名为
myapp_default
的网络。- 使用
web
的配置创建了一个容器。它以名称myapp_default
加入网络web
。- 使用
db
的配置创建了一个容器。它以名称myapp_default
加入网络db
。
因此,由于所有容器都链接到相同的some_network
,因此您可以省略它。
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"
}
]
}