testcontainers / testcontainers-dotnet

A library to support tests with throwaway instances of Docker containers for all compatible .NET Standard versions.
https://dotnet.testcontainers.org
MIT License
3.76k stars 271 forks source link

[Bug]: Container.Name always starts with forward slash #1114

Closed mu88 closed 7 months ago

mu88 commented 7 months ago

Testcontainers version

3.7.0

Using the latest Testcontainers version?

Yes

Host OS

Windows 10

Host arch

x64

.NET version

8.0.101

Docker version

Client:
 Cloud integration: v1.0.35+desktop.10
 Version:           25.0.2
 API version:       1.44
 Go version:        go1.21.6
 Git commit:        29cf629
 Built:             Thu Feb  1 00:24:09 2024
 OS/Arch:           windows/amd64
 Context:           default

Server: Docker Desktop 4.27.1 (136059)
 Engine:
  Version:          25.0.2
  API version:      1.44 (minimum version 1.24)
  Go version:       go1.21.6
  Git commit:       fce6e0c
  Built:            Thu Feb  1 00:23:17 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.28
  GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Docker info

Client:
 Version:    25.0.2
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.12.1-desktop.4
    Path:     C:\Program Files\Docker\cli-plugins\docker-buildx.exe
  compose: Docker Compose (Docker Inc.)
    Version:  v2.24.3-desktop.1
    Path:     C:\Program Files\Docker\cli-plugins\docker-compose.exe
  debug: Get a shell into any image or container. (Docker Inc.)
    Version:  0.0.22
    Path:     C:\Program Files\Docker\cli-plugins\docker-debug.exe
  dev: Docker Dev Environments (Docker Inc.)
    Version:  v0.1.0
    Path:     C:\Program Files\Docker\cli-plugins\docker-dev.exe
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.21
    Path:     C:\Program Files\Docker\cli-plugins\docker-extension.exe
  feedback: Provide feedback, right in your terminal! (Docker Inc.)
    Version:  v1.0.4
    Path:     C:\Program Files\Docker\cli-plugins\docker-feedback.exe
  init: Creates Docker-related starter files for your project (Docker Inc.)
    Version:  v1.0.0
    Path:     C:\Program Files\Docker\cli-plugins\docker-init.exe
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc.)
    Version:  0.6.0
    Path:     C:\Program Files\Docker\cli-plugins\docker-sbom.exe
  scout: Docker Scout (Docker Inc.)
    Version:  v1.3.0
    Path:     C:\Program Files\Docker\cli-plugins\docker-scout.exe

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 18
 Server Version: 25.0.2
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  seccomp
   Profile: unconfined
  cgroupns
 Kernel Version: 5.15.133.1-microsoft-standard-WSL2
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 24
 Total Memory: 4.802GiB
 Name: docker-desktop
 ID: 7ecf11cf-d186-4888-b03d-4934292531d1
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5555
  127.0.0.0/8
 Registry Mirrors:
  https://docker.company.ch/
 Live Restore Enabled: false

What happened?

When starting a container, the Name property always starts with a / which is not present when running docker ps: image

Relevant log output

No response

Additional information

At the moment, the following test fails:

using FluentAssertions;
using Testcontainers.PostgreSql;

namespace TestProject1;

public class UnitTest
{
    [Fact]
    public async Task ContainerName_ShouldNotStartWithForwardSlash()
    {
        var postgreSqlContainer = new PostgreSqlBuilder().Build();

        await postgreSqlContainer.StartAsync();

        postgreSqlContainer.Name.Should().NotStartWith("/"); // <= fails, the current value is `/musing_khayyam`
    }
}

I'm happy to provide a fix if it's a smaller issue and you can help me walk in the right direction.

HofmeisterAn commented 7 months ago

This is not an issue. This is how Docker stores the container name internally. See the following commands:

docker run --name foo httpd
docker inspect --format '{{.Name}}' foo

We do not modify any data here; we simply forward it. I do not want to change it TBH.

mu88 commented 7 months ago

Oh wow, I didn't know that! 😲 Learning never stops...

Could you image introducing a dedicated property that corresponds to the value one sees via docker -ps? If not it's also fine, just close the issue :)

HofmeisterAn commented 7 months ago

As mentioned, I have no intention to change it. We do not recommend using the container name (setting it through the builder's API) anyway. In case you need to set up a container-to-container communication, you can use WithNetworkAliases(string). Or is there any other use case I am missing?

mu88 commented 7 months ago

I will try whether WithNetworkAliases helps in my use case 💪🏻