[Java] Docker-compose ~volume trap~

3 minute read

Introduction

First post. My name is Chinese cabbage. I am a college student with no hobbies in programming. While I was writing the code the other day, I was addicted to a deep acupuncture point, and I could not post relevant information even after various investigations, so I will post it.

What happened

I used windows and heard that wsl2 was officially introduced, so I decided to try running Docker-desktop on wsl2. I’ve been playing around for about a month after preparing the environment, and I thought it would be super hot because I could touch various languages without polluting my PC environment. I used VS Code’s Remote Development to connect to the container started with docker, and edited/debugged the code in the container.

However, a problem occurred

It was one of the days when I was writing the code under Rashomon, waiting for the rain to stop. I heard that somebody called springboot is very good, so I tried it. I’ve lightly implemented some of the official Japanese tutorials so that I can write the basics of spring MVC without cheating. Now that I’ve been in good shape, I decided to try database processing by linking with MySQL, and created an environment with docker. In particular,

  • java container
  • MySQL container
  • phpmyadmin container (for ease of database management)

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 on a java container with volume. However, when I try to write the code, the linkage with the database does not work. Emits a database connection error every time I debug. The code itself is almost a copy and paste of the tutorial, so it should not work… Isn’t this because of docker, not code?

Symptoms

In addition to the above, similar symptoms occurred in other containers. Below is a list.

  • My MySQL integration doesn’t work *Or maybe the editing of the configuration file for linkage is not reflected
  • It worked when I reflected it manually
  • It’s called Not Found even though I created html and linked it to the controller
  • I made a React environment, but it takes more than 1 minute to learn start
  • After yarn start, editing the file does not reflect in the browser

What they have in common is that editing/creating files is not reflected.

Resolution

After doing various experiments, I came to the conclusion that there is a problem with vloume of docker. When the file on windows is specified as volume, it seems that changes in the file in volume are not reflected in real time. Therefore, it is one solution that you have to specify the volume, but if you want to save the code permanently, it is troublesome to copy it to windows and bring it. ‥ There is a method to solve it 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.

“You should put the file to mount volume on Linux, not on Windows!”

······I’m sorry. I had a glimpse of it, but the mount itself worked and wasn’t it good on Windows? I thought. I never expected that such a strange problem would occur.

So, I solved it by mounting the directory created on wsl2 on Linux and recreating the React environment. The yarn start has reached 20 seconds, and it will be reflected every time you change the code. React may be.

At the end

In conclusion, I think it is better to create the directory mounted by volume on Linux. It’s probably correct because the people who are developing are saying it. I was so bad that I was trying to get angry. I’m sorry···. You must read the official document carefully.