Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
A Developer's Essential Guide to Docker Compose

You're reading from   A Developer's Essential Guide to Docker Compose Simplify the development and orchestration of multi-container applications

Arrow left icon
Product type Paperback
Published in Oct 2022
Publisher Packt
ISBN-13 9781803234366
Length 264 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Emmanouil Gkatziouras Emmanouil Gkatziouras
Author Profile Icon Emmanouil Gkatziouras
Emmanouil Gkatziouras
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Part 1: Docker Compose 101
2. Chapter 1: Introduction to Docker Compose FREE CHAPTER 3. Chapter 2: Running the First Application Using Compose 4. Chapter 3: Network and Volumes Fundamentals 5. Chapter 4: Executing Docker Compose Commands 6. Part 2: Daily Development with Docker Compose
7. Chapter 5: Connecting Microservices 8. Chapter 6: Monitoring Services with Prometheus 9. Chapter 7: Combining Compose Files 10. Chapter 8: Simulating Production Locally 11. Chapter 9: Creating Advanced CI/CD Tasks 12. Part 3: Deployment with Docker Compose
13. Chapter 10: Deploying Docker Compose Using Remote Hosts 14. Chapter 11: Deploying Docker Compose to AWS 15. Chapter 12: Deploying Docker Compose to Azure 16. Chapter 13: Migrating to Kubernetes Configuration Using Compose 17. Index 18. Other Books You May Enjoy

Using your Docker image on Docker Compose

By using Compose, we have achieved running the default NGINX image and changing the default HTML page that was displayed. Since we have started utilizing Compose, we will proceed with using and testing custom Docker images.

For our use case, we want to develop an NGINX image that prints logs in JSON format since it’s feasible for tools such as CloudWatch (https://aws.amazon.com/cloudwatch/), StackDriver (https://cloud.google.com/products/operations), and ELK Stack (https://www.elastic.co/elastic-stack/) to persist data in JSON format and offer enhanced querying capabilities by having field conditions based on JSON elements.

The problem will require us to identify how NGINX defines the current logging format. Since we have a container already running through Compose, we will shell into the container and check the configuration:

$  docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS                  NAMES
dc0ca7ebe0cb   nginx     "/docker-entrypoint.…"   7 hours ago   Up 7 hours   0.0.0.0:8080->80/tcp   chapter1-nginx-1
$ docker exec -it chapter1-nginx-1 cat /etc/nginx/nginx.conf
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

By finding our running container using docker ps and issuing cat, through the container shell, we retrieved the current log_format from the instance by checking the /etc/nginx/nginx.conf file. We will change this format to JSON and build a custom Docker image preloaded with that format.

We will copy the file locally to apply the change:

$  docker cp chapter1-nginx-1:/etc/nginx/nginx.conf nginx.conf

By editing nginx.conf instead of log_format, we set the json format:

log_format  main escape=json '{"remote_addr":"$remote_addr","remote_user":"$remote_user","time":"[$time_local]","request":"$request",'
                     '"status":"$status","body_bytes_sent":"$body_bytes_sent","http_referer":"$http_referer",'
                      '"http_user_agent":"$http_user_agent","http_x_forwarded_for":"$http_x_forwarded_for"}';             

Our file will look like this:

user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  escape=json '{"remote_addr":"$remote_addr","remote_user":"$remote_user","time":"[$time_local]","request":"$request",'
                      '"status":"$status","body_bytes_sent":"$body_bytes_sent","http_referer":"$http_referer",'
                      '"http_user_agent":"$http_user_agent","http_x_forwarded_for":"$http_x_forwarded_for"}';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

Now that we have the config file needed, we will create the base NGINX image that will use this configuration. The Dockerfile will be the following:

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf

Let’s build the image:

$ docker build -t custom-nginx:0.1 .

Let’s go ahead and use it with the recently created docker-compose.yaml file:

services:
  nginx:
    image: custom-nginx:0.1
    ports:
      - 8080:80
    volumes:
      - ./static-site:/usr/share/nginx/html
$ docker compose up
…
chapter1-nginx-1  | 2022/02/10 08:09:27 [notice] 1#1: start worker process 33
chapter1-nginx-1  | {"remote_addr":"172.19.0.1","remote_user":"","time":"[10/Feb/2022:08:09:33 +0000]","request":"GET / HTTP/1.1","status":"200","body_bytes_sent":"177","http_referer":"","http_user_agent":"curl/7.77.0","http_x_forwarded_for":""}
…

By now, Compose runs successfully on your application that also uses the custom Docker image. So far, Compose was sufficient to use a custom image and also include some modification at runtime such as mounting a file as well as doing port mapping. The results were the same as the ones we would expect if we run the application using Docker commands.

You have been reading a chapter from
A Developer's Essential Guide to Docker Compose
Published in: Oct 2022
Publisher: Packt
ISBN-13: 9781803234366
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