Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting docker login error when creating container from local image #183

Closed
paulvanbladel opened this issue Jan 11, 2020 · 14 comments
Closed
Labels
bug Something isn't working
Milestone

Comments

@paulvanbladel
Copy link

I have a regular docker image build locally with docker (i'm using docker on windows 10).
So, following works perfect:y:

docker build --pull -f Subsystem1.Api.Dockerfile -t subsystem1image .
docker run --name subsystem1 --rm -it -p 9999:80 subsystem1image

If I want to create now a container from the image with dotnet-testcontainers

var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
             
              .WithImage("subsystem1image")
              .WithName("subsystem1xxx")
              .WithPortBinding(8081, 80)
              .WithWaitStrategy(Wait.UntilPortsAreAvailable(80));

            using (var testcontainer = testcontainersBuilder.Build())
            {
                await testcontainer.StartAsync();
                .....

I get

  

Any idea?
Note that creating a container from an image on docker hub (eg nginx) works as expected.
I'm not expecting that i need to login because I'm not pulling the image from a remote nor local docker repo, just from the local images.

@paulvanbladel paulvanbladel changed the title Getting docker Getting docker login error when creating container from local image Jan 11, 2020
@HofmeisterAn
Copy link
Collaborator

HofmeisterAn commented Jan 11, 2020

@paulvanbladel Could you add the error message? Let me see if I can recreate the issue. What is the base image of Subsystem1.Api.Dockerfile?

@paulvanbladel
Copy link
Author

No access to PC right now, but the error message is about docker login.
The docker file is a regular docker file for a . Net project and works as expected with
docker run --name subsystem1 --rm -it -p 9999:80 subsystem1image
So the base image is a public image from Microsofton docker hub.
I will post the full error details when I have PC access.
Cheerd

@HofmeisterAn
Copy link
Collaborator

HofmeisterAn commented Jan 11, 2020

I did some local tests and had no issues. Is your Docker base image based on Windows? In this case Wait.UntilPortsAreAvailable(80) will not work. It's a Unix implementation, you need to write your own implementation for Windows. E. g. with PowerShell and Test-NetConnection.

This was my local test:

var imageFromDockerfile = await new ImageFromDockerfileBuilder()
  .WithName("alpine:custom")
  .WithDockerfile("Dockerfile")
  .WithDockerfileDirectory(".")
  .WithDeleteIfExists(true)
  .Build();

var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  .WithImage(imageFromDockerfile);

using (var testcontainer = testcontainersBuilder.Build())
{
  await testcontainer.StartAsync();
}

I can only imagine that the image does not exist locally and must be pulled from a Docker registry that it not authenticated. Until now, DockerImageOperations.CreateAsync does not use any authentication, because you can always use docker login. Of course, we can change this behavior.

@paulvanbladel
Copy link
Author

No it's a Linux based image, but I am running it on Windows 10.
I will check the behavior on a Linux host.
Thanks for your continuous support

@HofmeisterAn
Copy link
Collaborator

I don't think changing the host system will solve the problem (if it exists). Unix and Windows should not behave differently in this case.

@paulvanbladel
Copy link
Author

Hi André,
Sorry for the delay.
The exact error message is

Message: 
    Docker.DotNet.DockerApiException : Docker API responded with status code=NotFound, response={"message":"pull access denied for subsystem1image, repository does not exist or may require 'docker login': denied: requested access to the resource is denied"}
    
  Stack Trace: 
    DockerClient.HandleIfErrorResponseAsync(HttpStatusCode statusCode, HttpResponseMessage response, IEnumerable`1 handlers)
    DockerClient.MakeRequestForStreamAsync(IEnumerable`1 errorHandlers, HttpMethod method, String path, IQueryString queryString, IRequestContent body, IDictionary`2 headers, TimeSpan timeout, CancellationToken token)
    StreamUtil.MonitorStreamForMessagesAsync[T](Task`1 streamTask, DockerClient client, CancellationToken cancel, IProgress`1 progress)
    TestcontainersClient.RunAsync(TestcontainersConfiguration config, CancellationToken ct)
    TestcontainersContainer.Create()
    TestcontainersContainer.StartAsync()
    UnitTest1.MyOwnContainer() line 51
    --- End of stack trace from previous location where exception was thrown ---

Note that on the same dev machine following works like a charm:

docker run --name subsystem1 --rm -it -p 9999:80 subsystem1image

My docker file is:

# Build stage
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS buildenv

WORKDIR /generator

ENV projectName "Subsystem1.Api"
# restore
COPY src/${projectName}/${projectName}.csproj ./${projectName}/
RUN dotnet restore ${projectName}/${projectName}.csproj


# copy src
COPY ./src/${projectName} ./${projectName}

# publish
RUN dotnet publish ${projectName}/${projectName}.csproj -o /publish -c Release

# Runtime stage

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime

ENV projectName "Subsystem1.Api"
COPY --from=buildenv /publish /publish
WORKDIR /publish

ENTRYPOINT "dotnet" "${projectName}.dll"

But I'm wondering if the dockerfile is really relevant since I'm trying to create a container from an image which has been created already based on the docker file?

I tried also to use directly the docker dotnet package that you are using internally and that works also:

[Fact]
        public async Task UsingDockerDotNet()
        {
            DockerClient client = new DockerClientConfiguration(
                new Uri("npipe://./pipe/docker_engine"))
                .CreateClient();
            var images = await client.Images.ListImagesAsync(new Docker.DotNet.Models.ImagesListParameters() { All = true });
            var container = await client.Containers.CreateContainerAsync(new Docker.DotNet.Models.CreateContainerParameters()
            {
                 Image= "subsystem1image",
            });
            
        }

I'll try to do some more debugging to find the problem.
Cheers
paul.

@paulvanbladel
Copy link
Author

@HofmeisterAn
André, I was using the stable 1.0.0, when switching to the latest beta the login error disappeared.

@HofmeisterAn
Copy link
Collaborator

HofmeisterAn commented Jan 12, 2020

I just rechecked the code. I also discovered the reason for the problem. In version 1.0.0 I used the property label, to determine if a image exists or not. I your case, the label wasn't set.
https://github.com/HofmeisterAn/dotnet-testcontainers/blob/0bf47ff0f3cd988a84d6c7df8abb66060f357bc6/src/DotNet.Testcontainers/Client/DockerApiClientImage.cs#L34

I changed this behavior in beta version 1.1.0 to the correct property reference:
https://github.com/HofmeisterAn/dotnet-testcontainers/blob/e2c6c18a5f9c6d201cb4c2e6b3a7425724930a58/src/DotNet.Testcontainers/Clients/DockerImageOperations.cs#L32

I'll release a new version later this day.

@HofmeisterAn HofmeisterAn added the bug Something isn't working label Jan 12, 2020
@HofmeisterAn HofmeisterAn added this to the 1.1.0 milestone Jan 12, 2020
@paulvanbladel
Copy link
Author

@HofmeisterAn
Andrė, my apologies that I didn't notice earlier that I wasn't using the latest version.
Did you activate source link?
It would allow to debug directly into the sources when consuming the nuget package. See https://github.com/dotnet/sourcelink/blob/master/README.md
Just a suggestion of course.

@HofmeisterAn
Copy link
Collaborator

No worries. Symbols are included and I also publish a snupkg package to nuget.org:
https://github.com/HofmeisterAn/dotnet-testcontainers/blob/60dece7fe03984897058d14aceeb46dae4ad860d/build.cake#L124

Do I need anything else?

@paulvanbladel
Copy link
Author

I will check and keep you posted.

@paulvanbladel
Copy link
Author

@HofmeisterAn
André, I thin you need to include this package reference
https://github.com/dotnet/sourcelink/blob/master/README.md#githubcom-and-github-enterprise
I am not familiar with the cake approach but everything here seems to be covered by your script.
https://github.com/dotnet/sourcelink/blob/master/README.md#using-source-link-in-net-projects

@HofmeisterAn
Copy link
Collaborator

Ok, thanks. I'll check that.

@HofmeisterAn
Copy link
Collaborator

@paulvanbladel Package is now part of the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants