NET 6 and SQL Server development environment into a Docker container using Visual Studio Code’s Remote Development today. Although it went smoothly with the documentation, the problem arose after the project was versioned. I think the point of this article is that in today’s containerized, cross-platform era, line breaks actually have to be confronted, or else you’ll be at a loss for what to do.
Experience the feeling of developing in containers
-
Install the Docker Desktop and Remote Development extensions first.
-
Create the basic ASP.NET Core 6 project and open it with Visual Studio Code first.
-
Press
F1
>Remote-Containers: Add Development Container Configuration Files
-
Select a Dev Container template first:
C# (.NET) and MS SQL
-
Select .NET version:
6.0-focal
-
Select Node.js version:
none
-
Select the tool to be used in the container: Here we do not select it first, we can just press
OK
to finish the settingThat’s all set! π
-
Next, let’s do Git version control.
To do this, I’ll start by adding a .NET-specific
.gitignore
file.1
dotnet new gitignore
The following is the final version, basically each file you can click on to see.
Establish version control.
-
Run
F1
>Remote-Containers: Reopen in Container
to start automaticallyBehind this action, all the required container images are automatically created, and then automatically started, the container environment is automatically initialized (installing the necessary development tools), the necessary network connection port forwarding is turned on (Port Forwarding), and then you can smoothly develop, test, and execute in the Container container connected to Visual Studio Code.
Experience the problem after version control
Since the .devcontainer/Dockerfile
file in the project is responsible for building all the environments in the Dev Container development container, it contains SQL-related tools such as sqlcmd, bcp, sqlpackage, etc.
The contents of this .devcontainer/Dockerfile
are as follows
|
|
You can find that he has copied mssql/installSQLtools.sh
to the container, the point is that the Dev Container provided by Microsoft is based on Ubuntu 20.04.4 LTS
as the main operating system, and when running this command, it will require all Shell Scripts to be executed with LF
as the main line break character, so the common Windows CRLF
line break character is not available, and the process of building container image will be wrong!
Let’s take a look at the process of how this error occurred.
-
The container does not contain the project source code.
Microsoft’s Dev Container uses volumn mapping to mount source code into the container, so you only have the “project execution environment” in the container!
-
Judging
~/.gitconfig
settingsWhen we first create a container image, it automatically copies the Windows local
~/.gitconfig
domain-wide configuration file to the~/.gitconfig
container environment, so basically the Git settings underUbuntu 20.04.4 LTS
are exactly the same as Windows. But Windows has a default value ofcore.autocrlf=true
, which means that when all files are checked out, all text files are automatically treated as line feeds withCRLF
, but that’s a big problem! -
Retrieve the source code
Since the
.devcontainer/mssql/installSQLtools.sh
file was initially saved asLF
, it will be automatically converted toCRLF
newlines as soon as the source code is retrieved, so we can expect the.devcontainer/Dockerfile
to have Error!You can try removing the entire
.devcontainer
and retrieving the file withgit reset --hard
. -
Run
F1
>Remote-Containers: Reopen Folder Locally
to go back to Windows and open this project -
Run
F1
>Rebuild Without Cache and Reopen in Container
Rebuild the container image of the Dev Container
At this point you will see the error message.
|
|
If you look at the Terminal, you can see the relatively detailed error message.
|
|
When the problem occurred, it wasn’t very understandable at first, but you should now know very well that it was the line break that caused the error!
The right solution
The most standard solution to this problem is as follows.
-
Run
F1
>Remote-Containers: Reopen Folder Locally
to go back to Windows and open this project -
Create a
.gitattributes
file under the.devcontainer/mssql/
directory and add the following.1
*.sh text eol=lf
This will ensure that all
*.sh
files in that directory will always useLF
as a newline character when Git pulls them out. -
Delete the
.devcontainer/mssql/*.sh
file, and usegit checkout
orgit reset --hard
to retrieve the file. -
Run
F1
>Rebuild Without Cache and Reopen in Container
to rebuild the container image of Dev Container
You should be able to build the container again this time! π
Conclusion
This problem is actually not likely to occur in macOS or Linux, only Windows users will encounter this situation, which makes me wonder if no Microsoft engineers are using Windows anymore ah? π
However, I still think that as long as you are a Windows-loving developer like me, you should understand the difference between CRLF
and LF
, so that you will step on fewer mines in the process of containerization in the future.