#Copy files into docker image with PythonSDK

1 messages ยท Page 1 of 1 (latest)

noble gale
#

Hello again. Sorry if I disturb a bit, I have some doubts about the Python SDK. I'm running this script and publishing the image, but for some reason the final image does not contain the content of the mounted directory. I've been checking the doc and I didn't found a "copy" function or something similar, like Docker does. ```async def test_and_push():

async with dagger.Connection(dagger.Config(log_output=sys.stderr)) as client:

    # get reference to the local project
    src_id = await client.host().workdir(".").id()

    node_container = (

        client.container()

        # pull container
        .from_("node:16.3.0")

        # mount cloned repository into image
        .with_mounted_directory("/app", src_id)

        # set working directory
        .with_workdir("/app")
        
        # run command to install dependencies
        .exec(["npm", "install"])
        
        # run command to install dependencies
        .exec(["npm", "run", "test"])

    )


    await node_container.publish(f"{os.getenv('DOCKER_REGISTRY')}:latest") ```  

Then I can see the publish run ok, but when I check the "app/" dir there is nothing inside.

root@220c659d1f5e:/app# ls -lisa
total 8
3679265 4 drwxr-xr-x 2 root root 4096 Nov 14 11:45 .
3680966 4 drwxr-xr-x 1 root root 4096 Nov 14 14:35 ..

Thanks in advance.

mild vortex
#

Hi @noble gale ๐Ÿ‘‹ Thanks for the question! We have this issue open: https://github.com/dagger/dagger/issues/3673 to make what you're describing more intuitive. The short answer for why it's not working the way you're expecting it to is that mounts are not included in the actual container. That means we have to explicitly include those files.

Here's an example of how to accomplish that: https://github.com/kpenfound/dagger-demos/blob/main/python/multiplatform/main.py#L16-L24

After I create the container python from whatever base I want to use, I get a reference to that image's filesystem, and then say .with_directory("/src", src_id to combine the base fs with my directory. And then I reassign the containers fs with python.with_fs(multistage).

This can also be done by mounting the app to something like /tmp, and then doing an exec(["cp", "-r", "/tmp", "/app"]). Whichever feels more natural.

I'll add that this is only required for publishing. So if you have a pipeline that's just running npm run test you can have it exactly as your original example

noble gale
#

Hello @mild vortex , it works perfectlly, I was a bit confused, but with that example I could handle it, running now. Thanks a lot.

mild vortex
#

Awesome, glad I could help! ๐Ÿ˜