On a firewalled Docker host you must add rules to open and close ports when containers start or stop.
One way to do it is to create a Systemd unit file per container and open/close ports with ExecStartPre/ExecStopPost directives. This has the disadvantage that relies on an external service management system and cannot use restart policies from Docker itself.
Is there a way to solve this problem with only Docker provided facilities?
The most generic solution I can think of is a facility (a plugin maybe?) to execute execute arbitrary commands on specified container events. A hooks mechanism in other words. Does Docker has something like this?
You can use ENTRYPOINT on docker to execute script during start up, and on this shell script you put a hook on SIGTERM using trap command
ie. create shell script start.sh
#!/bin/bash
stop() {
echo "container is being stopped"
}
trap 'stop' #
... other command ...
Next you can hook ENTRYPOINT
of docker to start.sh above
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.