Skip to content
Open
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
4 changes: 4 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ func (d *docker) createContainer(
ExtraHosts: cfg.ExtraHosts,
}

if cfg.ShmSize > 0 {
hostConfig.ShmSize = cfg.ShmSize
}

resp, err := d.client.ContainerCreate(ctx, containerConfig, hostConfig, nil, nil, cfg.ContainerName)
if err == nil {
return &resp, nil
Expand Down
25 changes: 25 additions & 0 deletions gnomock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,31 @@ func TestGnomock_withCustomImage(t *testing.T) {
require.NoError(t, gnomock.Stop(container))
}

func TestGnomock_withShmSize(t *testing.T) {
t.Parallel()

cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
require.NoError(t, err)

const expectedShmSize = int64(256 * 1024 * 1024) // 256MB

container, err := gnomock.StartCustom(
testutil.TestImage,
gnomock.DefaultTCP(testutil.GoodPort80),
gnomock.WithShmSize(expectedShmSize),
)
require.NoError(t, err)
require.NotNil(t, container)

// Inspect the container to verify ShmSize was set correctly
containerJSON, err := cli.ContainerInspect(context.Background(), container.DockerID())
require.NoError(t, err)
require.Equal(t, expectedShmSize, containerJSON.HostConfig.ShmSize)

require.NoError(t, gnomock.Stop(container))
require.NoError(t, cli.Close())
}

func initf(context.Context, *gnomock.Container) error {
return nil
}
Expand Down
14 changes: 14 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ func WithUser(user string) Option {
}
}

// WithShmSize sets the size of the shared memory (/dev/shm) in bytes. This is
// equivalent to the --shm-size flag in docker run. Useful for applications that
// need more shared memory than the default 64MB, such as PostgreSQL when using
// large shared_buffers. If size is 0 or negative, the Docker default of 64MB will be used.
func WithShmSize(size int64) Option {
return func(o *Options) {
o.ShmSize = size
}
}

// HealthcheckFunc defines a function to be used to determine container health.
// It receives a host and a port, and returns an error if the container is not
// ready, or nil when the container can be used. One example of HealthcheckFunc
Expand Down Expand Up @@ -336,6 +346,10 @@ type Options struct {
// This is equivalent to the --user flag in docker run.
User string `json:"user"`

// ShmSize specifies the size of the shared memory (/dev/shm) in bytes.
// This is equivalent to the --shm-size flag in docker run.
ShmSize int64 `json:"shm_size"`

ctx context.Context
init InitFunc
healthcheck HealthcheckFunc
Expand Down
Loading