devcontainers / templates

Repository for Dev Container Templates that are managed by Dev Container spec maintainers. See https://github.com/devcontainers/template-starter to create your own!
https://containers.dev/templates
MIT License
924 stars 242 forks source link

`docker-compose up` fails in templates/src/cpp-mariadb/.devcontainer/ #152

Closed bluesherpa closed 1 year ago

bluesherpa commented 1 year ago

Should running docker-compose up work in the .devcontainer folder?

The following error is generated using using code in the templates repository under templates/src/cpp-mariadeb/.devcontainer.

C:\code\microsoft\cpp-mariadb.devcontainer>docker-compose up [+] Building 0.1s (2/2) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 1.26kB 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to create LLB definition: failed to process "mcr.microsoft.com/devcontainers/cpp:0-${templateOption:imageVariant}": unsupported modifier (i) in substitution

The following error is generated using the template "C++ & MariaDB" from the devcontainers extension - VS Code v1.77.3, Dev Containers v0.288.1.

C:\code\test\cpp-mariadb.devcontainer>docker-compose up [+] Building 0.7s (8/10) => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 1.22kB 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for mcr.microsoft.com/devcontainers/cpp:0-debian-11 0.2s => [1/6] FROM mcr.microsoft.com/devcontainers/cpp:0-debian-11@sha256:0605a13ffcd3d6d437e55129e93c5d650b8a6c9477d 0.0s => [internal] load build context 0.0s => => transferring context: 78B 0.0s => CACHED [2/6] RUN apt-get update && export DEBIAN_FRONTEND=noninteractive && apt-get -y install curl 0.0s => [3/6] COPY ./install-mariadb.sh /tmp/ 0.0s => ERROR [4/6] RUN chmod +x /tmp/install-mariadb.sh && /tmp/install-mariadb.sh && rm -f /tmp/install-mariadb.sh 0.4s

[4/6] RUN chmod +x /tmp/install-mariadb.sh && /tmp/install-mariadb.sh && rm -f /tmp/install-mariadb.sh:

0 0.346 /bin/sh: 1: /tmp/install-mariadb.sh: not found


failed to solve: executor failed running [/bin/sh -c chmod +x /tmp/install-mariadb.sh && /tmp/install-mariadb.sh && rm -f /tmp/install-mariadb.sh]: exit code: 127

samruddhikhandale commented 1 year ago

@alexander-smolyakov Can you help investigate this issue? thanks!

bluesherpa commented 1 year ago

I figured out part of the problem. This second error appears to be a problem with line endings in the file install-mariadb.sh.

Here is the error: failed to solve: executor failed running [/bin/sh -c chmod +x /tmp/install-mariadb.sh && /tmp/install-mariadb.sh && rm -f /tmp/install-mariadb.sh]: exit code: 127

To confirm this, I manually ran install-mariadb.sh from inside of the container and got an unknown command error "/bin/bash^M"

I did this by commenting out this line in the Dockerfile: RUN chmod +x /tmp/install-mariadb.sh && /tmp/install-mariadb.sh && rm -f /tmp/install-mariadb.sh

I then performed the following steps starting in the .devcontainer folder:

docker build -t test -f Dockerfile . docker run -it --rm --name=test test bash cd /tmp ./install-mariadb.sh

got the error invalid command "/bin/bash^M"

sudo apt install dos2unix dos2unix install-mariadb.sh ./install-mariadb.sh

the installation was successful

I exited the container, deleted the container and image, ran dos2unix on the install-mariadb.sh file from outside of the container, then opened the project folder in Visual Studio Code, telling it to open the folder in a new container. Everything worked this time.

This was using code generated from inside of Visual Studio Code by going to F1 > Dev Containers > Create New Container > C++ and MariaDB.

Let me know if you have any questions about these steps/instructions.

I'll try again using the newest version of the cpp_mariadb template from GitHub.

bluesherpa commented 1 year ago

I see that https://github.com/devcontainers/templates/blob/main/src/cpp-mariadb/.devcontainer/Dockerfile has:

FROM mcr.microsoft.com/devcontainers/cpp:0-${templateOption:imageVariant}

How would I use this template inside of Visual Studio Code so that the placeholder is replaced with the correct platform?

samruddhikhandale commented 1 year ago

I see, looks like you have copied over the contents from this repository? Can you follow the instructions mentioned in https://github.com/devcontainers/templates/issues/113#issuecomment-1406905679 Thanks!

I'll shortly open a PR to update the README to provide these instructions.

bluesherpa commented 1 year ago

It sounds like the line endings in install-mariadb.sh need to be fixed, not the README, if that helps.

bluesherpa commented 1 year ago

I looked at the steps in #113 and confirmed that the it pulls from the templates/src/cpp-mariadb folder of the repo. It looks like the line endings issue is all that needs to be addressed.

alexander-smolyakov commented 1 year ago

Hey @bluesherpa, I didn't manage to reproduce your case. As I can see, the end-of-line for all scripts is set to LF, so it should work fine with Docker. Could you please double-check your system configuration? Sometimes IDE or Git could perform end-of-line conversion based on their configuration.

Related docs:

VS code default settings:

  // The default end of line character.
  //  - \n: LF
  //  - \r\n: CRLF
  //  - auto: Uses operating system specific end of line character.
  "files.eol": "auto",
bluesherpa commented 1 year ago

Hello @alexander-smolyakov, thank you for your assistance. I have confirmed that when cloning with Tortoise Git, the line endings are automatically converted to Windows format. This is due to the fact that AutoCrLF is enabled by default when using OpenSSH/git. Consequently, the file that was copied into the container has Windows-style line endings, which is causing the error.

I see two possible solutions:

  1. Disabling Auto CrLf.
  2. Using dos2unix after copying the file into the container.

Which approach do you recommend?

alexander-smolyakov commented 1 year ago

@bluesherpa, glad to hear that you were able to find the source of the issue.

I see two possible solutions:

  1. Disabling Auto CrLf.
  2. Using dos2unix after copying the file into the container.

Which approach do you recommend?

It depends on the use case. If you are working on multiple repositories, and some contain projects intended to work on different platforms, it's better to keep AutoCrLF enabled. Otherwise, you can disable AutoCrLF.

bluesherpa commented 1 year ago

@alexander-smolyakov Sounds good. I'll go with option 2 for now resolving the issue. Thanks again, I appreciate your help with this.