Skip to content

Commit c4f3392

Browse files
committed
fix: use tar to copy paths into code volume
A simple `docker cp x/y` will fail because `x` doesn't exist. The documentation is vague, but it appears we need to pre-create parents in order to copy nested paths. To do that would require starting/running the container, which we don't have reason to do yet. To avoid it, we can switch to using a tar archive as input to `docker cp -`. This will create create any parents as necessary.
1 parent 135cf6b commit c4f3392

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/Restyler/Config/CopyFiles.hs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import Restyler.Prelude
1717

1818
import Autodocodec hiding ((.=))
1919
import OptEnvConf hiding (env)
20-
import Path ((</>))
2120
import Restyler.CodeVolume
2221
import Restyler.Config.Glob
2322
import Restyler.Config.RemoteFile
@@ -110,9 +109,6 @@ copyCodeFiles remoteFiles paths vol = \case
110109
-- docker cp . container:/path
111110
dockerCp "." $ vol.container.unwrap <> ":" <> toFilePath vol.path.unwrap
112111

113-
dockerCpAll = traverse_ $ \p -> do
114-
-- docker cp foo/bar.x container:/path/foo/bar.x
115-
dockerCp (toFilePath p)
116-
$ vol.container.unwrap
117-
<> ":"
118-
<> toFilePath (vol.path.unwrap </> p)
112+
dockerCpAll ps =
113+
-- tar -cf - ps | docker cp - container:/path
114+
dockerCpTar ps $ vol.container.unwrap <> ":" <> toFilePath vol.path.unwrap

src/Restyler/Monad/Docker.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Monad m => MonadDocker m where
3131
dockerVolumeCreate :: HasCallStack => String -> m ()
3232
dockerVolumeRm :: HasCallStack => String -> m ()
3333
dockerCp :: String -> String -> m ()
34+
dockerCpTar :: [Path Rel File] -> String -> m ()
3435
dockerRm :: String -> m ()
3536

3637
-- | An instance that invokes the real @docker@
@@ -59,6 +60,19 @@ instance
5960
dockerVolumeCreate name = runDocker_ ["volume", "create", name]
6061
dockerVolumeRm name = runDocker_ ["volume", "rm", name]
6162
dockerCp src dst = runDocker_ ["cp", "--quiet", src, dst]
63+
dockerCpTar ps dst = do
64+
let
65+
tArgs :: [String]
66+
tArgs = ["-cf", "-"] <> map toFilePath ps
67+
68+
dArgs :: [String]
69+
dArgs = ["cp", "--quiet", "-", dst]
70+
71+
logProc "tar" tArgs
72+
withProcessWait_ (setStdout createPipe $ proc "tar" tArgs) $ \p -> do
73+
logProc "docker" dArgs
74+
runProcess_ $ setStdin (useHandleClose $ getStdout p) $ proc "docker" dArgs
75+
6276
dockerRm name = runDocker_ ["rm", name]
6377

6478
runDocker
@@ -103,4 +117,5 @@ instance Monad m => MonadDocker (NullDocker m) where
103117
dockerVolumeCreate _ = pure ()
104118
dockerVolumeRm _ = pure ()
105119
dockerCp _ _ = pure ()
120+
dockerCpTar _ _ = pure ()
106121
dockerRm _ = pure ()

0 commit comments

Comments
 (0)