In part 3, we scale our application and enable load-balancing. To do this, we must go one level up in the hierarchy of a distributed application: the service.
In a distributed application, different pieces of the app are called “services.” For example, if you imagine a video sharing site, it probably includes a service for storing application data in a database, a service for video transcoding in the background after a user uploads something, a service for the front-end, and so on.
Services are really just “containers in production.” A service only runs one image, but it codifies the way that image runs—what ports it should use, how many replicas of the container should run so the service has the capacity it needs, and so on. Scaling a service changes the number of container instances running that piece of software, assigning more computing resources to the service in the process.
Luckily it’s very easy to define, run, and scale services with the Docker platform -- just write a docker-compose.yml file.
A docker-compose.yml file is a YAML file that defines how Docker containers should behave in production.
Save this file as docker-compose.yml wherever you want. Be sure you have pushed the image you created in Part 2 to a registry, and update this .yml by replacing username/repo:tag with your image details.
version: "3"services:web:# replace username/repo:tag with your name and image detailsimage: username/repo:tagdeploy:replicas: 5resources:limits:cpus: "0.1"memory: 50Mrestart_policy:condition: on-failureports:- "80:80"networks:- webnetnetworks:webnet:
This docker-compose.yml file tells Docker to do the following:
Pull the image we uploaded in step 2 from the registry.
Before we can use the docker stack deploy command we first run:
docker swarm init
Note: We get into the meaning of that command inpart 4. If you don’t run docker swarm init you get an error that “this node is not a swarm manager.”
Now let’s run it. You need to give your app a name. Here, it is set to getstartedlab:
Our single service stack is running 5 container instances of our deployed image on one host. Let’s investigate.
Get the service ID for the one service in our application:
docker service ls
Look for output for the web service, prepended with your app name. If you named it the same as shown in this example, the name is getstartedlab_web. The service ID is listed as well, along with the number of replicas, image name, and exposed ports.
A single container running in a service is called a task. Tasks are given unique IDs that numerically increment, up to the number of replicas you defined in docker-compose.yml. List the tasks for your service:
docker service ps getstartedlab_web
Tasks also show up if you just list all the containers on your system, though that is not filtered by service:
docker container ls -q
You can run curl -4 http://localhost several times in a row, or go to that URL in your browser and hit refresh a few times.
Either way, the container ID changes, demonstrating the load-balancing; with each request, one of the 5 tasks is chosen, in a round-robin fashion, to respond. The container IDs match your output from the previous command (docker container ls -q).
Running Windows 10?
Slow response times?
You can scale the app by changing the replicas value in docker-compose.yml, saving the change, and re-running the docker stack deploy command:
docker stack deploy -c docker-compose.yml getstartedlab
Docker performs an in-place update, no need to tear the stack down first or kill any containers.
Now, re-run docker container ls -q to see the deployed instances reconfigured. If you scaled up the replicas, more tasks, and hence, more containers, are started.
docker stack rm getstartedlab
docker swarm leave --force
It’s as easy as that to stand up and scale your app with Docker. You’ve taken a huge step towards learning how to run containers in production. Up next, you learn how to run this app as a bonafide swarm on a cluster of Docker machines.
Note: Compose files like this are used to define applications with Docker, and can be uploaded to cloud providers using Docker Cloud, or on any hardware or cloud provider you choose with Docker Enterprise Edition.
To recap, while typing docker run is simple enough, the true implementation of a container in production is running it as a service. Services codify a container’s behavior in a Compose file, and this file can be used to scale, limit, and redeploy our app. Changes to the service can be applied in place, as it runs, using the same command that launched the service: docker stack deploy.
Some commands to explore at this stage:
docker stack ls # List stacks or appsdocker stack deploy -c <composefile> <appname> # Run the specified Compose filedocker service ls # List running services associated with an appdocker service ps <service> # List tasks associated with an appdocker inspect <task or container> # Inspect task or containerdocker container ls -q # List container IDsdocker stack rm <appname> # Tear down an applicationdocker swarm leave --force # Take down a single node swarm from the manager