Installing and Configuring Docker

First, you need to get Docker.[70] If you’re on Linux, you’ll be running the Docker daemon and CLI directly on your machine, whereas if you’re using Windows or Mac, you’ll download an application that will manage a VM for you. For Scala Native, I recommend configuring the VM with at least 4GB of RAM for fast build times.

Once Docker is installed and running, we can start creating containers. We can go to a terminal and execute this:

 $ docker run -it nginx

Docker will download an image and run the nginx web server in an isolated container.

Elsewhere in the book, I’ll use command prompts like this, beginning with a bare $, to indicate Docker or networking-related commands to be run outside of the container environment.

If we then switch to another terminal, we can use docker ps to check on it:

 $ docker ps
 CONTAINER ID IMAGE COMMAND CREATED
 dd95c11bd40e nginx "nginx -g 'daemon of…" Less than a second ago
 STATUS PORTS NAMES
 Up 1 second 443/tcp, 80/tcp inspiring_ramanujan

This tells us that the nginx process is running and is listening on ports 443 and 80; however, if we try to connect to localhost:80 with a web browser, we won’t be able to connect because Docker containers are isolated by default. If we want this web server to be accessible, even from our own machine, we need to map the port on the container to one that’s available on our host machine. We can map the port by passing additional arguments to docker run: if we do docker run -p 80 -it nginx, Docker will map port 80 to a random, free port on our host:

 CONTAINER ID IMAGE COMMAND CREATED
 e6ef6a3f55d8 nginx "nginx -g 'daemon of…" Less than a second ago
 STATUS PORTS NAMES
 Up 1 second 443/tcp, 0.0.0.0:32770->80/tcp thirsty_bassi

This tells us the port 32770 on our host (0.0.0.0 or 127.0.0.1 or localhost) is mapped to port 80 on the container. However, note that the assigned port is random and you may have a different port on your system. Now, we can point a browser at localhost:32770 (or whichever port you’ve been assigned) and we should see a Welcome to nginx! page, as well some log lines in our docker run terminal, indicating that it has received HTTP requests and served a successful response.