Are you a developer and getting started with containerization? Containerization is the new trend these days and it makes your existing application system-independent and more reliable. If you are just a beginner and getting started with tools like Docker, then this is the right place to be at. In this post, we have covered a tutorial on how to set up and use Docker on a Windows machine. The process assumes that you are somewhat familiar with the concept of containerization.
To give you a clearer definition of a container, I would directly like to quote Docker:
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
Set up & use Docker Desktop on Windows
Docker has quite a lot of use cases. The most popular of them is the containerization of existing applications. You can repackage your existing Java or .NET applications in a highly portable container that can be deployed to any server. But to do that, you need to understand the basics. So, let’s get started and see how to set it up on Windows.
1. Download and install the setup. Head over to Docker’s website and download Docker Desktop for Windows. Once downloaded, install it on your computer by following the simple instructions in the setup. Your computer might restart a few times during the process.
2. Once everything is installed, you need to create an account. Head over to hub.docker.com and create a new account. Now, use the same account to log into your Windows installation. Creating an account is totally free, and you won’t be charged anything.
3. The setup part is now over. You can confirm if Docker is running by going to system tray and clicking on the whale icon. Or you can open a CMD window and execute
docker --version
to check if you have Docker installed on your computer. Or you can also download the hello-world image to check if everything works fine. In the same CMD window execute docker run hello-world to run all the checks.
4. Now you need an image to start your first container. There are a lot of public images available for different purposes. You can go to Docker hub and search for any image you want. Images are available for WordPress, Ubuntu, Node.Js, etc. In this example, we will install a WordPress image on a local container so that you can run a local WordPress container on your computer.
5. The first step of creating any container is creating its configuration file. The configuration file specifies what image the container will use and with what parameters. So, create a new folder and inside that folder create a new file called docker-compose.yml. Paste the following contents into it and save the file:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data: {}
Source: Docker Documentation
6. Open a CMD window in this folder and execute
docker-compose up -d
to start downloading the images and setting up the container on your local machine. Once the command finishes execution, you will have a WordPress container up and running on your local machine. You can open it up in your browser by going to http://localhost:8000.
This is how you can create a configuration file and download the required things to run your application inside a container. Remember that there are tons of other possibilities available, and this post is here only to give you an overview of Docker and containerization. You can search the internet for more configuration files or create your own. Many free open-source Docker images are available online to get you started.
Once you’ve customized your image and made the necessary changes, you can also push it to a Docker Hub repository. Click here to download Docker Desktop for Windows. Further reading about Docker for Windows here.