A
A
Alexander2016-07-09 13:50:54
Computer networks
Alexander, 2016-07-09 13:50:54

How to automatically execute script on host after docker container is started?

Initially there is: a host running docker daemon. Disabled iptables and ip-masq in startup options. The host has an external network interface (eth0) connected to the bridge (br0), the bridge has a static address configured (192.168.0.10/24).
You need to run several containers and assign a fixed address to each:

  • app1 - 192.168.0.21
  • app2 - 192.168.0.22
  • app3 - 192.168.0.23

To run I use docker-compose up -d. Compose is interesting in that it restarts the container itself if it crashes.
There is a great article on this topic: blog.oddbit.com/2014/08/11/four-ways-to-connect-a-...
It is suggested that you first start the container as is, and then add the address from the host:
# docker run -d --name app1 local/app1
# ip link add app1-int type veth peer name app1-ext
# brctl addif br0 app1-ext
# ip link set netns $(docker-pid app1) dev app1-int
# nsenter -t $(docker-pid app1) -n ip link set app1-int up
# nsenter -t $(docker-pid app1) -n ip addr add 192.168.0.21/24 dev app1-int
# nsenter -t $(docker-pid app1) -n ip route del default
# nsenter -t $(docker-pid app1) -n ip route add default via 192.168.0.1 dev app1-int

The option is suitable, you can wrap it in a script.
The only question is: when the application crashes, compose will restart it. Is there a way to automatically run this script on the host immediately after (re)starting the container?
There is another option: run the script inside the container (via CMD), which, before the application starts, accesses the host (for example, via ssh) and runs the script. Looks crooked.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2016-07-11
@yaka

It may be possible to specify a static ip https://docs.docker.com/compose/compose-file/#/networks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question