专业财税服务推荐

精选优质财税服务,为企业提供专业、可靠的财税解决方案,助力企业健康发展

零报税代理记账
零申报代理记账
报税做账算帐财务报表老会计做账
代理记账
咨询微信:lhy_happyday
工商营业执照年度年报年检公示
全国个体、企业、公司、合作社工商年审年报服务!
个体/10元/次 企业/20元/次
咨询微信:lhy_happyday
财税咨询服务
一对一专业财税咨询,解决企业财税难题,提供定制方案
咨询微信:lhy_happyday
财务分析服务
小规模个体报税0申报税务年报工商年报月报季报报税代理记账
咨询微信:lhy_happyday
立即咨询专业财税顾问
微信号: lhy_happyday
会计从业9年,管理多家个体工商、小规模、一般纳税人等企业的财务、税务等相关工作!。
扫码或搜索添加微信,备注"财税咨询"获取专属优惠
知方号 知方号

Docker in Action: The Shared Memory Namespace docker --ipc

In this article, excerpted from the book Docker in Action, I will show you how to open access to shared memory between containers.

Linux provides a few tools for sharing memory between processes running on the same computer. This form of inter-process communication (IPC) performs at memory speeds. It is often used when the latency associated with network or pipe based IPC drags software performance below requirements. The best examples of shared memory based IPC usage is in scientific computing and some popular database technologies like PostgreSQL. 

Docker creates a unique IPC namespace for each container by default. The Linux IPC namespace partitions shared memory primitives like named shared memory blocks and semaphores, as well as message queues. It is okay if you are not sure what these are. Just know that they are tools used by Linux programs to coordinate processing. The IPC namespace prevents processes in one container from accessing the memory on the host or in other containers.

Sharing IPC Primitives Between Containers

I鈥檝e created an image named allingeek/ch6_ipc that contains both a producer and consumer. They communicate using shared memory. Listing 1 will help you understand the problem with running these in separate containers. 

Listing 1: Launch a Communicating Pair of Programs# start a producerdocker -d -u nobody --name ch6_ipc_producer allingeek/ch6_ipc -producer # start the consumerdocker -d -u nobody --name ch6_ipc_consumer allingeek/ch6_ipc -consumer

Listing 1 starts two containers. The first creates a message queue and starts broadcasting messages on it. The second should pull from the message queue and write the messages to the logs. You can see what each is doing by using the following commands to inspect the logs of each:

docker logs ch6_ipc_producerdocker logs ch6_ipc_consumer

If you executed the commands in Listing 1 something should be wrong. The consumer never sees any messages on the queue. Each process used the same key to identify the shared memory resource but they referred to different memory. The reason is that each container has its own shared memory namespace.

If you need to run programs that communicate with shared memory in different containers, then you will need to join their IPC namespaces with the --ipc flag. The --ipc flag has a container mode that will create a new container in the same IPC namespace as another target container.

Listing 2: Joining Shared Memory Namespaces # remove the original consumer dockerrm -v ch6_ipc_consumer# start a new consumer with a joined IPC namespacedocker -d --name ch6_ipc_consumer --ipc container:ch6_ipc_producer allingeek/ch6_ipc -consumer

Listing 2 rebuilds the consumer container and reuses the IPC namespace of the ch6_ipc_producer container. This time the consumer should be able to access the same memory location where the server is writing. You can see this working by using the following commands to inspect the logs of each:

docker logs ch6_ipc_producerdocker logs ch6_ipc_consumer

Remember to cleanup your running containers before moving on:

# remember: the v option will clean up volumes, # the f option will kill the container if it is running,# and the rm command takes a list of containers docker rm -vf ch6_ipc_producer ch6_ipc_consumer

There are obvious security implications to reusing the shared memory namespaces of containers. But this option is available if you need it. Sharing memory between containers is safer alternative to sharing memory with the host.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。