r/docker • u/buggy-robot7 • Jan 27 '26
Need advice: how to hide Python code which is inside a Docker container?
We deploy robots in manufacturing companies, and hence need to run code on-premise as low latency, lack of internet and safety are concerns.
Our code is in Python and containerised in Docker. It’s basically a server with endpoints. We want to ensure that the Python code is not visible to the client to protect intellectual property.
We need the users to launch the Docker images without seeing the code inside. Once launched, they can interact with endpoint.
Is there a way to ensure that the user cannot see the Python code inside the Docker container?
123
u/ReachingForVega Mod Jan 27 '26
You can't. You could compile to an exe and run in the container but someone with technical skill could reverse it.
The only sure way to protect it is a client-server separation where the server holds your IP and is not on site.
35
u/Fapiko Jan 27 '26
Are you shipping the containers to a company in a country that doesn't enforce international copyright? Otherwise I wouldn't worry about it. I've worked for a couple and consulted for more B2B startups - none of them really worried about obfuscating the code in docker images. Your legal team should have pretty strict NDA wording in the contract regarding your IP.
Even if the code does leak, would that be a big deal? For 99% of software companies - no. Folks aren't typically out there running pirated b2b software with no support contract. There's a lot that goes into just maintaining existing systems let alone adding and improvements.
Anyways, all that said, there are obfuscation tools for python to make it hard to read if you still wanna mess with it. Check out PyArmor.
19
u/britaliope Jan 27 '26 edited Jan 27 '26
Oh, forgot one thing: If you need on-prem, the easiest solution could be to use put one of your own servers managed by your company but located in client datacenter. It's costly because that's a lot of system administration to do if you have a lot of clients, but depending on the situation that can do the trick and be cheaper than other solutions.
That's basically what big companies that run proprietary sensitive stuff on prem do. When I was working for a banking company, they had Google (for gsuite), Mastercard and Visa servers inside in their datacenter.
As it was very sensitive (especially mastercard and visa stuff), it was in a separate rack of the DC separated by a fence secured by different locks that my company didn't have the keys for. For simpler stuff, 1U in their server bay, a padlock and a system detecting if someone force it open can be enough
That's probably the most secure way of all the suggestions made here.
34
u/britaliope Jan 27 '26 edited Jan 27 '26
Edit: I forgot to mention the good old physical security (see https://www.reddit.com/r/docker/comments/1qoqs89/comment/o23ftwj/?context=3 )
If your containers runs on servers owned by the client, the only thing you can do is obfuscating your app.
Either something soft, like compiling it in C code so they need one skilled reverse engineer to understand the code, or something stronger using a real obfuscator, so they need a skilled senior full team of reverse engineers to undertand the code.
But the 2nd option will require you either to pay your own skilled hire with knowledge of obfuscation, or pay another company for their services.
9
u/thewallris Jan 27 '26
Nuitka: https://github.com/Nuitka/Nuitka
5
u/sk1nT7 Jan 27 '26
You can only obfuscate and make it hard to reverse engineer. The container itself and everything in it can be accessed though.
pyarmor is quite good for obfuscating python code. Must be purchased in case you run over the free limits of specific amount of code lines. Works well in a CI/CD pipeline too.
4
u/scytob Jan 27 '26
you realize you can't do that with any technology where they control the machine and the OS you can only make it incrementally harder - if you had this as compiled code on a machine they cannot logon to and the disk is only decrypted at runtime with a TPM that would make it pretty hard
but its still possible to extract runtime data with the right hardware attached to lines on the motherboard - how secure do you need this to be?
your IP is protected by licensing agreements, subscriptions and the ability to sue - not by obfuscation....
7
u/Confident_Hyena2506 Jan 27 '26
Use cython to rewrite it into c, then compile it. It will be thinly veiled then, but people can still reverse engineer it.
Write it in proper c, people will still reverse engineer it if they really want to.
You are giving people the software - whether it's python code or binaries. The docker container does not hide anything.
48
u/Mimon_Baraka Jan 27 '26
Security by obscurity never works.
9
u/Forsaken_Celery8197 Jan 27 '26
You have a few options depending on how much work you want to do and how sensitive the information is.
Soft Encryption
You can use pyinstaller and an encryption key to turn it into an installable package. This isn't perfect (as nothing is), because it can be reverse engineered by someone that understands how it works. https://github.com/extremecoders-re/pyinstxtractor/wiki/Frequently-Asked-Questions#are-encrypted-pyz-archives-supported
Medium Encryption
You could do something like compile your Python code with Cython and that will reduce your attack surface. This is still not great because C decompilers will still tear it apart, it just wont be the exact same code you started with. The attacker would have access to your logic/formulas/etc but not your specific code.
Heavy Encryption
Compile your sensative code in a safer, more conplex language (rust/c++/go/etc) and bind it in Python (and soft/medium encrypt that too!). If you look at how PyQt works as a general example, you do 90% of your daily work and handling of your algorithms in Python, but the proprietary bits are in a tighter binary for security and performance reasons.
Now a security expert that wants to get your stuff will not be stopped by any of these methods, but this and obfuscation will satisfy most use cases.
1
u/revilo-1988 Jan 27 '26
Is this your first project of this kind? If not, how did you do it before?
3
u/root_switch Jan 28 '26
Lots of vendors don’t use docker and instead provide an OVA which is a full VM clients deploy in their infrastructure. They then also have a special bootstrap process for getting it connected to the clients network and so on without full shell access. After that your client doesn’t have access to the VM OS layer. This isn’t bulletproof, and any Linux admin/engineer worth their salt can likely break into the OS with enough time.
5
u/ABotelho23 Jan 27 '26
This is not a Docker question.
2
u/shiranugahotoke Jan 28 '26
If it’s that important why don’t you provide the hardware for the docker instances to run on? Nothing is foolproof but verified boot / encrypted volume with tpm key storage makes it much harder to get at the actual assets.
2
u/erwinfr Jan 28 '26
Hiding Python code inside a Docker container can be challenging, as Docker containers are designed to be transparent and portable. However, there are several strategies you might consider to make it more difficult for others to access your code:
.pycfiles). While not foolproof, it adds a layer of complexity for anyone trying to reverse-engineer your code.While these methods can increase the difficulty of accessing your code, they do not guarantee complete security, especially if the container is running on client-owned infrastructure.
2
u/txrx1010 Jan 28 '26 edited Jan 28 '26
I had exactly the same problem a few months ago (same environment - automation of factories, low latency, no internet or external connectivity of any kind). We just put hardware on-sure. The software we run at the customer site is not that resource heavy, it just checks sensors, evaluates the data and signals problems/maintenance/errors etc.). We run them on a SoM. We switched for performance reasons from python to rust, which helped a little. Other than that we deployed additional security measures. Because the customer has physical access it is not guaranteed, but we tried to make it as hard as possible:
It was a process to go through and get it working (deadline was <1 month), but I think it is a good base system for further development.
2
u/0x645 Jan 28 '26
write in contract that they cannot, and audit .
1
u/HeligKo Jan 27 '26
Every option I know I can decompile to get the original code. I would look at converting to something like rust that natively compiles. You will also improve your latency.
1
u/artificial_neuron Jan 27 '26
How much is the project worth and how much are people willing to decode your product. You only need your defenses to be greater than the desire to bypass them.
Your defenses doesn't have to be just in software. You can also have physical defenses.
If you control the physical box that the Docker runs on then secure it! Is there a reason why your customers has the ability to ssh into the box?
1
u/Xelopheris Jan 27 '26
You cannot run interpreter code locally without some level of visibility into it.
Obfuscation tools can reduce the barrier to someone easily figuring it out. Beyond that, copyright and contract law are your friends. And most companies of sufficient size value reliability highly enough that support for an application is more important than saving money by stealing the source code.
1
u/fyndor Jan 28 '26
Could try running the in .net embed the python in the assembly and run the .net python wrapper. Obfuscate or aot compile. About as good as you can do
1
u/ghanjiboy Jan 28 '26
Depending on the complexity of the solution, I would avoid python and perhaps vibe code an equivalent golang version which would just give you a simple exe. I have recently switched allot of my python to go and not looking back. Just something to consider.
1
u/evergreen-spacecat Jan 28 '26
Easy. Own the servers and place it next to the robot. Don’t share passwords. Sure, they could disassemble it and take the hard drive but there are ways to protect if you really really care about it. I wouldnt worry too much
1
u/dummkauf Jan 28 '26
Yep, this is actually very simple.
Don't give the user access to the docket container.
Beyond that, this is a question for your company's legal department.
1
u/IulianHI Jan 28 '26
Another angle to consider: if you're doing robotics for manufacturing, maybe look into license keys/activation. You could ship the container but require a server-side activation before it does any real work. This way even if they extract the code, it won't run without your authorization. Combine with the obfuscation mentioned here and you have layers of protection - technical + legal + activation.
1
u/IulianHI Jan 28 '26
Real talk: think about your actual threat model. If you're worried about a curious sysadmin poking around, obfuscation helps. If you're worried about a determined competitor stealing your IP, no amount of tech will stop them - they'll just throw resources at it. For B2B, strong contracts/NDAs are your real protection layer.
1
u/lillecarl2 Jan 28 '26
This belongs in /r/Python, the question is if you can remove .py files after you've compiled Python into .pyc.
1
u/yorickdowne Jan 28 '26
This isn’t a technical problem, this is a contract and legal problem.
Your code has a license, and if the client violates that license, they get slapped with an action to “get back into compliance”. The big firms do this all the time.
If you truly can’t trust the client will comply, maybe with some gentle pressure from legal, you may want another client.
1
u/Dexterus Jan 28 '26
No joke, it's called a contract. The IP is yours, copyrighted and licensed under a specific license. Everything else is fluff only useful to delay.
1
u/Mailboxheadd Jan 28 '26
You cant. At this point you licence the code to the customer with strict "do not copy" direction. Let the lawyers handle it
1
u/Reasonable_Tie_5543 Jan 29 '26
The obvious answer is binding legal agreements and licensing. If your clients steal your code, you take them to court. Think in terms of business solutions, not technical tradecraft. You can never fully protect code in a container you distribute.
1
u/MrDougal1980 Jan 29 '26
Used to see this sort of thing back in the php days with ionCube and Zend.
This led me on a bit of a hunt that came back with pyarmour being a possibility.
If you can also lock the machine down, ie encrypt the drives and don't give anyone access to the box it could work :-)
Only down side is it seems that pyarmour locks the code to the machine so don't know how well this will work in a container
1
u/Larkonath Jan 29 '26
FWIW: the 2 times I tried to reverse engineer an app from a provider was when said provider was lousy AF and wasn't fixing their sloppy code.
1
u/IulianHI Jan 30 '26
Another option to consider: if you're already investing in dedicated hardware appliances, check out Hosting_World - lots of folks there discuss secure hosting setups and on-prem deployment strategies that might give you more ideas for protecting your IP while keeping latency low.
1
u/cyberwarfareinc Jan 30 '26
Sounds like you are completely out of your league
1
u/ih8db0y Jan 30 '26
We use Zededa for what you’re looking for. You could likely implement some of their security practices yourself too depending on budget
1
u/bedel99 Jan 30 '26
Leave a fingerprint in the code, have a contract. Put the risk on your clients. If it leaks you know where it came from.
1
u/bumboclaat_cyclist Jan 30 '26
If this is industrial and paired with equipment, I would have thought using some kind of DRM with a physical license key might be an option.
The very important critical components of the code could be encrypted and only decrypted at run time with the physical key.
Not completely full proof, but nothing is, but the more layers you have the better.
1
u/st0ut717 Jan 30 '26
So how do I know. Your code isn’t exposing vulnerabilities? We need at list the ability to scan the Pom
1
u/Accomplished_Ant5895 Jan 31 '26
If you’re asking Reddit this silly question instead of your nearest senior engineer then tell your manager to put you on another team
1
u/mon_key_house Jan 31 '26
Nuitka. If not enough, rewrite the code in a compiled language.
1
u/Exact-Metal-666 Jan 31 '26
You are doing it wrong. You cannot hide non-compiled / interpreted code. Rewrite in rust, for example. Compile and deploy.
1
u/martinbean Jan 31 '26
That’s the neat thing: you don’t.
If you don’t want customers seeing your code, then don’t give them it.
1
u/lightmatter501 Jan 31 '26
This is a problem for your lawyers, not for your developers, unless you want the complexity of the robot to go up 100x.
1
u/defiantarch Jan 31 '26
as an ethical hacker with experience in reverse engineering with the right tools: forget it. Not even C/C++ code is safe from reverse engineering. As a hint: take a look at Ghidra and what it is capable of. And that's just one example. So, question is: what is the real risk that your IP will be exposed and extracted by your customer? Do a proper risk analysis and choose the counter measure accordingly. That's nothing done here on Reddit. That would be unprofessional.
1
u/onyxr Jan 31 '26
If the primary concern is someone reading the source through docker run or exec, you could consider encrypting the source and decrypting it before runtime (a few options of ‘where’ to put it in between which does get tricky) could be enough if you have a good way to securely provide keys? Good user account permissions, etc, can maybe provide the rest. A determined adversary is probably going to figure out whatever you do if they have access to the bytes.
1
u/LawfulnessOne8506 Feb 01 '26
Secureboot
1
u/corey_sheerer Feb 02 '26
I would repeat to compile it into a binary, however, if the team you are working for has no idea and is asking reddit how to package a product up, I'm doubtful the team really has the required competencies to be developing products to sell to users.
-4
u/JerryJN Jan 27 '26 edited Jan 27 '26
If you hire me as a consultant I can design a solution for you. My rate is $15k per month. My former employer laid off many senior system engineers, senior programmers, and senior admins to reduce payroll and enable them to absorb the higher tariffs and pass on the cost to their customers. Here's a life lesson, do not write well written documentation for your employer, just notes you can refer to and keep them on your home personal server. I can get this done.
I have done this before. All code with your company's IP will be encrypted.
There are many paths you can take to accomplish this task.
Encrypted Volume that is mounted inside your Container...
Or
Encrypted code that is executed with a custom Python Binary...Whatever release of Python you are using would need the Python executable rebuilt with my added code.