Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify an iterator in the volume path when using docker-compose to scale up service?

Background: I'm using docker-compose in order to place a tomcat service into a docker swarm cluster but I'm presently struggling with how I would approach the logging directory given that I want to scale the service up yet retain the uniqueness of the logging directory.

Consider the (obviously) made up docker-compose which simply starts tomcat and mounts a logging filesystem in which to capture the logs.

version: '2' services: tomcat: image: "tomcat:latest" hostname: tomcat-example command: /start.sh volumes: - "/data/container/tomcat/logs:/opt/tomcat/logs,z"

Versions

  • docker 1.11
  • docker-compose 1.7.1
  • API version 1.21

Problem: I'm looking to understand how I would approach inserting a variable into the 'volume' log path so that the log directory is unique for each instance of the scaled service

say,

volumes:
    - "/data/container/tomcat/${container_name}/logs:/opt/tomcat/logs,z"

I see that based on project name (or directory I'm in) the container name is actually known, so could I use this ?

eg, setting the project name to 'tomcat' and running docker-compose scale tomcat=2 I would see the following containers.

  • hostname/tomcat_1
  • hostname/tomcat_2

So is there any way I could leverage this as a variable in the logging volume, Other suggestions or approaches welcome. I realise that I could just specify a relative path and let the container_id take care of this, but now if I attach splunk or logstash to the logging devices I'd need to know which ones are indeed logging devices as opposed to the base containers f/s. However Ideally I'm looking use a specific absolute path here.

Thanks in advance dockers! R.

like image 555
Snowy Avatar asked Jul 21 '16 12:07

Snowy


People also ask

Which command is used to scale the services in docker?

The scale command enables you to scale one or more replicated services either up or down to the desired number of replicas. This command cannot be applied on services which are global mode. The command will return immediately, but the actual scaling of the service may take some time.

How do I specify volumes in Dockerfile?

In Dockerfile you can specify only the destination of a volume inside a container. e.g. /usr/src/app . When you run a container, e.g. docker run --volume=/opt:/usr/src/app my_image , you may but do not have to specify its mounting point ( /opt ) on the host machine.

Can Docker compose auto scale?

Docker – more specifically Docker-Compose – provides a solution with auto-scaling groups. This would spool up 10 instances of myservice. Awesome! They'll maintain the links to other instances, and docker-compose handles the naming.


1 Answers

You should really NOT log to the filesystem, and use a specialized log management tool like graylog/logstash/splunk/... instead. Either configure your logging framework in Tomcat with a specific appender, or log to sysout and configure a logging driver in Docker to redirect your logs to the external destination.

This said, if you really want to go the filesystem way, simply use a regular unnamed volume, and then call docker inspect on your container to find the volume's path on the filesystem :

[...snip...]
"Mounts": [
    {
        "Type": "volume",
        "Name": "b8c...SomeHash...48d6e",
        "Source": "/var/lib/docker/volumes/b8c...SomeHash...48d6e/_data",
        "Destination": "/opt/tomcat/logs",
[...snip...]

If you want to have nice-looking names in a specific location, use a script to create symlinks.

Yet, I'm still doubtfull on this solution, especially in a multi-host swarm context. Logging to an external, specialized service is the way to go in your use case.

like image 167
lbndev Avatar answered Oct 06 '22 00:10

lbndev