Run docker containers using VSCode and docker-compose| No Terminal needed

I always shy away from console, hence, I try to find ways to do things graphically (GUI). Docker mainly runs on a Linux machine but with the advent of WSL (Windows Subsystem of Linux) it is possible to run docker on Windows easily (I know Windows docker containers are also gaining traction these days, but they're not there yet). Here I'll be using Windows to show how docker can be managed easily with VSCode.

Prerequisites:

  1. VSCode (Visual Studio Code) should be installed.
  2. WSL 2 is up and running with a Linux distro installed. This article is based on Ubuntu-20.04.

Setup

Step 1: Adapting the mount path in WSL as per our requirements.

The default mount path for WSL is /mnt/c/Users/<username>, this is fine if we do not want to use persistent data (or volume mapping) which is accessible to us from outside the docker container. But if we'd want to map directories this won't work, coz, Windows by default looks for directory structure such as c/Users/<username> and as discussed earlier WSL gives us /mnt/c/Users/<username>. We need to correct this.

To do that we need to create a wsl.conf file in /etc/ directory. First open the WSL distro (in my case it is Ubuntu-20.04) and create a new file in /etc/ directory

sudo nano /etc/wsl.conf

and copy the contents of the file below and paste it in the nano editor

[automount]
enabled = true
root = /
wsl.conf

Press ctrl + x and then Y to save the file.

Now we need to restart the WSL Linux. One way (and the easier) is to simply restart the Windows system we're working on or open Powershell as administrator and then use Get-Service LxssManager | Restart-Service to restart WSL only (we will need to restart docker desktop as well).

Now when we start the WSL Linux, we can see that the mount point has changed.

Step 2: Setting up the directory for docker container installation.

I like to keep all my docker installations at one place. So, I create a folder anywhere to my liking and name it docker.

Step 3: Creating the docker-compose file using VSCode.

Open VSCode and then click on File > Open Folder...

and navigate to the docker folder.

Then create a new folder inside the docker folder by clicking the New Folder button and name it portianer.

Now, click on the portainer folder and then click on New File button and name it docker-compose.yml .

As soon as we create the docker-compose.yml file VSCode will prompt us to install an extension. Click on install and it'll install the docker extension (required).

We'll paste the following in the editor and save the file by pressing ctrl + s.

version: '2'

services:
  portainer:
    image: portainer/portainer
    container_name: portainer
    command: -H unix:///var/run/docker.sock
    restart: always
    ports:
      - 9000:9000
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./data:/data

Step 4: Running the docker container.

Right click on docker-compose.yml and click on Compose Up button

and done.

Conclusion:

We've learnt how to use VSCode to run docker containers in a Windows machine using WSL. To run any other docker container simply repeat step 3 and 4.

Ciao.