简体   繁体   English

使用Docker复制源文件时的最佳实践

[英]Best practices when copying source files with Docker

Lastly, I have been trying to improve my Docker skills, but there is something I'm still stuck when I have to deal with it. 最后,我一直在努力提高自己的Docker技能,但是当我不得不处理它时,仍然有一些困难。 Let's say I have an application divided into modules, each of them being its own Docker service. 假设我有一个分为模块的应用程序,每个模块都是自己的Docker服务。

All modules use the config stored under config/config.ini, but this config needs some preprocessing so what they use is the functions of config/config.py. 所有模块都使用存储在config / config.ini下的配置,但是此配置需要一些预处理,因此它们使用的是config / config.py的功能。

I have implemented that as: 我已经实现为:

  • Each Dockerfile of each module COPY the contents of the config/ folder into their own ( COPY ./config . ) 每个模块的每个Dockerfile都将config /文件夹的内容复制到它们自己的( COPY ./config .
  • Each module imports the config functions with: from config import ### 每个模块使用以下命令导入配置功能: from config import ###

This method does work, but it breaks the IDE as the config folder is not on the pythonpath so I lost some important functionality, and what is worse I don't think this is the cleanest way of achieving what I want. 此方法确实有效,但是由于config文件夹不在pythonpath上,所以它破坏了IDE,因此我失去了一些重要的功能,更糟糕的是,我认为这不是实现所需功能的最干净的方法。

Furthermore, this also happens with source files. 此外,源文件也会发生这种情况。 Normally in my python development, I would have just created a package called "APP" (see the folder structure below) and then I could have imported any source file in any folder of the package, but because each subfolder is a different Docker service I cannot make this project a python package. 通常,在我的python开发中,我会创建一个名为“ APP”的包(请参见下面的文件夹结构),然后可以将任何源文件导入到该包的任何文件夹中,但是因为每个子文件夹都是一个不同的Docker服务,无法将此项目设为python包。 For example, the tests are also dockerized so each test also copies the needed source files from other folders: 例如,还对测试进行了泊坞化,因此每个测试还从其他文件夹中复制了所需的源文件:

the file unit_test_main_module.py needs to test all the source files in main_module so it copies the files with the Dockerfile. 文件unit_test_main_module.py需要测试在所有的源文件main_module所以它的副本与Dockerfile的文件。

APP
 ├── unit_tests
     ├── Dockerfile
     └── unit_test_main_module.py
 ├── integration_tests
     ├── Dockerfile
     └── first_integration_test.py
 ├── config
     ├── config.ini
     └── config.py
 └── main_module
     ├── source_file_one.py
     └── source_file_two.py

Any advice? 有什么建议吗?

I would suggest having one Dockerfile per process . 我建议每个进程有一个Dockerfile。 A Docker image is basically a self-contained way to run a process. Docker映像基本上是一种运行过程的独立方式。 So if you only ever run one process, you only need one Dockerfile. 因此,如果只运行一个进程,则只需要一个Dockerfile。

You might want, perhaps, a second Dockerfile for a testing image. 您可能想要第二个Dockerfile作为测试映像。 But you don't need one per package. 但是您不需要每个包一个。

Assuming two, one for main application and one for tests, it would be in top directory. 假设有两个,一个用于主应用程序,一个用于测试,它将位于顶层目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM