amazon-web-servicesdockeramazon-elastic-beanstalk

beanstalk docker - how to set container name with Docker.run.json v1


I'm deploying this Docker.run.json file and its working.

{
    "AWSEBDockerrunVersion": "1",
    "Image": {
        "Name": "xxxxxx.dkr.ecr.us-east-1.amazonaws.com/myproject:build-xxxx-xxx-xxx-xx-xxx",
        "Update": "true"
    },
    "Ports": [
        {
            "ContainerPort": 80,
            "HostPort": 80
        }
    ]
}

Now I need to create a cron with .ebextensions to run a command inside my container.

The problem is the container is always run with random names.

Is there any way to define the container name so I can run command inside it?

docker exec MY_CONTAINER echo "Hello world"

Solution

  • You can dynamically get the name and exec your command:

    docker exec $(docker ps --filter "ancestor=xxxxxx.dkr.ecr.us-east-1.amazonaws.com/myproject" --format "{{.Names}}") echo "Hello world" >> /var/log/cron.log 2>&1
    

    ancestor=IMAGE_NAME: This filter selects containers that were created from a specific image, which will be static, so you can use it without any issue