Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate Dockerrun.aws.json files for staging and production

What is the best way to handle deployment to staging and production for the Dockerrun.aws.json file? Is there a way to pass variables to the image value, etc or have multiple Dockerrun.aws.json files one for each environment? Currently my staging env gets the image tagged as staging and production gets the images tagged as production but I need the Dockerrun.aws.json different for each env? I either want to do something like:

"image": "${IMAGE}",

where IMAGE is defined in the configs for each environment or separate each file out. So I can setup each container differently based on staging or production.

like image 222
mcd Avatar asked May 03 '18 19:05

mcd


1 Answers

Old question but in case it can help others, I wanted to do pretty much the same thing and automate it, so as a quick way to do it I came up with a simple shell script.

The idea was to have a Dockerrun.aws.json template file that would hold a dynamic ENV property, then depending on the desired environment, the script would use this template and generate the appropriate Dockerrun.aws.json file.

Example:

Create a shell script with the following content:

#!/bin/bash

# current script directory path
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# $1 will be the environement name passed to the script : it can only be dev or prod
# if empty, we ask for user input for convenience
if [ "$1" == "" ]; then
  echo -n "Enter your the environment (either 'dev' or 'prod') and press [ENTER]:"
  read ENV
else
  ENV=$1
fi

# check if environment name is valid
if [ "$ENV" == "dev" ] || [ "$ENV" == "prod" ] ; then

  # move to shell script directory
  cd $DIR

  # generate Dockerfile from template by replacing the ENV property by the input
  echo "Generating Dockerrun.aws.json..."
  sed -e "s/\${ENV}/$ENV/g" Dockerrun.aws.json.template > Dockerrun.aws.json

  # do other things here if necessary

else
  echo "$ENV is not a valid environment name, accepted values : env & prod"
  exit 0
fi

Then create your Dockerrun.aws.json.template file:

{
  "AWSEBDockerrunVersion": 2,
  "containerDefinitions": [
    {
      "name": "php-app",
      "image": "phpfpm-image-${ENV}",
      #...
    },
    {
      "name": "nginx-proxy",
      "image": "nginx-image-${ENV}",
      #...
    }
  ]
}

Now, just put the shell script where your Dockerrun.aws.jon.template file resides, and run it like so:

sh yourscript.sh dev

It will generate a valid file for you to use for the given environment.

This is a simple example that gives you a basic idea of what do to, then you can build something much more complex from it. I personally use it to pick all the right config files (.ebextensions, etc.) and then zip the whole thing to upload on beanstalk.

like image 92
mxlhz Avatar answered Nov 10 '22 17:11

mxlhz