Load Balancing Containers - Docker

Hi,

I was following this video and I wanted to check if I can use an env variable inside of the docker composer file. Instead of using version 3.2, I was willing to have the output of the command docker-compose -version.
Inside of the lb-challenge directory, I have created an .env file:

docker_ver=/usr/local/bin/docker-compose version | grep docker-py | awk '{print $NF;}'

Inside of the docker-compose file, here is what I have:
version: ${docker_ver}
services:
weather-app1:
build: ./weather-app
tty: true
networks:
- frontend
...
But when I am trying docker-compose config, I am getting this error related to my variable substitution:
TypeError: '<' not supported between instances of 'str' and 'int'
[15095] Failed to execute script docker-compose

Any idea how I can fix this error?

Many thanks,
Dan

  • post-author-pic
    Travis T
    01-11-2019

    Hey Dan,


    I'll have to look into it and get back to you.

    Thanks,
    Travis

  • post-author-pic
    Keith T
    01-11-2019

    Hi Dan,


    Behind the scenes, there is a version comparison going on and it can't do "3.2" < 3.0 because it's comparing a string to an integer. Because the environment variable will be a string you're going to continue to run into this issue. In this case, since the value needs to be an integer you're going to have a hard time using environment variables.

  • post-author-pic
    Travis T
    01-11-2019

    Okay so after giving it some thought dynamically assigning the version of Docker Compose is kind of a bad idea. The reason why is because you are specifying the version of the service definition. For example if your compose file was written for 2.4 and you change the version to 3.7 it’s not going t work.

  • post-author-pic
    Dan T
    01-14-2019

    Hi guys,

    Many thanks for your explanation. My point was if we can use a variable taken from the underlying OS(CentOS, in my case) inside of the docker-composer file. Might be a good usecase.
    So, we have 2 cases:
    - If the result of the command is an integer, it is a little bit more tricky to convert it to string;
    - If the result of the command is a string, I have tried something like this:

    [root@ip-10-0-1-208 lb-challenge]# cat .env 
    docker_ver=/usr/local/bin/docker-compose version | grep docker-py | awk '{print $1;}'


    [root@ip-10-0-1-208 lb-challenge]# cat docker-compose.yml
    version: '3.3'
    services:
    weather-app1:
    build: ./weather-app
    tty: true
    command: echo $docker_ver
    networks:
    - frontend
    weather-app2:
    build: ./weather-app
    tty: true
    networks:
    - frontend
    weather-app3:
    build: ./weather-app
    tty: true
    networks:
    - frontend
    loadbalancer:
    build: ./load-balancer
    tty: true
    ports:
    - 80:80
    networks:
    - frontend

    networks:
    frontend:

    The docker-compose file is build, but right after deploying, I am getting lb-challenge_weather-app1_1 down. I have tried to restart it, but it is getting down again.
    [root@ip-10-0-1-208 lb-challenge]# docker ps -a 
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    080f336a93af lb-challenge_weather-app2 "/bin/sh -c ./bin/www" 11 minutes ago Up 11 minutes 3000/tcp lb-challenge_weather-app2_1
    b3a17fb9d8a5 lb-challenge_loadbalancer "nginx -g 'daemon of…" 11 minutes ago Up 11 minutes 0.0.0.0:80->80/tcp lb-challenge_loadbalancer_1
    563882c48cb2 lb-challenge_weather-app3 "/bin/sh -c ./bin/www" 11 minutes ago Up 11 minutes 3000/tcp lb-challenge_weather-app3_1
    c1622fedced1 lb-challenge_weather-app1 "echo /usr/local/bin…" 11 minutes ago Exited (0) 3 minutes ago lb-challenge_weather-app1_1

    Might be a problem because of my echo command that I have introduced in the weather-app1?

    Many thanks,
    Dan

  • post-author-pic
    Travis T
    01-15-2019

    Yes challenge_weather-app1_1  is going to stop because it is executing the echo command and then stopping the container. It’s acting as a one off task rather than a long running process so the container will stop. Also, if you are wanting to parameterize your compose file you can do so using environment variables.

  • post-author-pic
    Dan T
    01-21-2019

    Hi Travis,

    I have tried to name the container after one output of a command like this:

    [root@ip-10-0-1-208 lb-challenge]# cat .env 
    docker_ver=/usr/local/bin/docker-compose version | grep docker-py | awk '{print $1;}'

    [root@ip-10-0-1-49 lb-challenge]# cat docker-compose.yml
    version: '3.3'
    services:
    weather-app1:
    build: ./weather-app
    container_name: ${docker_ver}
    tty: true
    command: echo $docker_ver
    networks:
    - frontend
    weather-app2:
    build: ./weather-app
    tty: true
    networks:
    - frontend
    weather-app3:
    build: ./weather-app
    tty: true
    networks:
    - frontend
    loadbalancer:
    build: ./load-balancer
    tty: true
    ports:
    - 80:80
    networks:
    - frontend

    networks:
    frontend:


    Although, I am doing something wrong. When the containers are deployed, the weather-app1 tries to get the name as "/usr/local/bin/docker-compose version | grep docker-py | awk '{print $1;}'", not the output of this command. Can you please let me know how I can fix this?


    Best regards,
    Dan

  • post-author-pic
    Travis T
    01-21-2019

    Okay you can pass variables one of two ways. Export the variable as seen below. Then execute docker-compose up.

    export docker_ver="$(docker-compose version | grep docker-py | awk '{print $1;}')"
    Or you can pass the variable when you execute docker-compose up.
    docker_ver="$(docker-compose version | grep docker-py | awk '{print $1;}')" docker-compose up
    Is this what you are looking for?




  • post-author-pic
    Dan T
    01-23-2019

    Hi Travis,

    Yes, this is what I was looking for and it works. I thought that I have to pass it to Docker's env file, not to Linux's env file.

    Thank you,
    Dan

  • post-author-pic
    Travis T
    01-23-2019

    Good deal

Looking For Team Training?

Learn More