How to mount the batch file location via WSL2 and start the Docker container

What is this?

We suggest how to double-click a batch file and mount the location of the batch file as a WSL2 volume when booting with Docker for Windows.

Assumed reader

--People who have installed WSL2 version docker --People who were warned of poor performance when mounting volume when docker started --People who have trouble starting wsl every time they start docker

Conclusion

The following is an example of starting a container image with a batch file name. If you change the batch file name, the container image to be started will change.

buildpack-deps.bat


@setlocal
@set "WORK=/root/workspace"

@cd /d "%~dp0"
@set "IMG=%~n0"

@if "%~0" == "%~f0" set "pause_if_error=@if errorlevel 1 pause"

@for %%i in ( "%CD%" ) do @set "WSL2CD=%%~di"
@for %%i in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do @call set "WSL2CD=%%WSL2CD:%%~i=%%~i%%"
@for %%i in ( "%CD%" ) do @call set "WSL2CD=%%WSL2CD:~0,-1%%%%~pnxi"
@set "WSL2CD=/mnt/%WSL2CD:\=/%"

wsl -e docker run -it --rm -w "%WORK%" -v "%WSL2CD%:%WORK%" %IMG% %*
%pause_if_error%
@exit /b %errorlevel%

The mount point is written directly in @ set" WORK = / root / workspace ", so rewrite it as appropriate. To fix the image, rewrite @set" IMG =% ~ n0 ". Since distro of wsl is not specified, if you want to specify it, add the option of wsl appropriately.

Confirmation environment

Windows10 1909 Docker version 19.03.13, build 4484c46d9d

Commentary

When starting docker, go through the wsl command. The volume specifies the path on wsl. What is doing the operation of WSL2CD is the process of converting the windows path to the WSL path.

With this, the warning disappears and it is refreshing!

reference

-Command Prompt (cmd.exe) Private Note

Recommended Posts

How to mount the batch file location via WSL2 and start the Docker container
How to start a Docker container with a volume mounted in a batch file
Note: [Docker] How to start and stop
How to check the logs in the Docker container
[Docker] How to see the contents of Volumes. Start a container with root privileges.
Wait for the container service to start with docker healthcheck
[Docker] How to access the host from inside the container. http://host.docker.internal:
Challenge to install WSL2 and docker
How to start Camunda with Docker
Check the contents by docker pull from ECR and start the container
Until you build the docker environment and start / stop the Ubuntu container
[Java] How to use the File class
How to delete the wrong migration file
Recreate the container and then start it
How to delete the migration file NO FILE
Push the Docker Image distributed by Docker Hub to Google Container Registry and start the VM based on that Image
[Docker] How to build when the source code is bind-mounted on the container
Pg_resetwal can be used to start the PostgreSQL Docker container when WAL is broken and cannot be started.
How to find the tens and ones
[Docker] Start the container as soon as possible
How to display the amount of disk used by Docker container for each container
How to start with Hyper-V instead of WSL2 on Docker Desktop for Windows
[Docker] Check the running container and enter there
How to get a heapdump from a Docker container
How to update pre-built files in docker container
How to find the total score and average score
Change the location folder of Docker image & container
How Docker works ~ Implement the container in 60 lines
[Spring Boot] How to refer to the property file
How to build a Jenkins server with a Docker container on CentOS 7 of VirtualBox and access the Jenkins server from a local PC
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
Docker for Windows: MySQL container does not start when migrating from Hyper-v to WSL2
[Java] How to extract the file name from the path
JavaFX-Specify the location of the CSS file relative to the fxml file
How to debug the generated jar file in Eclipse
[Ruby] How to get the tens place and the ones place
Common problems with WSL and how to deal with them
How to launch Swagger UI and Swagger Editor in Docker
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
How to write comments in the schema definition file in GraphQL Java and reflect them in GraphQL and GraphQL Playground
How to create an environment that uses Docker (WSL2) and Vagrant (VirtualBox) together on Windows
Automatically start the container
How to install Docker
How to correctly check the local HTML file in the browser
Difference between Java and JavaScript (how to find the average)
I want to bring Tomcat to the server and start the application
I tried to build the environment of WSL2 + Docker + VSCode
How to get the log when install4j does not start
How to change the contents of the jar file without decompressing
How to run React and Rails on the same server
How to check the extension and size of uploaded files
How to batch run JUnit and get coverage as well
[Docker] How to forcibly disconnect the session connected to the database [Postgres]
How to change the file name with Xcode (Refactor Rename)
[Docker] How to back up and restore the DB data of Rails application on docker-compose [MySQL]
How to build an environment with Docker, which is the minimum required to start a Rails application
[Swift5] How to create a .gitignore file and the code that should be written by default
Method definition location Summary of how to check When defined in the project and Rails / Gem