Developing inside docker on WSL2-Ubuntu from vscode

Viewed 598

I am trying run docker inside WSL (am running Ubuntu in WSL). Also am new to docker. The doc says:

To get the best out of the file system performance when bind-mounting files:

  • Store source code and other data that is bind-mounted into Linux containers (i.e., with docker run -v <host-path>:<container-path>) in the Linux filesystem, rather than the Windows filesystem.
  • Linux containers only receive file change events (“inotify events”) if the original files are stored in the Linux filesystem.
  • Performance is much higher when files are bind-mounted from the Linux filesystem, rather than remoted from the Windows host. Therefore avoid docker run -v /mnt/c/users:/users (where /mnt/c is mounted from Windows).
  • Instead, from a Linux shell use a command like docker run -v ~/my-project:/sources <my-image> where ~ is expanded by the Linux shell to $HOME.

I also came across following:

Run sudo docker run -v "$HOME:/host" --name "[name_work]" -it docker.repo/[name]. With, [$HOME:/host], you can access your home directory in /host dir in docker image. This allows you to access your files on the local machine inside the docker. So you can edit your source code in your local machine using your favourite editor and run them directly inside the docker. Make sure that you have done this correct. Otherwise, you may need to copy files from the local machine to docker, for each edit (a painful job).

I am not able to understand the format of parameter passed to -v option and what it does. I am thinking that it will allow to access Ubuntu directories inside docker. So $HOME:/host will map Ubuntu's home directory to /host inside.

Q1. But what is /host?

Q2. Can I do what is stated by above two quotes together? I mean what they are saying is compatible? I guess yes. What all its saying is I should not mount from windows director like /mnt/<driveletter>/.... If I am mounting linux directory like $USER/... then it will give better performance, right?

I tried out running it to understand it:

~$ docker run -v "$HOME:/host" --name "mydokr" -it docker.repo.in/dokrimg
root@f814974a1cfb:/home# ls

root@f814974a1cfb:/home# ll
total 8
drwxr-xr-x 2 root root 4096 Apr 15 11:09 ./
drwxr-xr-x 1 root root 4096 Sep 22 07:16 ../
root@f814974a1cfb:/home# pwd
/home

root@f814974a1cfb:/home# cd ..

root@f814974a1cfb:/# ll
total 64
drwxr-xr-x   1 root root 4096 Sep 22 07:16 ./
drwxr-xr-x   1 root root 4096 Sep 22 07:16 ../
-rwxr-xr-x   1 root root    0 Sep 22 07:16 .dockerenv*
lrwxrwxrwx   1 root root    7 Jul  3 01:56 bin -> usr/bin/
drwxr-xr-x   2 root root 4096 Apr 15 11:09 boot/
drwxr-xr-x   5 root root  360 Sep 22 07:16 dev/
drwxr-xr-x   1 root root 4096 Sep 22 07:16 etc/
drwxr-xr-x   2 root root 4096 Apr 15 11:09 home/
drwxr-xr-x   5 1000 1001 4096 Sep 22 04:52 host/
lrwxrwxrwx   1 root root    7 Jul  3 01:56 lib -> usr/lib/
lrwxrwxrwx   1 root root    9 Jul  3 01:56 lib32 -> usr/lib32/
lrwxrwxrwx   1 root root    9 Jul  3 01:56 lib64 -> usr/lib64/
lrwxrwxrwx   1 root root   10 Jul  3 01:56 libx32 -> usr/libx32/
drwxr-xr-x   2 root root 4096 Jul  3 01:57 media/
drwxr-xr-x   2 root root 4096 Jul  3 01:57 mnt/
drwxr-xr-x   2 root root 4096 Jul  3 01:57 opt/
dr-xr-xr-x 182 root root    0 Sep 22 07:16 proc/
drwx------   1 root root 4096 Aug 24 03:54 root/
drwxr-xr-x   1 root root 4096 Aug 11 10:24 run/
lrwxrwxrwx   1 root root    8 Jul  3 01:56 sbin -> usr/sbin/
drwxr-xr-x   2 root root 4096 Jul  3 01:57 srv/
dr-xr-xr-x  11 root root    0 Sep 22 03:32 sys/
-rw-r--r--   1 root root 1610 Aug 24 03:56 test_logPath.log
drwxrwxrwt   1 root root 4096 Aug 24 03:57 tmp/
drwxr-xr-x   1 root root 4096 Aug 11 10:24 usr/
drwxr-xr-x   1 root root 4096 Jul  3 02:00 var/

root@f814974a1cfb:/home# cd ../host
root@f814974a1cfb:/host# ll
total 36
drwxr-xr-x 5 1000 1001 4096 Sep 22 04:52 ./
drwxr-xr-x 1 root root 4096 Sep 22 07:16 ../
-rw-r--r-- 1 1000 1001  220 Sep 22 03:38 .bash_logout
-rw-r--r-- 1 1000 1001 3771 Sep 22 03:38 .bashrc
drwxr-xr-x 3 1000 1001 4096 Sep 22 04:56 .docker/
drwxr-xr-x 2 1000 1001 4096 Sep 22 03:38 .landscape/
-rw-r--r-- 1 1000 1001    0 Sep 22 03:38 .motd_shown
-rw-r--r-- 1 1000 1001  921 Sep 22 04:52 .profile
-rw-r--r-- 1 1000 1001    0 Sep 22 03:44 .sudo_as_admin_successful
drwxr-xr-x 5 1000 1001 4096 Sep 22 04:52 .vscode-server/
-rw-r--r-- 1 1000 1001  183 Sep 22 04:52 .wget-hsts

So I am not getting whats happening here. I know docker has its own file system.

Q3. Is is that, what am finding at /home and /host is indeed container's own file system?

Q4. Also, what happened to -v $HOME:/host here?

Q5. How can I do as stated by 2nd quote:

This allows you to access your files on the local machine inside the docker. So you can edit your source code in your local machine using your favourite editor and run them directly inside the docker.

Q6. How do I connect vscode to this container? From WSL-Ubuntu, I could just run code . to launch vscode. But the same does not seem to work here:

    root@f814974a1cfb:/home# code .
    bash: code: command not found

This link says:

A devcontainer.json file can be used to tell VS Code how to configure the development container, including the Dockerfile to use, ports to open, and extensions to install in the container. When VS Code finds a devcontainer.json in the workspace, it automatically builds (if necessary) the image, starts the container, and connects to it.

But I guess this says starting up creating new container form vscode. But not connecting to already existing container. I am not able to find my dockercontainer.json. I downloaded this container image using docker pull.

0 Answers
Related