What is Docker?

For a long time I avoided Docker, even though it popped up a lot in tutorials and blog posts about web development. I had a vague understanding of it, but I preferred to use VMs (Virtual Machines) like Virtual Box. Eventually I noticed that Laravel had started to use Docker as it’s recommended way of installing a new dev environment, so I thought I better do a quick course on it. After spending a week learning it, I wished I’d done it sooner, as it’s pretty damn impressive what you can do with it!

Docker basics

Docker is a platform for building, running and shipping applications, like Laravel, or any other web development platform. It’s main advantage is that a Docker app can run on any machine, all the files needed to run the app are stored in the container. You first start with an image, this a like a recipe for what is needed to build the container. The Docker desktop app is the chef, and once you give it a recipe, it does the rest! Then you are left with a container on your local machine which you can start and stop as you need to.

You can also upload it to Docker Hub, which is kinda like Git Hub in the way it works. Others can then work in the same environment. This is great for teams because everyone is working with the same versions of the libraries you need to build web apps, rather than the versions they have on their local machines.

Another key benefit is that say you need to run an older version of PHP to work on updating a legacy site. Without Docker you need to downgrade your local install, or at least switch to an older version, meaning your newer sites won’t work. This gets old very quickly. With Docker you can run multiple containers with different versions of software, depending on the project requirements. This also means you can emulate precisely the server environment that your website is intended to be deployed on.

Containers vs Virtual Machines

If a lot of these benefits sound similar to VMs it’s because they are. However Docker has some key advantages which has led to it becoming hugely popular among web developers. A Virtual Machine emulates an entire operating system, typically Unix or Windows, so it is pretty big, in the case of Windows, GB’s in size. They are also quite slow to start, and they take up a large amount of your computer’s power.

Containers on the other hand are much smaller, as they do not include the whole operating system, just the apps needed, like Vue, Node etc..This makes them small and fast to start. They are also more easily transferable.

Docker Structure

In the diagram above you can see the difference in the structure of a Container and a Virtual Machine. Every VM has an operating system installed and running, meaning to run three of these requires a seriously powerful computer. The same is not true for Docker containers, which use the resources of the host operating system.

An excellent Docker course, which is often on sale, is this one.

https://codewithmosh.com/p/the-ultimate-docker-course

But the beauty of Docker is you don’t really need to know how it works to use it, it’s like driving a car. You only need to know how to operate it. So the steps to start and run a container are as follows.

  • Install Docker desktop from here – https://www.docker.com/products/docker-desktop
  • Install and run it
  • Find an image you want and download it – a linux image is a good starting point. You can do it really easily by entered this command in terminal : docker pull ubuntu
  • Then you can run it: docker run ubuntu

At this point you have a container running ubuntu on you local machine!

Common commands

  1. To pull a new image down, use docker pull.
  2. To run an already pulled image use docker run. Note that if the image isn’t installed locally docker will automatically ask if you want to pull it down.
  3. If you want to run a container and have interactions with it, like an Ubuntu image where you will want to access the terminal , use docker run -it ubuntu for instance, ‘it’ stands for interactive.
  4. To list all containers, docker ps -a
  5. To show all running processes docker ps

Using Docker from the command line is simple once the application has been installed. You can also start and stop containers from the Docker app.

Coding with Docker

But how do you actually code within a container, do you need to install an IDE? Nope. VS Code for instance has a Docker extension, which enables you to code locally, and all your changes are mirrored instantly in the Docker container. You can also use Volumes to store data outside of the container, so that if the container is destroyed you don’t lose all the data in it. This is beyond the scope of this post, but there are many YouTube tutorials on Docker.

Summary

Docker is an incredibly useful, and free to use app that has quickly become a major part of many web developers workflow. It is less useful to the solo programmer, but for teams working on large projects Docker, along with Git Hub integration, is almost essential .