Can containers share a framework?

2019-03-04 15:01发布

问题:

I'm aware that Docker containers can share a data volume but is it possible for them to share frameworks? For instance, if i have two .NET services running on IIS can I just share the framework between them?

回答1:

Yes you can, what you usually do is

Alternative A:

create a busybox image and COPY your framework, expose the location as a volume VOLUME /opt/framework/

FROM alpine
COPY framework /opt/framework
VOLUME /opt/framework
COPY busyscript.sh /usr/local/bin/busyscript
RUN chmod +x /usr/local/bin/busyscript
CMD ["busyscript"]

While the busyscript.sh looks like

#!/bin/sh
#set -x

pid=0

# SIGTERM-handler
term_handler() {
  if [ $pid -ne 0 ]; then
    kill -SIGTERM "$pid"
    wait "$pid"
  fi
  exit 143; # 128 + 15 -- SIGTERM
}

# setup handlers
# on callback, kill the last background process, which is `tail -f /dev/null` and execute the specified handler
trap 'kill ${!}; term_handler' SIGTERM

echo "Started code"
# wait forever
while true
do
  tail -f /dev/null & wait ${!}
done

Add this image as a service in your docker-compose.yml as lets say "framework", then, on the services you want them to consume, you add

volume_from
  - framework:ro

Pros:

  • you can compile, build and deploy the framworks soley
  • there is more or less no runtime overhead for running this extra container

Con:

  • image-size overhead ( alpine, 30mb)

Alternative B You use one of your services as the "framework base", lets say service A, that means you copy the framework on that service ( one of the 2 consuming it ) and also again use VOLUME /opt/framework to expose it as volume

in the service B, the same way, you mount the volume

serviceB:
  volume_from
    - serviceA:ro

Pro:

  • no extra container

Con:

  • framework needs to be deployed with serviceA, no matter service A would need updates
    • you have a dependency on A, does A need an update, all other containers need to be recreated due to the share