As Programming and Programmers would have it, there're just so many buzz words, tools and frameworks out there. One of the major buzz words these days is Micro-Services and its been mostly driven by majority of tools, one of which is Docker. We'll be going through all you need to get started with Docker.
Why use Docker?
Well, the major advantage to using it for Development is that it containerises your applications, this way you can have various services in different containers as if they were running in VMs'(not exactly Virtual Machines though). The containers boot a lot faster than VMs, usually in less than a second and this is because the containers share the same kernel as the host. Well, on Windows it's a little different as it runs a small Linux Distro called MobyLinux within Hyper-V and that's where the magic starts. Docker makes scalability seamless and my favourite is sharing of ready built workspace for development.
Installation of Docker is pretty straightforward and I will be covering installation of Docker on Windows.
- Visit Docker's website to download the Docker for windows installer.
- Double click on the msi package and follow the prompt to proceed with the installation.
- When complete, go ahead and launch Docker (see below for container not starting error fix).
- Open Hyper-V Manager and turn off MobyLinuxVM.
- Now right-click on the Docker icon on the Taskbar.
- Click on settings to launch the Docker UI console.
- Click on the reset tab and click on the Restart Docker...
- This will take a while but ultimately, you should have Docker running in about a minute.
Now that we have Docker installed Lets pull some images from the Docker hub.
Image: This is at the heart of every container and they contain various layers of applications.
Container: A container is an instance of an image and you can have so many instances if you so choose.
Docker Hub: Is a repository of images uploaded by Docker users. Although it's actually a set of instructions stored in a Dockerfile that gets downloaded and built into an image.
At this point let's pop open Powershell and get our hands wet.
- Pull the Python official image from Docker hub by entering the command below. This is going to take a while, since it has to download all the layers that make up the image
docker pull python
- When the pull process is complete, you can now create and start a container from the Python image from a single command. Remember though, every single run of this command creates a new container with an interactive Python shell.
docker run -ti python
- A container is automatically created and started for you. to exit the container, you can enter
Ctrl Dinto the interactive window to exit and stop the container.
- I'm sure you're wondering what happened to your container at this point and how do you return to it. Well, you can simply enter
docker psto show you all running containers or
docker ps -ato show both running and stopped containers. You'll see something like the image below.
- So now that we can see our Python container. Lets start our container. Two ways to do this is to either using the name or the id (whether the full id or just a subset from the its start). So we have
docker start small_wrightor
docker start bf7c.
- We can now attach to it and continue playing with it.
docker attach bf7c.
- Now to stop the container
docker stop bf7cor kill it using
docker kill bf7c. The difference between these two commands is that the later immediately shuts the container down whether there's a process running or not while the former takes some time and if after about 10 seconds the container doesn't shutdown, it sends a kill command.
- You can now delete the container
docker rm bf7cand/or delete the image
docker rmi python.
I hope you enjoyed the read. I understand it's long but I wanted to cover as much basics as possible in order to prepare you for you for subsequent docker tutorials that I'll be posting in the future. I plan to write about three follow-ups to this article and I promise it'd be as short as needed. Kindly spread the word. Thanks.