Deploying Node.js: Setting up Docker on your laptop or computer

The best place to learn how to install Docker on your laptop is the

Docker documentation. What we’re looking for is the Docker Community Edition (CE), which is all we need:

  • macOS installation: https://docs.docker.com/docker-for-mac/install/
  • Windows installation: https://docs.docker.com/docker-for-windows/install/
  • Ubuntu installation: https://docs.docker.com/install/linux/docker-ce/ubuntu/

Instructions are also available for several other distributions. Some useful post-install Linux instructions are available at https://docs.docker.com/install/linux/linux- postinstall/.

Docker runs natively on Linux, and the installation is simply the Docker daemon and command-line tools. To run Docker on macOS or Windows, you need to install the Docker for Windows or Docker for Mac applications. These applications manage a virtual Linux environment in a lightweight virtual machine, within which is a Docker Engine instance running on Linux. In the olden days (a few years ago), we had to handcraft that setup. Thanks must be given to the Docker team, who have made this as easy as installing an application, and all the complexity is hidden away. The result is very lightweight, and Docker containers can be left running in the background with little impact.

Let’s now learn how to install Docker on a Windows or macOS machine.

1. Installing and starting Docker with Docker for Windows or macOS

The Docker team has made installing Docker on Windows or macOS very easy. You simply download an installer and, as with most other applications, you run the installer. It takes care of installation and provides you with an application icon that is used to launch Docker. On Linux, the installation is a little more involved, so it is best to read and follow the official instructions.

Starting Docker for Windows or macOS is very simple, once you’ve followed the installation instructions. You simply find and double-click on the application icon. There are settings available so that Docker automatically launches every time you start your laptop.

On both Docker for Windows and Docker for Mac, the CPU must support virtualization. Bundled inside Docker for Windows and Docker for Mac is an ultra-lightweight hypervisor, which, in turn, requires virtualization support from the CPU.

For Windows, this may require a BIOS configuration. Refer to https://docs.docker.com/docker-for-windows/troubleshoot/#virtualization-must-be-enabled for more information.

For macOS, this requires hardware from 2010 or later, with Intel’s hardware support for Memory Management Unit (MMU) virtualization, including Extended Page Tables (EPTs) and unrestricted mode. You can check for this support by running sysctl kern.hv_support. It also requires macOS 10.11 or later.

Having installed the software, let’s try it out and familiarize ourselves with Docker.

2. Familiarizing ourselves with Docker

With the setup accomplished, we can use the local Docker instance to create Docker containers, run a few commands, and, in general, learn how to use it.

As in so many software journeys, this one starts with Hello World:

$ docker run hello-world

Unable to find image ‘hello-world:latest’ locally

latest: Pulling from library/hello-world

ca4f61b1923c: Pull complete

Digest: sha256:66ef312bbac49c39a89aa9bcc3cb4f3c9e7de3788c94415 8df3ee0176d32b751

Status: Downloaded newer image for hello-world:latest 

Hello from Docker!

This message shows that your installation appears to be working correctly. 

To try something more ambitious, you can run an Ubuntu container with:

$ docker run -it ubuntu bash 

The docker run command downloads a Docker image, named on the command line, initializes a Docker container from that image, and then runs that container. In this case, the image, named hello-world, was not present on the local computer and had to be downloaded and initialized. Once that was done, the hello-world container was executed and it printed out these instructions.

The docker run hello-world command is a quick way to verify that Docker is installed correctly.

Let’s follow the suggestion and start an Ubuntu container:

$ docker run -it ubuntu bash

Unable to find image ‘ubuntu:latest’ locally

latest: Pulling from library/ubuntu

5c939e3a4d10: Pull complete

c63719cdbe7a: Pull complete

19a861ea6baf: Pull complete

651c9d2d6c4f: Pull complete

Digest: sha256:8d31dad0c58f552e890d68bbfb735588b6b820a46e4596 72d96e585871acc110

Status: Downloaded newer image for ubuntu:latest 

root@83058f30a327:/# uname -a

Linux 83058f30a327 4.14.131-linuxkit #1 SMP Fri Jul 19 12:31:17 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

root@83058f30a327:/# exit

$ uname -a

Darwin MacBook-Pro-4 17.7.0 Darwin Kernel Version 17.7.0: Thu Jan 23 07:05:23 PST 2020;

root:xnu-4570.71.69~1/RELEASE_X86_64 x86_64 

The Unable to find image phrase means that Docker has not downloaded the named image yet. Therefore, it downloaded not only the Ubuntu image but also the images it depends on. Any Docker image can be built in layers, meaning we always define an image in terms of a base image. In this instance, we see that the Ubuntu image required four layers in total.

Images are identified by an SHA-256 hash, and there is both a long-form identifier and a short-form identifier. We can see both the long and short identifiers in this output.

The docker run command downloads an image, configures it for execution, and executes the image. The -it flag means to run the image interactively in the terminal.

In the docker run command line, the part after the image name to execute is passed into the container as command options to execute. In this case, the command option says to run bash, which is the default command shell. Indeed, we were given a command prompt and can run Linux commands.

You can query your computer to see that while the hello-world container has executed and finished, it still exists:

The docker ps command lists the running Docker containers. As we see here, the hello-world container is no longer running, but the Ubuntu container is. With the -a switch, docker ps also shows those containers that exist but are not currently running.

The last column is the container name. Since we didn’t specify a container name when launching the container, Docker created a semi-random name for us.

When you’re done using a container, you can clean up with the following command:

$ docker rm clever_napier

clever_napier 

The clever_napier name is the container name automatically generated by Docker. While the image name was hello-world, that was not the container name. Docker generated the container name so that you have a more user-friendly identifier for the containers than the hex ID shown in the CONTAINER ID column:

$ docker rm 83058f30a327 83058f30a327 

It’s also possible to specify the hex ID. However, it is, of course, more user friendly to have a name for the container than a hex ID. When creating a container, it’s easy to specify any container name you like.

We’ve installed Docker on our laptop or computer and tried a couple of simple commands to familiarize ourselves with Docker. Let’s now get down to some work. We’ll start by setting up the user authentication service in Docker containers.

Source: Herron David (2020), Node.js Web Development: Server-side web development made easy with Node 14 using practical examples, Packt Publishing.

Leave a Reply

Your email address will not be published. Required fields are marked *