Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: Connecting to external database

I have hbase instance running which my app connects, We are planning to move the app into docker container but keep hbase running outside of docker. I could make the my app running in docker container connect to hbase by using add-host option while running docker container as below

docker run -dit --add-host hbasehost:xxx.xxx.xxx.xxx mydockerimage

However what we need is auto-scaling function of swarm, as we have multiple services to run, what is the correct way of achieving this if i want to run my app as docker service instead of individual container, i couldn't find any references to "--add-host" in "docker service"

like image 725
suresh Avatar asked Nov 22 '16 05:11

suresh


People also ask

Can Docker container connect to outside world?

Your Docker container can connect to the outside world, but the outside world cannot connect to the container. To make the ports accessible for external use or with other containers not on the same network, you will have to use the -P (publish all available ports) or -p (publish specific ports) flag.

Can you Dockerize database?

Docker is great for running databases in a development environment! You can even use it for databases of small, non-critical projects which run on a single server.


2 Answers

Update: As of docker 1.13 you now have a similar flag to add entries to /etc/host.

To add a host at service creation, you can use the --host flag:

docker service create --name myapp --host "hbasehost:xxx.xxx.xxx.xxx" --replicas 5 myimage

To update the service and add an additional host after its creation, you use the --host-add flag:

docker service update --host-add "hbase:x.x.x.x" myapp

You can also remove a host using --host-rm.


Original answer

--add-host only appends an host:IP association in /etc/hosts.

You can switch from using --add-host to using environment variables with --env even though this will require slight changes to your app to use the environment variable instead of the hostname to connect to hbase.

# Applies environment variables for all tasks in a service.

docker service create --name myapp --replicas=5 --env HBASEHOST=xxx.xxx.xxx.xxx myimage

Then you can scale the service using:

docker service scale myapp=20

Additional tasks should be able to use the environment variable to connect to hbase.

Source: I'm an ex-Docker Swarmkit maintainer

like image 197
abronan Avatar answered Oct 28 '22 12:10

abronan


If you pointed your docker hosts to an internal dns server or a dns server that you can create records on, You can create A record for your hbasehost on that DNS server. Containers will use Internal Docker DNS then fall back to hosts DNS Server Settings. But setting a record in /etc/hosts get the job done (weired).

My docker swarm node's resolv.conf:

nameserver 10.110.1.25
nameserver 10.110.1.26

Now verify from inside one of the containers:

root@05d05f934c68:/# ping dc1.mydomain.net
PING dc1.mydomain.net (10.110.1.25): 56 data bytes
64 bytes from 172.20.21.5: icmp_seq=0 ttl=121 time=0.929 ms
like image 40
Farhad Farahi Avatar answered Oct 28 '22 13:10

Farhad Farahi