如何将 Django 的 docker-compose.yml 翻译成 Dockerrun.aws.json

How to translate docker-compose.yml to Dockerrun.aws.json for Django

我正在按照 https://docs.docker.com/compose/django/ to get a basic dockerized django app going. I am able to run it locally without a problem but I am having trouble to deploy it to AWS using Elastic Beanstalk. After reading here 上的说明进行操作,我想我需要将 docker-compose.yml 翻译成 Dockerrun.aws.json 才能正常工作。

原来的docker-compose.yml是

version: '2'
services:
  db:
    image: postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

这是我目前翻译的内容

{
  "AWSEBDockerrunVersion": 2,
  "volumes": [
    {
      "name": "db"
    },
    {
      "name": "web"
    }
  ],
  "containerDefinitions": [
    {
      "name": "db",
      "image": "postgres",
      "essential": true,
      "memory": 256,
      "mountPoints": [
        {
          "sourceVolume": "db"
          "containerPath": "/var/app/current/db"
        }
      ]
    },
    {
      "name": "web",
      "image": "web",
      "essential": true,
      "memory": 256,
      "mountPoints": [
        {
          "sourceVolume": "web"
          "containerPath": "/var/app/current/web"
        }
      ],
      "portMappings": [
       {
         "hostPort": 8000,
         "containerPort": 8000
       }
     ],
     "links": [
        "db"
      ],
      "command": "python manage.py runserver 0.0.0.0:8000"
    }
  ]
}

但它不起作用。我做错了什么?

你有一些问题。

1) 'web' 似乎不是 'image',您在 docker-compose 中将其定义为 'build . '。请记住,Dockerrun.aws.json 必须从某个地方拉取镜像(最简单的是使用 ECS 的存储库)

2) 我认为'command'是一个数组。所以你有:

"command": ["python" "manage.py" "runserver" "0.0.0.0:8000"]

3) 你的挂载点是正确的,但是顶部的卷定义是错误的。 { "name": "web", "host":{ "sourcePath": "/var/app/current/db" } 我不是 100% 确定,但这条路对我有用。 如果你有 Dockerrun.aws.json 文件,旁边是一个名为 /db 的目录。那将是挂载位置。

我一直在努力了解 Dockerrun 格式的来龙去脉。查看 Container Transform:"Transforms docker-compose, ECS, and Marathon configurations"...它是救命稻草。这是它为您的示例输出的内容:

{
    "containerDefinitions": [
        {
            "essential": true,
            "image": "postgres",
            "name": "db"
        },
        {
            "command": [
                "python",
                "manage.py",
                "runserver",
                "0.0.0.0:8000"
            ],
            "essential": true,
            "mountPoints": [
                {
                    "containerPath": "/code",
                    "sourceVolume": "_"
                }
            ],
            "name": "web",
            "portMappings": [
                {
                    "containerPort": 8000,
                    "hostPort": 8000
                }
            ]
        }
    ],
    "family": "",
    "volumes": [
        {
            "host": {
                "sourcePath": "."
            },
            "name": "_"
        }
    ]
}
Container web is missing required parameter "image".
Container web is missing required parameter "memory".
Container db is missing required parameter "memory".

也就是说,在这种新格式中,您必须告诉它分配给每个容器多少内存。此外,您需要提供图像 - 没有构建选项。正如评论中提到的那样,您想要构建并推送到 DockerHub 或 ECR,然后为其提供该位置:例如 Dockerhub 上的 [org name]/[repo]:latest,或 ECR 的 URL。但是 container-transform 为您完成 mountPointsvolumes - 太棒了。