QoL issues with docker + vscode

there’s a lot of things about my docker + vscode setup that are really annoying to me. i figured out 2 weeks ago how to get a remote container running, and that has already been a big improvement for me from using Atom before. but i wish i could rebuild the docker container with the Rebuild Container dialog without having to delete all the .build directories because there really are a lot of them, especially if you are like me and use swift package edit a lot and have a lot of nested SPM projects at a time.

error checking context: 'can't stat '.build/x86_64-unknown-linux-gnu/debug/ModuleCache''.

and i rebuild the image over and over again because if i try to expose ports with the devcontainer.json it doesn’t work unless i rebuild the whole image.

and then there is @johannesweiss ’s incremental build trick which if i could learn how to do it would save me a lot of time because swift-nio-ssl takes such a long time to compile but i don’t know where to put the --mount=type=cache,target=/code/.build in the devcontainer.json or in the dockerfile or what /code/.build is supposed to be because the directory layout is different in the host and in the container, and when i try --mount=type=cache it says cache is not a mount type:

[11350 ms] Start: Run: docker run --sig-proxy=false -a STDOUT -a STDERR --mount type=bind,source=/home/klossy/host-project,target=/workspaces/container-project --mount type=volume,src=vscode,dst=/vscode -l devcontainer.local_folder=/home/klossy/host-project --cap-add=NET_ADMIN -p 80:80 -p 443:443 --mount=type=cache,target=/workspaces/container-project/.build --entrypoint /bin/sh vsc-swiftinit.org-804c4a4ab43ecd3d8f6619cceea10125 -c echo Container started
WARNING: Error loading config file: /home/klossy/.docker/config.json: open /home/klossy/.docker/config.json: permission denied
docker: Error response from daemon: invalid mount config for type "cache": mount type unknown.

so i really wish there was a guide for how to do this because i swear i remember there was one but there is nothing on Swift.org - Installing Swift and using with IDEs anymore and the links just go backwards to swift.org or the vscode website.

What version of Docker do you have? The --mount=type=cache,... requires BuildKit but that's now the default. Officially, you're supposed to stick # syntax=docker/dockerfile:1.2 int the first line of your Dockerfile but it definitely works.

This Dockerfile

# syntax=docker/dockerfile:1.2
FROM ubuntu
RUN --mount=type=cache,target=/code/.build echo -e 'HELLO\nHELLO\nHELLO\n' > /code/.build/foo
RUN --mount=type=cache,target=/code/.build cat /code/.build/foo

run with docker build --progress=plain . gives me

[...]
#7 [stage-0 2/3] RUN --mount=type=cache,target=/code/.build echo -e 'HELLO\nHELLO\nHELLO\nHELLO\n' > /code/.build/foo
#7 sha256:08bbbee8f4ccf79923c15f0a9289e4c66a2754e30b67c8b161908d68c5499032
#7 DONE 0.1s

#8 [stage-0 3/3] RUN --mount=type=cache,target=/code/.build cat /code/.build/foo
#8 sha256:516c3f9cb153f4b3574906ef4bf39498cdd3719983da984240dae4ef31c3e639
#8 0.272 -e HELLO
#8 0.272 HELLO
#8 0.272 HELLO
#8 0.272 HELLO
#8 0.272 
#8 DONE 0.3s
[...]

The target=/path needs to be where your .build directory in the container lives.

I can't help you with devcontainer.json things unfortunately.

1 Like

you were right, the problem was i was using an old version of docker that came with apt install docker on ubuntu 20.04, then i upgraded docker to 20.10.17 everything just worked!

1 Like