Debug School

rakesh kumar
rakesh kumar

Posted on • Edited on

Deploying a website using containerization on Windows Subsystem for Linux (WSL)

How to install wsl
how to open wsl in visual studio
How to Copy your website files from Windows to your WSL home directory
how to Install Docker Desktop on Windows
how to Install Docker Desktop on linux
how to create docker file and create image
Error command 'docker' could not be found in this WSL 2 distro
How to create docker image to deploy website
how to run container from docker image
how to list container
Difference Between docker ps and docker ps -a
how to exit from container
how to enter in particular container or opens a shell inside the container
How to create and starts container with volume mounts
how to Copy the File Out of the Existing Container
How to create and starts container using network bridge
how to stop all running container

How to install wsl

Step1:Enable Required Windows Features
You must have these Windows features enabled:

Virtual Machine Platform

Windows Subsystem for Linux

Windows Hypervisor Platform (sometimes needed)

How to enable:

Open Control Panel → Programs → Turn Windows features on or off.
Enter fullscreen mode Exit fullscreen mode

Check the boxes for:

Windows Subsystem for Linux

Virtual Machine Platform

Windows Hypervisor Platform (if available)

Click OK and restart your PC

step2: open cmd not gitbash

wsl --install
Enter fullscreen mode Exit fullscreen mode

step3:type password
step4: you will get prompt

rakes@Zebronics:/mnt/c/Users/rakes$
Enter fullscreen mode Exit fullscreen mode

step5: press window +R

how to open wsl in visual studio

step 1:open visual studio
step2: Open VS Code.

Go to the Extensions view (Ctrl+Shift+X).

Search for "WSL" or "Remote - WSL".

If it is not installed, click Install.

If it is already installed, ensure it is enabled

step3:Go to the Extensions view (Ctrl+Shift+p)
Press F1 or Ctrl+Shift+P to open the Command Palette.

Type and select WSL: Connect to WSL (or WSL: Connect to WSL using Distro for a specific Linux distribution).

Use the File menu to open a folder located in your WSL filesystem.

how to Install Docker Desktop on Windows

step 1:go to site

https://docs.docker.com/desktop/setup/install/windows-install/
Enter fullscreen mode Exit fullscreen mode

step 2:Enable WSL Integration

Enable WSL Integration

Open Docker Desktop.

Go to Settings > Resources > WSL Integration.

Enable integration for your Ubuntu distribution.

Restart Docker Desktop and your WSL terminal.

Try your command again in WSL:

text
docker --version

step 3:Create a New File Named Dockerfile
In the Explorer sidebar, right-click on your project folder.

Select New File.

Name the file exactly:

Dockerfile
(No extension, capital "D", case-sensitive on Linux/WSL.)

  1. Add Your Dockerfile Content Click to open the new Dockerfile.

Paste your Dockerfile instructions, for example:

FROM ubuntu:20.04

RUN apt-get update && apt-get install -y wget net-tools

RUN wget https://sourceforge.net/projects/xampp/files/XAMPP%20Linux/8.2.12/xampp-linux-x64-8.2.12-0-installer.run
RUN chmod +x xampp-linux-x64-8.2.12-0-installer.run
RUN yes | ./xampp-linux-x64-8.2.12-0-installer.run

EXPOSE 80 3306

CMD ["/opt/lampp/lampp", "start"]

EXPOSE 80 3306

CMD ["/opt/lampp/lampp", "start"]
Enter fullscreen mode Exit fullscreen mode

then save as from filemenu

how to rename dockerfile

mv my-xampp-image.dockerfile Dockerfile
Enter fullscreen mode Exit fullscreen mode

how to Install Docker Desktop on linux

how-to-install-and-use-docker-on-ubuntu
how-to-install-docker-in-linux

how to create docker file and create image

step1
Visit the Official XAMPP Download Page
Go to the XAMPP official download page.

Scroll to the section labeled XAMPP for Linux
step2 right click installer and copy link

copied link is

https://sourceforge.net/projects/xampp/files/XAMPP%20Linux/8.2.12/xampp-linux-x64-8.2.12-0-installer.run
Enter fullscreen mode Exit fullscreen mode
RUN wget https://sourceforge.net/projects/xampp/files/XAMPP%20Linux/8.2.12/xampp-linux-x64-8.2.12-0-installer.run
Enter fullscreen mode Exit fullscreen mode

step3 create dockerfile

FROM ubuntu:20.04

RUN apt-get update && apt-get install -y wget net-tools

RUN wget https://sourceforge.net/projects/xampp/files/XAMPP%20Linux/8.2.12/xampp-linux-x64-8.2.12-0-installer.run
RUN chmod +x xampp-linux-x64-8.2.12-0-installer.run
RUN yes | ./xampp-linux-x64-8.2.12-0-installer.run

EXPOSE 80 3306

CMD ["/opt/lampp/lampp", "start"]
Enter fullscreen mode Exit fullscreen mode

step 4 save as from menu

step5: run docker command to build image

docker build -t my-xampp-image .
Enter fullscreen mode Exit fullscreen mode

step6: check docker image created

docker images
docker image inspect motoshare
Enter fullscreen mode Exit fullscreen mode



FROM ubuntu:20.04

RUN apt-get update && apt-get install -y wget net-tools

RUN wget https://sourceforge.net/projects/xampp/files/XAMPP%20Linux/8.2.12/xampp-linux-x64-8.2.12-0-installer.run
RUN chmod +x xampp-linux-x64-8.2.12-0-installer.run
RUN yes | ./xampp-linux-x64-8.2.12-0-installer.run

# Add this line to copy your website into the container
COPY motoshare-web /opt/lampp/htdocs
# (Optional) Set permissions if needed
RUN chown -R root:root /opt/lampp/htdocs

EXPOSE 80 3306

CMD ["/opt/lampp/lampp", "start"]
Enter fullscreen mode Exit fullscreen mode

How to Copy your website files from Windows to your WSL home directory

cp -r /mnt/c/xampp/htdocs/motoshare-web ~/motoshare-web
Enter fullscreen mode Exit fullscreen mode

mv /home/rakes/motoshare-web /home/rakes/my-xampp-docker/

Error command 'docker' could not be found in this WSL 2 distro

How to create docker image to deploy website

step1:create docker file

FROM ubuntu:20.04

RUN apt-get update && apt-get install -y wget net-tools

RUN wget https://sourceforge.net/projects/xampp/files/XAMPP%20Linux/8.2.12/xampp-linux-x64-8.2.12-0-installer.run
RUN chmod +x xampp-linux-x64-8.2.12-0-installer.run
RUN yes | ./xampp-linux-x64-8.2.12-0-installer.run

# Add this line to copy your website into the container
COPY motoshare-web /opt/lampp/htdocs
# (Optional) Set permissions if needed
RUN chown -R root:root /opt/lampp/htdocs

EXPOSE 80 3306

CMD ["/opt/lampp/lampp", "start"]
Enter fullscreen mode Exit fullscreen mode

save the file

step2:run command to create image from dockerfile

docker build -t motoshare .
Enter fullscreen mode Exit fullscreen mode
docker images
docker image inspect motoshare
Enter fullscreen mode Exit fullscreen mode

how to run container from docker image

docker run -it --name mycontainer motoshare bash
Enter fullscreen mode Exit fullscreen mode

output

in dockerimage my path is

COPY motoshare-web /opt/lampp/htdocs 
Enter fullscreen mode Exit fullscreen mode

so inside htdocs itself motoshare all folder structure appear

=============ANOTHER EXAMPLE===================


docker run \
--name traccar \
--hostname traccar \
--detach --restart unless-stopped \
--publish 80:8082 \
--publish 5000-5300:5000-5300 \
--publish 5000-5300:5000-5300/udp \
traccar/traccar:latest
Enter fullscreen mode Exit fullscreen mode

note

if http port is for 80 reserved then run below command

docker run \
--name traccar \
--hostname traccar \
--detach --restart unless-stopped \
--publish 8082:8082 \
--publish 5000-5300:5000-5300 \
--publish 5000-5300:5000-5300/udp \
traccar/traccar:latest
Enter fullscreen mode Exit fullscreen mode
docke ps-a
Enter fullscreen mode Exit fullscreen mode
<public ip-address>:8082
13.203.171.107:8082
Enter fullscreen mode Exit fullscreen mode

note never run together two command run container and

wget https://github.com/traccar/traccar/releases/download/v6.8.1/traccar-linux-64-6.8.1.zip
Enter fullscreen mode Exit fullscreen mode

it would crash

note

not run unzip command at root it would crash

sudo mv traccar-linux-64-*.zip /opt/
cd /opt
sudo unzip traccar-linux-64-*.zip
Enter fullscreen mode Exit fullscreen mode

how to list container


docker ps -a
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
Enter fullscreen mode Exit fullscreen mode

output

Note
docker ps command by default only shows running containers
not show container name

Difference Between docker ps and docker ps -a

how to exit from container

how to enter in particular container or opens a shell inside the container

docker exec -it container_name_or_id bash
docker exec -it d06f16e55c55 bash
Enter fullscreen mode Exit fullscreen mode

How to create and starts container with volume mounts

docker run -it --name motocontainer -v shared-data:/data motoshare
Enter fullscreen mode Exit fullscreen mode

Before running command my folder structure

After running command my folder structure

how to Copy the File Out of the Existing Container

existing file structure


docker cp 70f935f448d0:/opt/lampp/htdocs/hello.txt ./hello.txt
Enter fullscreen mode Exit fullscreen mode

how to stop all running container

docker stop $(docker ps -q)
Enter fullscreen mode Exit fullscreen mode
docker start $(docker ps -aq)
Enter fullscreen mode Exit fullscreen mode
docker rm -f $(docker ps -aq)
Enter fullscreen mode Exit fullscreen mode
docker rm $(docker ps -a -q --filter status=exited)
Enter fullscreen mode Exit fullscreen mode

How to create and starts container with volume mounts

docker network create --driver bridge my-bridge-network
docker run -dit --name container1 --network my-bridge-network alpine sh
docker run -dit --name container2 --network my-bridge-network alpine sh
Enter fullscreen mode Exit fullscreen mode

Step 1: Create a User-Defined Bridge Network

docker network create --driver bridge my-bridge-network
This creates a new bridge network named my-bridge-network.

Step 2: List All Networks
bash

docker network ls
Enter fullscreen mode Exit fullscreen mode

Shows all Docker networks, including your new user-defined bridge.

Step 3: Inspect the Network

docker network inspect my-bridge-network
Enter fullscreen mode Exit fullscreen mode

Displays details like subnet, gateway, and connected containers.

Enabling Container-to-Container Communication
When containers are attached to the same user-defined bridge network, they can communicate directly using container names.

Step 1: Run Containers on the Same Network

docker run -dit --name container1 --network my-bridge-network alpine sh
docker run -dit --name container2 --network my-bridge-network alpine sh
Enter fullscreen mode Exit fullscreen mode

Both containers are now connected to my-bridge-network.

Step 2: Test Communication
Open a shell in container1:

docker exec -it container1 sh
Enter fullscreen mode Exit fullscreen mode

From inside container1, ping container2 by name:

ping container2
Enter fullscreen mode Exit fullscreen mode

Note:If the target container is stopped or not running, attempts to communicate with it (by name or IP) will fail. source container and target container bith should be up

so follow these step

docker start container4 container5
docker exec -it container4 sh
ping container5
Enter fullscreen mode Exit fullscreen mode

Top comments (0)