Deploying a Docker container to AWS Elastic Beanstalk using Dockerrun.aws.json (please help!)

Cross post from /r/docker as I think that forum has given me all the help they can here…

https://www.reddit.com/r/docker/comments/gj78h1/deploying_a_docker_image_to_amazon_elastic/?utm_medium=android_app&utm_source=share

…

I’m a web developer but I have been parachuted into a DevOps role I did not ask for because I have set up a few old school LAMP servers in the past and I’m being pushed for results. The client wants to use Docker for development and deployment of a WordPress site to AWS EB. I can’t get my head around the deployment…

Here is my previous post about setting up the development environment if anyone is interested:

https://www.reddit.com/r/docker/comments/gi1i83/questions_about_docker_as_a_development/

So here’s where I’m at now… I have a docker-compose.yml file (two actually - one for development and one for deployment) and those appear to work. Here is the ‘deploy’ file:

version: “3.3”

services:
    wordpress:
        container_name: deploy-wordpress
        build: ./wordpress
        restart: always
        environment:
            WORDPRESS_DB_HOST: 'my-rds-instance.rds.amazonaws.com:3306'
            WORDPRESS_DB_USER: admin
            WORDPRESS_DB_PASSWORD: password
            WORDPRESS_DB_NAME: wordpress
        ports:
            - 80:80

I can run ‘docker-compose up’ and it is visible on port 80 and connects to RDS. Great!

But when I try to run ‘eb create eb-wp-test-production’ to deploy to EB it creates an unusable environment with ‘Severe’ health. This is the output of that command (just the errors with the irrelevant stuff taken out):

eb create eb-wp-test-production
error: open(“mysql/ca-key.pem”): Permission denied
fatal: cannot hash mysql/ca-key.pem

[...]

2020-05-13 19:37:28    ERROR   [Instance: i-096ff1f0fa0362cc0] Command failed on instance. An unexpected error has occurred [ErrorCode: 0000000001].
2020-05-13 19:37:28    INFO    Command execution completed on all instances. Summary: [Successful: 0, Failed: 1].
2020-05-13 19:38:30    ERROR   Create environment operation is complete, but with errors. For more information, see troubleshooting documentation.

My understanding was that I just need a Dockerrun.aws.json file to perform this task. Here’s what I have:

{
“AWSEBDockerrunVersion”: “1”,
“Authentication”: {
“Bucket”: “my-bucket-with-docker-conf”,
“Key”: “dockercfg”
},
“Image”: {
“Name”: “my-dockerhub-username/eb_wp_test:v0.2”,
“Update”: “true”
},
“Ports”: [
{
“ContainerPort”: “80”
}
],
“Volumes”: [
{
“HostDirectory”: “/wordpress/src”,
“ContainerDirectory”: “/var/www/html”
}
],
“Logging”: “/var/log/apache2”
}

Questions:

  1. Does ‘docker-compose build’ put all the environment variables etc from docker-compose.yml into the image? In other words does docker-compose have anything to do with running the image outside of my local environment?

  2. If the answer to the above is no… how do I set it up for use on AWS EB? Just with the Dockerrun.aws.json file?

  3. I’m not doing anything with MySQL so what is the error…

error: open(“mysql/ca-key.pem”): Permission denied
fatal: cannot hash mysql/ca-key.pem

… and why do I get it?

  1. I think I am missing an “Entrypoint” and “Command” in the above file, what should these be for WordPress on Apache? Is this the real problem here?

Thanks in advance for any help anyone can give me!

I’ve not used Elastic Beanstalk, but here is what I found:

  1. It specifies in the docker-compose reference that anything specified in the ‘environment’ section is not passed to build; for that you need to use the ‘args’ section of your build: https://docs.docker.com/compose/compose-file/#args
  2. This answer on StackOverflow suggests including a .config file in the zip you upload to Elastic Beanstalk - it doesn’t look like you are doing this currently?https://stackoverflow.com/a/50473256
    You may also be able to pass --envvars to your command so your command may look like eb create eb-wp-test-production --envvars WORDPRESS_DB_HOST=rds.amazonaws.com although I am not sure how you would pass multiple arguments to that.
  3. You may be getting that error because there is no database host defined, I’m not sure what it will try and to in that case
  4. When you omit the docker entry point or command, I think they inherit from the containers you’ve built from - in your case, the Wordpress apache container.

I would try specifying your environment variables in the args section if you want them built into your container, and see if that works - would explain why it was working when running from Docker compose however!

So just to clear this up in my head… If I use args instead of environment, those variables will be ‘built in’ to the image created, and therefore stay with it when I deploy?

I read this in the tutorial linked below which seems to contradict (I really hope you are right though!) →

“Environment stuff only applies to containers, not images. Just a heads-up to prevent confusion. Read on for more info on that. Those ARG variables will not be available in containers started based on the built image without further work.”

This makes no sense to me. Containers are instances of images unless I’ve totally misunderstood this whole thing. Why would args not be available in containers based on the image? Or is that saying (badly) that ‘arguments set with env are not available to future containers but arguments set with arg are’? If so it agrees with you but is terribly written and has cost me an hour!

Said tutorial: https://vsupalov.com/docker-env-vars/

Sorry, I got this slightly wrong: args are for use with the ‘arg’ directive in your Dockerfile, but you can use them for setting default environment variables like:

ARG myvar

ENV myenvvar $myvar

This will set the default environment variable for your built image to the value of myvar which you pass in when you build the container. In this instance, I think you are passing runtime arguments in your Docker compose file which are not picked up by Docker during build time.