Can I config the hosts file for spring boot docker container? - docker

I develop a spring boot application and build a docker image. To get the app running, I have to add a new line "127.0.0.1:www.hostname.com" to /etc/hosts. I want to make this config automatically when I run docker image. I tried this:
docker run --add-host www.hostname.com:127.0.0.1 xxx/name:1.0
what I expect is:
www.hostname.com:8080 = 127.0.0.1:8080
but it is not work.
Please give some advice. thanks.

If you run the container with --hostname e.g.
docker run -d -ti --hostname myhost --name mytest ubuntu /bin/bash
you add a line 172.17.0.3 www.hostname.com to container's /etc/hosts, where 172.17.0.3 is the private address of the container itself, so it is resolving it internally from the container itself and from the docker docker server too.
If you run the container with --add-host e.g.
docker run -d -ti --add-host www.hostname.com:127.0.0.1 --name mytest ubuntu /bin/bash
you add a line 127.0.0.1 www.hostname.com to container's /etc/hosts, so it is resolving www.hostname.com to 127.0.0.1 internally from the container itself.
It works, at least in Docker version 1.13.0 on Linux.
Otherwise you can change container's /etc/hosts at system boot using sed or other substitution command in CMD or overriding the command at docker run.

Related

No such image or container error

I want to setup a rancher server and a rancher agent on the same server.
Here is what i have done for creating server:
docker run -d --restart=unless-stopped -p 8080:8080 rancher/server:stable
Then, I have opened my web-browser on 8080 port.
I have chosen a login/password and enabled access control.
Then i wanted to create a host (agent). Rancher web interface says me to type this command:
docker run -e CATTLE_AGENT_IP=x.x.x.x --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.10 http://nsxxx.ovh.net:8080/v1/scripts/yyyy:1514678400000:zzzz
I have no error message, but I do not see any entry in host section (in rancher web interface).
So I tried to execute a shell on the agent docker container:
docker exec -ti xxxxx /bin/bash
I tried to manually run run.sh script and here is what I see:
Error: No such image or container: nsxxx
I suppose this is because docker containers cannot communicate together, but I have done exactly what is in the documentation...
Thanks for your help
For docker exec your need to replace the xxxxx string with the container id or the name of the container. Both you get from the docker ps command

docker: connect to database container via dockerhost

I am trying to connect from an application container to a database container in two situations, one succeeds, one doesn't.
There are two containers on my dockerhost:
mysql container with port 3306 connected to 3356 on dockerhost
application container
At work, dockerhost has IP-address 10.0.2.15, at home, dockerhost has IP-address 192.168.8.11 (hostname -I).
In both situations, I want to connect to the database container from the app container with host 10.0.2.15/192.168.8.11 and port 3356.
When I do this at work (Windows network, Vagrant/Virtualbox dockerhost), this is no problem. I can 'telnet 10.0.2.15 3356' from the app container and connect to the db container.
When I do this at home (Ubuntu), it is impossible to connect. The only way is to use the docker ip address of the db container (172.17.0.2) with port 3306. However, I can ping 192.168.8.11.
The scripts to start the containers are identical; I do not use --add-host, so the dockerhost IP-address is not in /etc/hosts.
Any suggestions?
Ok, use docker to run 3 database instances
docker run --name mysqldb1 -e MYSQL_ROOT_PASSWORD=changeme -d mysql
docker run --name mysqldb2 -e MYSQL_ROOT_PASSWORD=changeme -d mysql
docker run --name mysqldb3 -e MYSQL_ROOT_PASSWORD=changeme -d mysql
Each one will have a different IP address on my host machine:
$ for i in mysqldb1 mysqldb2 mysqldb3
> do
> docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $i
> done
172.17.0.2
172.17.0.3
172.17.0.4
Repeat this on your machine and you'll very likely have different IP addresses.
So how is this problem fixed.
The older approach (deprecated in docker 1.9) is to use links. The following commands will shows how environment variables are set within your linked application container (the one using the database)
$ docker run -it --rm --link mysqldb1:mysql mysql env
..
MYSQL_PORT_3306_TCP_ADDR=172.17.0.2
$ docker run -it --rm --link mysqldb2:mysql mysql env
..
MYSQL_PORT_3306_TCP_ADDR=172.17.0.3
$ docker run -it --rm --link mysqldb3:mysql mysql env
..
MYSQL_PORT_3306_TCP_ADDR=172.17.0.4
And the following demonstrates how links are also created:
$ docker run -it --rm --link mysqldb1:mysql mysql grep mysql /etc/hosts
172.17.0.2 mysql 2a12644351a0 mysqldb1
$ docker run -it --rm --link mysqldb2:mysql mysql grep mysql /etc/hosts
172.17.0.3 mysql 89140cbf68c7 mysqldb2
$ docker run -it --rm --link mysqldb3:mysql mysql grep mysql /etc/hosts
172.17.0.4 mysql 27535e8848ef mysqldb3
So you can just refer to the other container using the "mysql" hostname or the "MYSQL_PORT_3306_TCP_ADDR" environment variable.
In Docker 1.9 there is a more powerful networking feature that enables containers to be linked across hosts.
http://docs.docker.com/engine/userguide/networking/dockernetworks/
you can use my container acting as a NAT gateway to dockerhost without any manually setup https://github.com/qoomon/docker-host

How to link to additional container without restarting?

X-post from https://groups.google.com/forum/#!topic/docker-user/A180aHSlQRE
Let's say I run following command to link web container to db container-
docker run -d -P --name web --link db training/webapp python app.py
Now I want my web container to be linked to additional container WITHOUT restarting web container. Is it possible?
No, once started you can't link the container to another container. But you can link a new container to the web container:
docker run -d -P --name myapp --link web <image> <command>
or you can link another web container to the db container:
docker run -d -P --name web2 --link db training/webapp python app.py
Having said that since your first web container is running you can also run:
docker inspect web
to find out the details of that container and see if you'd like to use them in your new container that you create. Another thing you can try is to make your web container interactive so once you started it you can modify it at runtime.
Actually, there is way to overcome this limitation.
What you can do in this situation is to add LINKED_CONTAINER_IP and LINKED_CONTAINER_NAME directly into /etc/hosts file of "HOST CONTAINER" using following procedure:
get the IP address of the running container which you want additionally to link into "host container" using
docker inspect --format '{{ .NetworkSettings.IPAddress }}' CONTAINERID
directly from host
3 then add that ip with the name into /etc/hosts of "HOST CONTAINER" file as following
first get interactive access into shall of running host container
docker exec -it CONTAINERID sh
once you get into shell prompt add line to /etc/hosts file using
echo "LINKED_CONTAINER_IP LINKED_CONTAINER_NAME" >> /etc/hosts
verify using ping LINKED_CONTAINER_NAME
Please note this is temporary solution which works only until any of containers are restarted in which case IP address may change and therefore resolving to LINKED_NAME will not work any more!

Docker in Docker: Port Mapping

I have found a similar thread, but failed to get it to work. So, the use case is
I start a container on my Linux host
docker run -i -t --privileged -p 8080:2375 mattgruter/doubledocker
When in that container, I want to start another one with GAE SDK devserver running.
At that, I need to access a running app from the host system browser.
When I start a container in the container as
docker run -i -t -p 2375:8080 image/name
I get an error saying that 2375 port is in use. I start the app, and can curl 0.0.0.0:8080 when inside both containers (when using another port 8080:8080 for example) but cannot preview the app from the host system, since lohalhost:8080 listens to 2375 port in the first container, and that port cannot be used when launching the second container.
I'm able to do that using the image jpetazzo/dind. The test I have done and worked (as an example):
From my host machine I run the container with docker installed:
docker run --privileged -t -i --rm -e LOG=file -p 18080:8080
jpetazzo/dind
Then inside the container I've pulled nginx image and run it with
docker run -d -p 8080:80 nginx
And from the host environment I can browse the nginx welcome page with http://localhost:18080
With the image you were using (mattgruter/doubledocker) I have some problem running it (something related to log attach).

How to assign a host port to container port using docker if container is already created and running?

We can create a new container and define your application port in docker run command like
sudo docker run -d -p 5000:5000 training/webapp python app.py
or
sudo docker run -d -P training/webapp python app.py
But, what if someone forgot to specify -p or -P option in docker run command? The container get created and runs the application locally. Now how could I assign a port on which application is running locally in container to the port of my Ubuntu host machine?
Kindly, help on this.
Thanks.
Short: You can't. You need to stop the container (or not) and start a new one with the proper parameters.
Docker spins up a local proxy and setup the iptables for proper NAT. If you really can't start a new container, you could manually setup the iptables and spin up a socat. You can take a look at the network part of the Docker code for more info.

Resources