Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Ultimate Docker Container Book

You're reading from   The Ultimate Docker Container Book Build, test, ship, and run containers with Docker and Kubernetes

Arrow left icon
Product type Paperback
Published in Aug 2023
Publisher Packt
ISBN-13 9781804613986
Length 626 pages
Edition 3rd Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Dr. Gabriel N. Schenker Dr. Gabriel N. Schenker
Author Profile Icon Dr. Gabriel N. Schenker
Dr. Gabriel N. Schenker
Arrow right icon
View More author details
Toc

Table of Contents (26) Chapters Close

Preface 1. Part 1:Introduction
2. Chapter 1: What Are Containers and Why Should I Use Them? FREE CHAPTER 3. Chapter 2: Setting Up a Working Environment 4. Part 2:Containerization Fundamentals
5. Chapter 3: Mastering Containers 6. Chapter 4: Creating and Managing Container Images 7. Chapter 5: Data Volumes and Configuration 8. Chapter 6: Debugging Code Running in Containers 9. Chapter 7: Testing Applications Running in Containers 10. Chapter 8: Increasing Productivity with Docker Tips and Tricks 11. Part 3:Orchestration Fundamentals
12. Chapter 9: Learning about Distributed Application Architecture 13. Chapter 10: Using Single-Host Networking 14. Chapter 11: Managing Containers with Docker Compose 15. Chapter 12: Shipping Logs and Monitoring Containers 16. Chapter 13: Introducing Container Orchestration 17. Chapter 14: Introducing Docker Swarm 18. Chapter 15: Deploying and Running a Distributed Application on Docker Swarm 19. Part 4:Docker, Kubernetes, and the Cloud
20. Chapter 16: Introducing Kubernetes 21. Chapter 17: Deploying, Updating, and Securing an Application with Kubernetes 22. Chapter 18: Running a Containerized Application in the Cloud 23. Chapter 19: Monitoring and Troubleshooting an Application Running in Production 24. Index 25. Other Books You May Enjoy

Building images with Docker Compose

To demonstrate how to build a Docker image using Docker Compose, we need a small application. Proceed as follows:

  1. In the chapter’s folder (ch11), create a subfolder, step2, and navigate to it:
    mkdir step2 && cd step2
  2. From the previous exercise, copy the db folder containing the database initialization script to the step2 folder and also copy the docker-compose.yml file:
    $ cp -r ../step1/db .
    $ cp ../docker-compose.yml .
  3. Create a folder called web in the step2 folder. This folder will contain a simple Express.js web application.
  4. Add a file called package.json to the folder with this content:
Figure 11.8 – The package.json file of the sample web application

Figure 11.8 – The package.json file of the sample web application

Note

If you prefer not to type yourself, you can always download the files from the sample solution: https://github.com/PacktPublishing/The-Ultimate-Docker-Container-Book/tree/main/sample-solutions/ch11/step2.

  1. Create a folder called src inside the web folder.
  2. Add a file called server.js to the src folder with this content:
Figure 11.9 – The server.js file of the sample web application

Figure 11.9 – The server.js file of the sample web application

This file contains the complete logic for our simple web application. Of interest is specifically the logic for the /animal endpoint on lines 32 to 40. Also note how we connect to the PostgreSQL database using a constant pool of type Pool (lines 15 to 21). The username, password, and database name should match the ones we define for the database.

  1. Add another file called index.html to the src folder with this content:
Figure 11.10 – The index.html file of the sample web application

Figure 11.10 – The index.html file of the sample web application

This file serves as a template to display the image of a wild animal.

  1. Add a folder called public/css to the web folder:
    mkdir -p public/css
  2. Add a file called main.css to this public/css folder, which we will use to style our sample web application. Add this content to the file:
Figure 11.11 – The main.css file of the sample web application

Figure 11.11 – The main.css file of the sample web application

  1. Now we need some real images to display. The easiest way is to copy our sample images from GitHub:
    1. Create a folder called images in the public folder.
    2. Then, download all images into this images folder, which you can find here: https://github.com/PacktPublishing/The-Ultimate-Docker-Container-Book/tree/main/sample-solutions/ch11/step2/web/public/images.
  2. We now need to make a small addition to the docker-compose.yml file that we have copied from the step1 folder. Locate the docker-compose.yml file in the step2 folder, open it, and after line 4, add this snippet:
    ports:
    - 5432:5432

    The result should look like this:

Figure 11.12 – Add host port mapping to the db service

Figure 11.12 – Add host port mapping to the db service

This way, we can actually access the database from any application running on the host. We will use this possibility in the coming steps.

  1. Now, we are ready to run and test this application:
    1. Run the database using the docker-compose file and this command:
    $ docker compose up db --detach

    We are telling Docker Compose to only start the db service and to run it in detach mode, indicated by the --detach parameter.

    1. Navigate to the web folder:
    $ cd web
    1. Install all dependencies with the following:
    $ npm install
    1. Run the application using the following:
    $ npm run start

    You should see this:

Figure 11.13 – Running the web application natively

Figure 11.13 – Running the web application natively

  1. Open a browser tab and navigate to http://localhost:3000/animal and you should see something like this:
Figure 11.14 – The web application running and displaying a wild animal

Figure 11.14 – The web application running and displaying a wild animal

  1. Refresh the browser a few times and notice that each time, a new random animal is displayed.
  2. Before you leave, make sure to stop the web application and stop the other containers with docker compose down.

Great, now we can move on to the next step, where we will Dockerize the web application and use Docker Compose to build the image:

  1. Add a file called Dockerfile to the web folder and add this snippet:
Figure 11.15 – Dockerfile for the web application

Figure 11.15 – Dockerfile for the web application

Analyze this Dockerfile and try to understand what it does exactly. Refer to what you learned in Chapter 4, Creating and Managing Container Images, if needed.

  1. Open the docker-compose.yml file from the step2 folder and add the definition of the web service, right after the db and pgadmin services and before the volumes section (that is, after line 24). The snippet to add should look like this:
Figure 11.16 – Defining the service called web in the docker-compose.yml file

Figure 11.16 – Defining the service called web in the docker-compose.yml file

Make sure that on line 2, you replace the gnschenker username with your own Docker Hub username.

  1. Build the image using this command:
    $ docker compose build web

    The preceding command assumes that you are in the step2 folder and that there is a docker-compose.yml file located in that folder.

When building the image, Docker looks for and uses a Dockerfile in the web folder, as instructed by the build: web instruction on line 3 in the preceding snippet.

To build images using Docker Compose, use the following instructions:

  1. Open a terminal window.
  2. Make sure that you are in the ch11/step2 subfolder of the The-Ultimate-Docker-Container-Book folder:
    $ cd ~/ The-Ultimate-Docker-Container-Book/ch11/step2
  3. Then, build the images:
    $ docker compose build

    If we enter the preceding command, then the tool will assume that there must be a file in the current directory called docker-compose.yml and it will use that one to run. In our case, this is indeed the case, and the tool will build the images.

  4. Observe the output in your terminal window. You should see something like this:
Figure 11.17 – Building the Docker image for the web service

Figure 11.17 – Building the Docker image for the web service

In the preceding screenshot, you can see that docker-compose first downloads the base image, node:19.7-alpine, for the web image we’re building from Docker Hub. Subsequently, it uses the Dockerfile found in the web folder to build the image and names it gnschenker/ch11-web:2.0.

After building the Docker image for the web service, we are ready to use Docker Compose to run the whole multi-service application.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image