Skip to content

Commit

Permalink
Add container platform to docker command
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com>
  • Loading branch information
pditommaso committed Mar 4, 2025
1 parent 15c6f3c commit 8580bd7
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ abstract class ContainerBuilder<V extends ContainerBuilder> {

protected boolean privileged

protected String platform

String getImage() { image }

V addRunOptions(String str) {
Expand Down Expand Up @@ -121,6 +123,11 @@ abstract class ContainerBuilder<V extends ContainerBuilder> {
return (V)this
}

V setPlatform(String platform) {
this.platform = platform
return (V)this
}

V setWorkDir( Path path ) {
this.workDir = path
return (V)this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ class DockerBuilder extends ContainerBuilder<DockerBuilder> {
if( memory )
result << "--memory ${memory} "

if( platform )
result << "--platform ${platform} "

if( tty )
result << '-t '

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ class PodmanBuilder extends ContainerBuilder<PodmanBuilder> {
result << "--memory ${memory} "
}

if( platform ) {
result << "--platform ${platform} "
}

// the name is after the user option so it has precedence over any options provided by the user
if ( name )
result << '--name ' << name << ' '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,9 @@ class BashWrapperBuilder {
if( this.containerCpuset )
builder.addRunOptions(containerCpuset)

if( this.containerPlatform )
builder.setPlatform(this.containerPlatform)

// export task work directory
builder.addEnv('NXF_TASK_WORKDIR')
// export the nextflow script debug variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import nextflow.container.ContainerConfig
import nextflow.executor.BashWrapperBuilder
import nextflow.executor.TaskArrayExecutor
import nextflow.util.MemoryUnit

/**
* Serializable task value object. Holds configuration values required to
* launch the task execution
Expand Down Expand Up @@ -88,6 +87,8 @@ class TaskBean implements Serializable, Cloneable {

String containerOptions

String containerPlatform

Map<String,Path> inputFiles

List<String> outputFiles
Expand Down Expand Up @@ -152,6 +153,7 @@ class TaskBean implements Serializable, Cloneable {
this.containerNative = task.isContainerNative()
this.containerEnabled = task.isContainerEnabled()
this.containerOptions = task.config.getContainerOptions()
this.containerPlatform = task.config.getContainerPlatform()
// secret management
this.secretNative = task.isSecretNative()
this.secretNames = task.config.getSecret()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import nextflow.util.CmdLineHelper
import nextflow.util.CmdLineOptionMap
import nextflow.util.Duration
import nextflow.util.MemoryUnit
import nextflow.util.SysHelper

/**
* Task local configuration properties
*
Expand Down Expand Up @@ -450,6 +452,11 @@ class TaskConfig extends LazyMap implements Cloneable {
return null
}

String getContainerPlatform() {
final result = getArchitecture()
return result ? result.getDockerArch() : SysHelper.DEFAULT_DOCKER_PLATFORM
}

def getClusterOptions() {
return get('clusterOptions')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,23 @@ class DockerBuilderTest extends Specification {
.runCommand == 'docker run -i -v "$NXF_TASK_WORKDIR":"$NXF_TASK_WORKDIR" -w "$NXF_TASK_WORKDIR" --device /dev/fuse --cap-add SYS_ADMIN fedora'
}

def 'test container platform' () {
expect:
new DockerBuilder('fedora')
.build()
.runCommand == 'docker run -i -v "$NXF_TASK_WORKDIR":"$NXF_TASK_WORKDIR" -w "$NXF_TASK_WORKDIR" fedora'

new DockerBuilder('fedora')
.setPlatform('amd64')
.build()
.runCommand == 'docker run -i --platform amd64 -v "$NXF_TASK_WORKDIR":"$NXF_TASK_WORKDIR" -w "$NXF_TASK_WORKDIR" fedora'

new DockerBuilder('fedora')
.setPlatform('linux/arm64')
.build()
.runCommand == 'docker run -i --platform linux/arm64 -v "$NXF_TASK_WORKDIR":"$NXF_TASK_WORKDIR" -w "$NXF_TASK_WORKDIR" fedora'
}

def 'test add mount'() {

when:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,21 @@ class PodmanBuilderTest extends Specification {
.runCommand == 'podman run -i -v "$NXF_TASK_WORKDIR":"$NXF_TASK_WORKDIR" -w "$NXF_TASK_WORKDIR" --cpu-shares 1024 --memory 400m fedora'

}

def 'test container platform' () {
expect:
new PodmanBuilder('fedora')
.build()
.runCommand == 'podman run -i -v "$NXF_TASK_WORKDIR":"$NXF_TASK_WORKDIR" -w "$NXF_TASK_WORKDIR" fedora'

new PodmanBuilder('fedora')
.setPlatform('amd64')
.build()
.runCommand == 'podman run -i -v "$NXF_TASK_WORKDIR":"$NXF_TASK_WORKDIR" -w "$NXF_TASK_WORKDIR" --platform amd64 fedora'

new PodmanBuilder('fedora')
.setPlatform('linux/arm64')
.build()
.runCommand == 'podman run -i -v "$NXF_TASK_WORKDIR":"$NXF_TASK_WORKDIR" -w "$NXF_TASK_WORKDIR" --platform linux/arm64 fedora'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ class BashWrapperBuilderTest extends Specification {
bash.getContainerCpus() >> null
bash.getContainerCpuset() >> null
bash.getContainerOptions() >> null
bash.getContainerPlatform() >> 'amd64'
bash.isSecretNative() >> false
bash.getSecretNames() >> []

Expand Down
3 changes: 3 additions & 0 deletions modules/nf-commons/src/main/nextflow/util/SysHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import nextflow.file.FileHelper
@CompileStatic
class SysHelper {

public static final String DEFAULT_DOCKER_PLATFORM = 'linux/amd64'

private static String DATE_FORMAT = 'dd-MMM-yyyy HH:mm'

/**
Expand Down Expand Up @@ -195,4 +197,5 @@ class SysHelper {

return buffer.toString()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ import nextflow.util.SysHelper
import nextflow.util.Threads
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import static nextflow.util.SysHelper.DEFAULT_DOCKER_PLATFORM

/**
* Wave client service
*
Expand All @@ -93,8 +95,6 @@ class WaveClient {

public static final List<String> DEFAULT_CONDA_CHANNELS = ['conda-forge','bioconda']

private static final String DEFAULT_DOCKER_PLATFORM = 'linux/amd64'

final private HttpClient httpClient

final private WaveConfig config
Expand Down Expand Up @@ -473,8 +473,7 @@ class WaveClient {
// get the bundle
final bundle = task.getModuleBundle()
// get the architecture
final arch = task.config.getArchitecture()
final dockerArch = arch? arch.dockerArch : DEFAULT_DOCKER_PLATFORM
final dockerArch = task.config.getContainerPlatform()
// compose the request attributes
def attrs = new HashMap<String,String>()
attrs.container = containerImage
Expand Down

0 comments on commit 8580bd7

Please sign in to comment.