Skip to content

SFTP

Not available until the next release main

Introduction

The Testcontainers module for SFTP provides an easy-to-use SFTP server built on OpenSSH. It is ideal for testing file-transfer workflows without connecting to a real remote server.

Adding this module to your project dependencies

Please run the following command to add the SFTP module to your Go dependencies:

go get github.com/testcontainers/testcontainers-go/modules/sftp

Usage example

ctx := context.Background()

sftpContainer, err := sftp.Run(ctx, "atmoz/sftp:latest",
    sftp.WithUser("alice", "secret"),
)
defer func() {
    if err := testcontainers.TerminateContainer(sftpContainer); err != nil {
        log.Printf("failed to terminate container: %s", err)
    }
}()
if err != nil {
    log.Printf("failed to start container: %s", err)
    return
}

Module Reference

Run function

  • Not available until the next release main

Info

The Run function requires at least one user to be configured via WithUser, otherwise it returns an error.

The SFTP module exposes one entrypoint function to create the SFTP container, and this function receives three parameters:

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error)
  • context.Context, the Go context.
  • string, the Docker image to use.
  • testcontainers.ContainerCustomizer, a variadic argument for passing options.

Image

Use the second argument in the Run function to set a valid Docker image. In example: Run(context.Background(), "atmoz/sftp:latest", sftp.WithUser("alice", "secret")).

Container Options

When starting the SFTP container, you can pass options in a variadic way to configure it.

WithUser

  • Not available until the next release main

WithUser(username, password string) adds an SFTP user. At least one user is required. Multiple calls accumulate users.

sftpContainer, err := sftp.Run(ctx, "atmoz/sftp:latest",
    sftp.WithUser("alice", "secret"),
    sftp.WithUser("bob", "password"),
)

The following options are exposed by the testcontainers package.

Basic Options

Lifecycle Options

Files & Mounts Options

Build Options

Logging Options

Image Options

Networking Options

Advanced Options

Experimental Options

Container Methods

The SFTP container exposes the following methods:

Address

  • Not available until the next release main

Address(ctx context.Context) (string, error) returns the host:port address of the SFTP server. Use this value directly with an SSH or SFTP client dial call.

addr, err := sftpContainer.Address(ctx)
// addr is e.g. "localhost:49160"