Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add memory and cpu limits for docker run #49

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/TestContainers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ module TestContainers
M.setVolumeMounts,
M.setRm,
M.setEnv,
M.setMemory,
M.setCpus,
M.withWorkingDirectory,
M.withNetwork,
M.withNetworkAlias,
Expand Down
26 changes: 26 additions & 0 deletions src/TestContainers/Docker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ module TestContainers.Docker
setSuffixedName,
setRandomName,
setCmd,
setMemory,
setCpus,
setVolumeMounts,
setRm,
setEnv,
Expand Down Expand Up @@ -276,6 +278,8 @@ data ContainerRequest = ContainerRequest
volumeMounts :: [(Text, Text)],
network :: Maybe (Either Network Text),
networkAlias :: Maybe Text,
cpus :: Maybe Text,
memory :: Maybe Text,
links :: [ContainerId],
naming :: NamingStrategy,
rmOnExit :: Bool,
Expand Down Expand Up @@ -311,6 +315,8 @@ containerRequest image =
volumeMounts = [],
network = Nothing,
networkAlias = Nothing,
memory = Nothing,
cpus = Nothing,
links = [],
rmOnExit = False,
readiness = mempty,
Expand Down Expand Up @@ -363,6 +369,22 @@ setCmd :: [Text] -> ContainerRequest -> ContainerRequest
setCmd newCmd req =
req {cmd = Just newCmd}

-- | Set the memory limit of a Docker container. This is equivalent to
-- invoking @docker run@ with the @--memory@ parameter.
--
-- @since x.x.x.x
setMemory :: Text -> ContainerRequest -> ContainerRequest
setMemory newMemory req =
req {memory = Just newMemory}

-- | Set the cpus limit of a Docker container. This is equivalent to
-- invoking @docker run@ with the @--cpus@ parameter.
--
-- @since x.x.x.x
setCpus :: Text -> ContainerRequest -> ContainerRequest
setCpus newCpus req =
req {cpus = Just newCpus}

-- | The volume mounts to link to Docker container. This is the equivalent
-- of passing the command on the @docker run -v@ invocation.
setVolumeMounts :: [(Text, Text)] -> ContainerRequest -> ContainerRequest
Expand Down Expand Up @@ -525,6 +547,8 @@ run request = do
volumeMounts,
network,
networkAlias,
memory,
cpus,
links,
rmOnExit,
readiness,
Expand Down Expand Up @@ -569,6 +593,8 @@ run request = do
++ [["--volume", src <> ":" <> dest] | (src, dest) <- volumeMounts]
++ [["--rm"] | rmOnExit]
++ [["--workdir", workdir] | Just workdir <- [workDirectory]]
++ [["--memory", value] | Just value <- [memory]]
++ [["--cpus", value] | Just value <- [cpus]]
++ [[tag]]
++ [command | Just command <- [cmd]]

Expand Down
Loading