Learn docker docker compose -- scale from scratch

Keywords: Docker Redis Python Programming

Environment preparation, the flash redis project in the previous section, starts the project:

docker-compose up -d
Starting flask-redis_web_1   ... done
Starting flask-redis_redis_1 ... done

View status:

docker-compose ps
       Name                      Command               State           Ports
-------------------------------------------------------------------------------------
flask-redis_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp
flask-redis_web_1     python app.py                    Up      0.0.0.0:8080->5000/tcp

docker-compose scaleĀ 

Currently, there is only one service container in our docker compose. You can extend the service through scale.

docker-compose up --help
    --scale SERVICE=NUM        Scale SERVICE to NUM instances. Overrides the
                               `scale` setting in the Compose file if present.

For example, change the number of web services to three:

docker-compose up --scale web=3 -d
WARNING: The "web" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash.
Starting flask-redis_web_1 ...
Starting flask-redis_web_1 ... done
Creating flask-redis_web_2 ... error
Creating flask-redis_web_3 ... error

ERROR: for flask-redis_web_2  Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_2 (952f3eea8bdf14f0a94845fb6ef5039285f7ffe40620faca32f40643b802fa97): Bind for 0.0.0.0:8080 failed: port is already allocated

ERROR: for flask-redis_web_3  Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_3 (50c776deb73b272d04181a8ab385f27cc31689aeeed804436fd2b92836cf25b9): Bind for 0.0.0.0:8080 failed: port is already allocated

ERROR: for web  Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_2 (952f3eea8bdf14f0a94845fb6ef5039285f7ffe40620faca32f40643b802fa97): Bind for 0.0.0.0:8080 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.

An error was found because the interface has been assigned. The port of each container wants to be bound to the 8080, which is obviously impossible.

To delete a container:

docker-compose down
Stopping flask-redis_web_1   ... done
Stopping flask-redis_redis_1 ... done
Removing flask-redis_web_3   ... done
Removing flask-redis_web_2   ... done
Removing flask-redis_web_1   ... done
Removing flask-redis_redis_1 ... done
Removing network flask-redis_default

Delete the port binding in docker-compose.yml:

version: "3"

services:

  redis:
    image: redis

  web:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      REDIS_HOST: redis

Execute the command docker compose up -- scale web = 3 - D

docker-compose up --scale web=3 -d
Creating network "flask-redis_default" with the default driver
Creating flask-redis_web_1   ... done
Creating flask-redis_web_2   ... done
Creating flask-redis_web_3   ... done
Creating flask-redis_redis_1 ... done

Can be executed successfully.

docker-compose ps
       Name                      Command               State    Ports
-----------------------------------------------------------------------
flask-redis_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp
flask-redis_web_1     python app.py                    Up      5000/tcp
flask-redis_web_2     python app.py                    Up      5000/tcp
flask-redis_web_3     python app.py                    Up      5000/tcp

It is found that the web port is not mapped to the host's port, all of which are 5000container ports.

Posted by [uk]stuff on Sat, 02 Nov 2019 02:54:00 -0700