pulumi / pulumi-docker

A Docker Pulumi resource package, providing multi-language access to Docker resources and building images.
84 stars 14 forks source link

Cannot build Docker Image in GitLab using .NET SDK #360

Open sfmskywalker opened 4 years ago

sfmskywalker commented 4 years ago

I'm not sure if this is an issue with GitLab, Pulumi.Docker.Image.Image, or myself (most likely :D), but I'm at my wits end here.

Problem description

I have a very simple Pulumi C# project that builds a Docker image from a Dockerfile. This works fine on my local machine (building the image works, but I haven't configured a registry server, so pushing the built image fails - this is expected).

But when I try and run this project from GitLab, it fails with a System.ComponentModel.Win32Exception (2): No such file or directory error when trying to build the image.

Steps to reproduce

First let's make sure it works locally:

  1. Clone my sample project.
  2. Open a shell and navigate to /infra/MyApplication.Infra and do pulumi up.
  3. Notice that although pushing fails, it does build the image.

Now create a PR and observe the build pipeline or simply trigger one. Notice the following error:

Diagnostics:
   docker:image:Image (my-service):
     error: Pulumi.ResourceException: ' docker build ../../ -t my-service' failed with exit code 1
     System.ComponentModel.Win32Exception (2): No such file or directory

Initially I thought perhaps it doesn't like that the Dockerfile is two levels up, so I copied it to the same directory as where the Pulumi .csproj file lives and updated the code accordingly. However, the build failed with the same error.

I'm certain I am missing something important, but I need a little help understanding it. Thanks in advance!

mikhailshilkov commented 4 years ago

@sfmskywalker I've glanced at this issue a couple of times already but I don't have great ideas. Weren't you able to solve it?

It looks like something is wrong in your CI setup, since the local build works. What happens if you try to build the docker image outside of Pulumi, as a separate step in the pipeline?

sfmskywalker commented 4 years ago

@mikhailshilkov Unfortunately not - I'm completely blocked by it for GitLab -_-

When I build the docker image outside of pulumi, it works fine. But I used a different build image for that (docker:stable with service docker:dind). Tomorrow I will try to do the same in the build yaml containing the Pulumi steps.

sfmskywalker commented 4 years ago

I tried the DIND approach, but then it fails because of ./setup.sh: not found.

The .gitlab.yml looks like this:

stages:
  - build

pulumi:
  stage: build
  image: docker:stable
  services:
    - docker:dind
  before_script:
    - chmod +x *.sh
    - ./setup.sh
  script:
    - ./pulumi-up.sh
  artifacts:
    paths:
    - pulumi-log.txt
    expire_in: 1 week
  only:
  - master

pulumi-preview:
  stage: build
  image: docker:stable
  services:
    - docker:dind
  before_script:
    - chmod +x *.sh
    - ./setup.sh
  script:
    - ./pulumi-preview.sh
  only:
  - merge_requests

The setup.sh file does exist in the repo's root, and it's fine if I use a .NET docker image for this build. So clearly I'm misunderstanding how DIND works. I'll try and get a better understanding of how this is supposed to work, but any suggestions in the mean time would be thoroughly appreciated.

As a plan B, I might let go of letting Pulumi build the docker image, and simply leave that part to GitLab. Instead, I would have my infra Pulumi code simply post the container registry credentials to a GitLab project's Variables section.

badokun commented 2 years ago

I'm having the same issue. Hoping to use pulumi exclusively to do all things. My pulumi project is in dotnet, so obviously in my circle ci project I need to reference a dotnet base image (executor), but then that image doesnt contain docker itself (dind) so the pulumi dotnet project fails when it tries to build the docker image.

Did you end up building the docker image outside of pulumi?

sfmskywalker commented 2 years ago

Yes, I ended up building the docker image outside of pulumi.

badokun commented 2 years ago

Thanks for replying... I figured out how to do it inside pulumi, by adding this config in my circle-ci config

      - setup_remote_docker:
          version: 20.10.7
          docker_layer_caching: true

Not sure if the same exists in gitlab

sfmskywalker commented 2 years ago

Great! I'll give that a go next time. Thanks for sharing!