Skip to content

Commit

Permalink
add memory and cpu limits for docker run
Browse files Browse the repository at this point in the history
  • Loading branch information
justinwoo authored and alexbiehl committed Apr 7, 2024
1 parent b5b31d3 commit 75ed7a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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

0 comments on commit 75ed7a3

Please sign in to comment.