Using Docker
In the previous section, we explored the fundamentals of cloud architectures. We’re now going to move on from theory to practice by installing and configuring Docker step by step to launch our first container.
Docker is not just a tool but a paradigm shift—a new era where software can be packaged and isolated, ensuring consistency across environments. By the end of this section, Docker will be more than a concept; it will be an integral part of your toolbox, starting with the deployment of an NGINX container.
Installing Docker
Fortunately for us, the Docker team has provided a script that simplifies the installation process. This script is compatible with a range of Linux distributions, including Red Hat Enterprise Linux (RHEL), CentOS, Fedora, Debian, Ubuntu, and their derivatives.
To install Docker, you will need to run the script with root privileges. Open your terminal and enter the following command:
# curl -s https://get.docker.com | bash
With this single command, Docker will be installed natively on your system using custom repositories adapted to your Linux distribution’s package manager. This means that updating Docker will be as easy as updating any other package on your system.
Your first Docker container
There are many ways to operate Docker containers. For instance, you can launch a container using a simple command:
root@docker:~# docker run -d nginx latest: Pulling from library/nginx e1caac4eb9d2: Pull complete
This command pulls the NGINX image from Docker Hub and starts a new container in detached mode. However, this container runs with default settings and isn’t yet configured for specific use.
To customize the container, you can pass additional parameters. For example, to map the container’s port 80 to the host’s port 80, allowing web traffic to reach the container, you would run the following:
root@docker:~# docker run -d nginx -p 80:80
The first command launches the NGINX container with the default configuration. The second command runs the same container, but now it is accessible via the host machine’s port 80. But what if you require a more advanced and complete configuration? This is where Docker Compose steps in, offering a solution for managing multi-container Docker applications with ease.
In the context of this book, the NGINX image serves as an excellent example of Docker’s capabilities as it showcases how containerization simplifies the deployment of services that traditionally require dedicated servers.
As we explore deeper, we’ll see how to tailor an NGINX container to serve static content or act as a reverse proxy, introducing the concept of Docker volumes and how to use them to serve custom configuration files and content.
Simplifying with Docker Compose
After running our first Docker container, it became clear that managing a container’s parameters directly from the command line can quickly become cumbersome. Docker Compose simplifies this process by allowing us to define and run multi-container Docker applications using a YAML file for configuration.
Let’s create an equivalent setup to the one we ran in the previous section—an NGINX container with port 80 exposed to the host machine:
- Begin by creating a
/root/nginx
directory, and within that directory, save a file nameddocker-compose.yml
with the following content:version: '3' services: nginx: image: nginx:latest ports: - "80:80"
A reminder
Although the file format is called YAML, the extension needs to be .yml
.
In this docker-compose.yml
file, we’ve defined a service named nginx
, using the official nginx
image tagged with latest
, which means it will be updated every time we pull it from Docker Hub. If you wish, you can specify a fixed version, such as nginx:1.25.4
. More details can be found on the Docker Hub for NGINX page (https://hub.docker.com/_/nginx/).
- Now, with the Docker Compose file saved, run the following command in the same directory:
root@nginx:~/nginx# docker compose up [+] Running 1/1 ✔ Container nginx-nginx-1 Created
- You’ve just launched your first container using Docker Compose. To stop this container, simply use the Ctrl + C shortcut (
^C
). Alternatively, you can start the container in detached mode withup -d
and stop it withdown
:root@nginx:~/nginx# docker compose up -d root@nginx:~/nginx# docker compose down
With Docker Compose, running NGINX in Docker becomes a matter of defining the desired state in a file, which is easier to manage and read than standalone commands. In the next section, we’ll explore how to further configure NGINX in Docker, tailoring it to our specific needs.