Docker-compose ~ volume trap ~

Introduction

This is the first post. My name is Chinese cabbage. I'm a college student who doesn't have a hobby in programming. While I was writing the code the other day, I was addicted to a deep pot, and I did not find any related information even after searching various things, so I will post it.

What happened

I'm using windows, and when I heard that wsl2 was officially introduced, I decided to run Docker-desktop on wsl2. After preparing the environment and playing for about a month, I was able to come into contact with various languages without polluting the environment of my PC, and I thought it was extremely hot. I used vscode's Remote Development to connect to the container launched by docker, and edited and debugged the code inside the container.

But the problem occurred

It was at the end of one day when I was writing the code under the Rashomon while waiting for the rain. I heard that something like springboot is really cool, so I tried it. I implemented some of the official Japanese tutorials lightly, and now I can write the basics of spring MVC without cheating. Now that I'm on the right track, I decided to try database processing in cooperation with MySQL, and created an environment with docker. In particular,

I made three of them as one network with docker-compose. I also wanted to save the code I wrote, so I mounted a text directory with volume on the java container. However, when I try to write the code, the cooperation with the database does not work well. It spits out a database connection error every time it debugs. The code itself is almost a copy of the tutorial, so it can't be stuck ... Isn't this caused by docker, not the code?

Symptoms

Other than the above, similar symptoms occurred in other containers. Below is a list of them.

What they have in common is that ** file editing / creation is not reflected **.

Solution

After a lot of experimentation, I came to the conclusion that there is a problem with docker's vloume. When a file on windows is specified as volume, changes in the file in volume are not reflected in real time. So it is one solution that you have to specify volume, but if you want to save the code permanently, it is troublesome to copy it to windows one by one.   There is a solution while using volume. In the first place, docker for windows official has the following description.

Store source code and other data that is bind-mounted into Linux containers (i.e., with docker run -v :) in the Linux filesystem, rather than the Windows filesystem.

** "Volume mount files should be placed on Linux, not on Windows!" **

······I'm sorry. I had a glimpse of it, but the mount itself worked fine, and it's okay on Windows, right? I was thinking. I never thought that such a strange problem would occur.

So, I solved it by mounting the directory created on Linux of wsl2 and recreating the React environment. The yarn start is now 20 seconds, and every time I change the code, it's reflected. React Maybe.

At the end

In conclusion, I think it is better to create a directory to be mounted with volume on Linux. There is no doubt that the people who are developing it are saying it. I was bad when I was trying to do it in my own way. I'm sorry···. You have to read the official document carefully.

Recommended Posts

Docker-compose ~ volume trap ~
try docker-compose
replace trap
Command docker-compose