Here's the thing, I need to tell Docker to not containerize the container’s networking, because it needs to connect to a MongoDB that is inside a VPN (enterprise private DB).
There is a Docker command that let's me do exactly that: --net=host
. Reference here.
So, for example, when running the container on my local machine, I will do something like:
docker run --rm -it --net=host [image-name]:[version] bash -il
And that command will do the trick. Thanks to that, I can connect to the "private" MongoDB.
So, my question is: Is there a way customize the docker run
command of a Single Docker Environment on Elastic Beanstalk so I can add the --net=host
?
I have tried using the container_commands into the config.yml
file to add that instruction there, but I don't think that does what I need, here is a snippet:
container_commands:
00-test_command:
command: bundle exec thin --net=host
01-networking-fix:
command: "docker run --rm -it --net=host [image-name]:[version] bash -il"
Note that the latest version of the AWS stack (with Docker 1.7.1) has a slightly different pre-deploy setup. You'll need to update the file at the location:
/opt/elasticbeanstalk/hooks/appdeploy/enact/00run.sh
or, for example, if you want to pass args to your Docker image:
I ended up fixing it with two container commands
Update: I also had to fix the Upstart script. Unfortunately, I didn't write down what I did because I didn't end up needing to alter the
docker run
command. You would do afiles
directive for (I think)/etc/init/docker
. AWS edits the Nginx configuration in the same manner as in01flip.sh
in that file as well.Explanation:
In the
64bit Amazon Linux 2015.03 v2.0.2 running Docker 1.7.1
platform version, the file you need to edit is/opt/elasticbeanstalk/hooks/appdeploy/enact/00run.sh
. This file is now far more complex than Samar's version so I didn't want to put the actual contents in there. However, the change is basically the same. There's the line that starts withI fixed it with a container command:
This successfully adds the
--net=host
argument but now there's another problem. The system ends up with an invalid Nginx directive. Using--net=host
means that when you rundocker inspect <container id>
there is no IP address in the NetworkSettings. AWS uses this to create the server directive for Nginx and ends up generatingserver :<some port you chose>
(before adding--net=host
it would look likeserver <ip>:<port>
). I needed to patch that file, too. It's generated in/opt/elasticbeanstalk/hooks/appdeploy/enact/01flip.sh
.While elastic beanstalk is generally well suited for applications that work with standard set of configurations, its difficult to customize and keep things updated along with the updates AWS provides to EB stacks. Having said that, I've done something like below which is a bit hacky but works fine.
This is not very neat, at least I have to make sure that it does not break with updates on elastic beanstalk. The above one is for docker 1.5 stack but you can do something similar with the version you're running.