Reputation: 22596
I'm trying to "dockerize" an LAMP application and I have a problem to send email. I have 2 containers, one for apache/php and another for mysql. Everything works fine but I can't send any email. I've installed sendmail on the apache container but it needs to connect to a smtp server.
I've google a bit, and most answer are "setup your own MTA container", however, I'm running docker on Ubuntu, and there is already an MTA setup ( I can send email and use sendmail out of the box). So the idea is to use the host smtp server.
It should be possible to setup a "tunnel" or a "route" (I'm not sure of the term) to forward connection to the port 25 from inside the container to the port 25 of the host (basically the reverse of what docker does with -p). I've read the docker advanced networking and the 'ip' command manual but I can't figure how to do it.
At the moment my solution is to create all the container with --net=host
. This way sendmail
can see the smpt server of the host. The problem with this method is: you can't use --link
and --net=host
at the same time, therefore mean all the containers have to use --net=host
.
Upvotes: 3
Views: 7607
Reputation: 24483
You want to reach the host from within the container. You can already do this. For example, if the host that's running Docker is docker.mb14.com
then you can hit that address from within the container.
But that would give you an external-facing interface, and you probably don't want to listen on that. Instead, you can use an internal-facing interface and give it a friendly name inside the container with --add-host <alias>:<ip>
. This will add an /etc/hosts
entry just like --link
The documentation for this includes an example of adding an entry for your host system:
Note: Sometimes you need to connect to the Docker host, which means getting the IP address of the host. You can use the following shell commands to simplify this process:
$ alias hostip="ip route show 0.0.0.0/0 | grep -Eo 'via \S+' | awk '{ print \$2 }'" $ docker run --add-host=docker:$(hostip) --rm -it debian
(And there's an open issue that might help if you need an IPv6 address.)
Edit: After that, if you want to port forward so that you're talking to localhost
on the container, you need to handle that part yourself. There are lots of ways to do this (firewall rule, netcat, proxy) and they're independent of Docker. There is no built-in equivalent of Docker's -p
flag that goes in the other direction.
Upvotes: 4
Reputation: 1289
Use docker links. Docker links exposes the envrionment variables as well as make updates to /etc/hosts.
https://docs.docker.com/userguide/dockerlinks/
Upvotes: -1