Programmatically write on docker writeable layer

Can we programmatically access the docker container writeable layer?Docker run command allows to run command in the container but can we access this layer as a stream and programmatically write contents on it.

Hello Tahir,

You can write a script or program and copy it inside your container(on the writable layer).
Then, you can execute your script in your container.

Best Regards,
Fouscou

Thanks. I understand that copy into the container will work. But my question more towards accessing the writeable layer using API like storage drivers access the docker union file system programatically. Can we similarly access the writeable layer instead of Docker run or copy commands?

Why would you want to manipulate a immutable layer?
Doesnt’ this kind of beat the idea of consistancy and mess with the expected sha256 checksum for a layer?

What is wrong about writing a Dockerfile to generate the image with the layers you need?

You can always export an image (as in docker save) and untar it’s content to get files you need from an existing image and the layers it is composed from and reuse those files in your own Dockerfile…

My question is not towards immutable lower level layer but towards container writeable layer. I do want to make any changes in lower readonly but only at the container writeable layer. Using Docker copy or mount you can copy files to container or you can create a custom docker file with the required files. I do not want to make new Dockerfile but want to use existing image. Is there a way to access this thin writeable layer as a file system. For example, a pseudo code:

containerFS := docker.getWriteableLayer(“container1”)
containerFS.CreateDirectory("/home/newDir")
containerFS.RemoveFile("/home/file.txt")

Thanks

You can leverage exec to perform actions on a running container, check https://docs.docker.com/engine/api/v1.40/#operation/ContainerExec for usage. Docker provides sdk’s for a wide range of programming languages.

1 Like