What is Docker and Why it is Useful for Windows Users?
Consistency Across Environments: Docker ensures that the application runs the same way on any machine. This is particularly goodfor Windows users working in teams where most developers use Linux or Mac. By containerizing the application, Docker eliminates the "works on my machine" problem, ensuring that the code behaves identically regardless of the underlying operating system.
Simplified Deployment Process: Docker containers encapsulate everything the application needs to run, which simplifies the deployment process. Whether you're deploying to a local server, a cloud provider, or a hybrid environment, Docker ensures that the deployment process is consistent and straightforward.
By using Docker, Windows users can effectively collaborate with team members who use different operating systems. However, as with most programming related things, installing the necessary software and getting a basic example running can often be the most frustrating part.
OK first things first , go to the Docker website and download the installer here. It should take around 5 minutes for Docker to be installed, which then required a reboot of your machine.
Since we are beginners with Docker, we will just use the default settings on the following prompt
We continue without signing in , because we have quite enough promotional emails to deal with!
Ok once that is done we are going to do some basic things with the command line to ensure that we have the Linux distribution we want (Ubuntu) and also ensure that we have WSL 2 due to its increased performance. You can type "Powershell" in to your search bar at the bottom of your screen and you should see the following as the best match.
OK once it is opened, you can copy and paste the following command to set up a new user, type in your new username and password when prompted. This installs Ubuntu 22.04 as the default on my machine, you can also install different distributions if you prefer, but I feel Ubuntu is the most user friend of all the Linux distros.
wsl --install
Enable Integration with Docker Desktop and WSL
Now that we have Ubuntu installed, we navigate back to Docker Desktop and select the gear icon in order to modify settings. You should see Ubuntu or your chosen distro appear here.
Click on "Apply & restart" in order for the changes to take effect.
OK so how can we reopen a WSL session with the distribution we just created. Well it is actually very simple, first off we need to go back to the search bar and type "Windows Terminal" , which will open a session and then type 'Ubuntu' (or whatever distro you are using).
Ok now we can check that our Docker integration we previously selected is working as expected, type the following command in to your terminal
docker ps --all
This is the command to list all containers in Docker, since we have only just installed it we should expect the following output in our terminal indicating that we do not have any containers or images created yet.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
How to Change Directory to Desktop with WSL on Windows?
One of the most tricky things is navigating the file system using the Linux terminal on Windows, often for Windows users we orientate ourselves around the desktop application. In order to find this on the command line you can try the following (ensure you replace your username with the correct name)
Since my machine is a fresh install I needed to add OneDrive which was new to me. I guess you could try the following :
cd /mnt/c/Users/your_user_name/OneDrive/Desktop
Alternatively if that doesn't work you can try
cd /mnt/c/Users/your_user_name/Desktop
OK so I think we can all agree that trying to remember this long path every time we want to navigate to the desktop, well luckily we can make what is known as a symbolic link so we don't have to remember it. Make sure you were able to navigate to your desktop correctly, you can verify this by typing 'ls' when in the folder and ensuring it shows the same files/applications as you see on your desktop.
OK making the symbolic link is very easy ensure you switch out the path to the Desktop with the one you found on your machine.
First we switch back to the root directory simply by typing cd
cd
Creating the sym link with the directory from my desktop, note that I am simply naming it desktop so it will be easy to remember but you can choose whatever you like.
ln -s /mnt/c/Users/johnc/OneDrive/Desktop ~/desktop
Once that is done we can navigate to the desktop with
cd ~/desktop
Ok that should save us a lot of hassle down the road.
Installing WSL extension in VsCode
We are probably going to want to use the Ubuntu terminal in our coding sessions, since I use Vscode let's see how to use it here. If you don't have Vscode installed you can click on the Microsoft store icon and search for Vscode.
Ok once that is completed , let's open up a WSL session the way we described previously, first we type "Windows Terminal" in to the search bar and once the terminal is opened we type "Ubuntu" and then of course we want to change the working directory to the desktop , so if we have created the sym link like we shown in the last step simply typing cd ~/desktop should bring us to the correct place.
For the next part of this guide we are going to make a directory called dockerTest and use that to run our first docker image.
Once we have done that we can simply type
code .
This will open a VsCode instance in the new folder we created. Next select the Extensions icon on the left sidebar and type "WSL" and install the first package that comes up.
Open a WSL Ubuntu Terminal from Vscode
Now that we have the extension installed, we can open a terminal, on the top menu click on 'Terminal' which will open a Windows Powershell terminal , we have 2 options for opening an Ubuntu terminal, the first is we simply type Ubuntu , although if you haven't called your distro Ubuntu or something else goes wrong, you can click on the dropdown on the left shown below
Select the name of your distro or WSL build , mine would be Ubuntu (WSL) from the menu above.
Making Your First Docker Image
For this part we are going to create a simple Flask web application to test out Docker. We need to create 3 files in the dockerTest folder we just created.
app.py
This python file contains the basic building blocks of a Flask application , you can copy and paste the code below in to your file.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
requirements.txt
All you need to put in this file is the following:
Flask
DockerFile
Note that you should not include an extension in this file, and be sure to name it DockerFile like we have shown
A Dockerfile is a simple text file that contains a set of instructions to build a Docker image. Think of it as a recipe that tells Docker how to create your application’s environment. This file defines everything needed to run your application, including the operating system, software dependencies, and configuration settings.
# Use an official Python runtime as a parent image
FROM python:3.10-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Run app.py when the container launches
CMD ["python", "app.py"]
Build The Docker Image
Ensuring we are in the correct directory as shown below
Then to build the image you can copy paste the following in to your terminal.
docker build -t my-first-image .
OK and the first time I tried to run this I got the following error "ERROR: failed to solve: failed to read dockerfile: open Dockerfile: no such file or directory"
I have left this up as it may be useful to someone that has run in to the same error, the reason this has happened is because I named my build file 'DockerFile" when it should have been "Dockerfile" , renaming the file so the folder looks like the image below should fix the issue. It is also possible you get this error if you are not in the correct directory, so first check whether you have named your docker file correctly, and then ensure your terminal session is in the right working directory i.e. the one with the docker file.
OK so let's try again with the correct file name
docker build -t my-first-image .
And run the image in a container called "my-first-container"
docker run --name my-first-container -p 4000:80 my-first-image
If all has went well then you should see something that looks like :
* Serving Flask app 'app'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:80
* Running on http://172.17.0.2:80
Press CTRL+C to quit
172.17.0.1 - - [28/Jun/2024 08:17:21] "GET / HTTP/1.1" 200 -
OK cool, now that is about enough of the command line stuff for a while, go back to your Docker Desktop application and you should see your container running.
If you have ever tried using Docker solely from the command line, you will appreciate how great Docker Desktop is, you can stop / start the container from here and also just click on the port link and it will open the web application where you should see in plain text "Hello World!"
Here you can view the logs, inspect the container along with much more!