Can We Remove Shell Access and User-Level Commands from a Docker Image After Building to Prevent Tampering?

Hi everyone,

I have a question regarding Docker image security and would appreciate some guidance.

Is it possible to remove or disable user-level commands, shell access, package managers, and other interactive utilities from a Docker image after the build process, while ensuring that the application continues to run normally?

My thought is that if the final image does not provide access to commands such as sh, bash, apt, apk, or other tools, it could make it more difficult for someone using the container to inspect or tamper with application files, source code, environment configurations, or other internal components.

Ideally, the container would only expose what is necessary to execute the application, without providing an interactive environment for the user.

I am not very proficient in Docker, so this is just an idea I am exploring rather than a proposed solution. I would like to understand:

  1. Is this technically possible without affecting application functionality?

  2. Are there established Docker practices or base images designed for this purpose (such as Distroless)?

  3. Would removing these utilities actually improve source-code and configuration protection, or can someone with host-level Docker access still bypass these restrictions?

  4. Are there any better approaches to achieving this?

I would appreciate any suggestions, practical experiences, or recommendations.

Thanks in advance!

Seems like you have not heard about hardened images that aim for zero vulnerabilities and only container the necessary.

Depends on the application and what it requires to work properly.

Distroless is good starting point, but hardened image are the real deal

You can find the Docker hardened image catalogue at https://dhi.io. Every repo should at least have a dev and final app tag that can be pulled and used free of charge without an additional subscription.

Though, using dhi.io images requires a login with docker login dhi.io before images can be pulled. You can use your DockerHub credentials.

Why would removing tools that are irrelvant for an application improve the source-code? It would reduce the attack surface, as only required tools and dependencies are available in the image. So no shell, no tools to download stuff from the internet is usually a good starting point.

There is no way to protect yourself from someone that can control the host docker engine and knows how docker works (thanks to ai even those who don’t will get it working). You can always create a container that mounts the process and mount namespace of another container and look around. Docker Desktop even comes with a convenient subcommand called.debug for this.

If it’s about protecting the running container from people that access the service: Use hardend images, run a read-only container filesystem, drop unnecessary capabilities - never run privileged containers.

If it’s about protecting the docker engine from users that have access to the api: there is a bunch of stuff you can do, like running rootless docker, instead of (the normal rootful) docker. protect access to the docker api with mutual tls certificat verification. Use an authz plugin like Open Policy Agent. We had a couple of topics about this in the past, so the forum search should yield some useful posts.