Saturday 5 March 2016

Dockers: part 4 - Managing multiple services

Introduction:
  1. Your application might have a lot of services. Its best to run each individual service inside their own docker container.
  2. However, dealing manually with multiple docker containers is hard. Building them, bringing them up and down, linking them withe each other and killing them are not so easy tasks to do one by one manually.
  3. docker-compose is a utility who makes dealing with orchestration processes (start, shutdown, link, volumes) of docker containers very easy task.
  4. Using compose is a three step process
    • Define your app's environment with DockerFile.
    • Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
    • Use docker-compose commands to manage whole lifecycle of your application:
          Start/Stop/rebuild services
          View status of running services
          Stream the log output of running services
          Run a one-off command on service
  5. Read "docker-compose File" section to see how the .yml file looks like.

Installation (Ubuntu):
  1. Install Docker. See instructions here.
  2. Run following
    curl -L https://github.com/docker/compose/releases/download/1.6.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
  3. Make the binary executable
    chmod +x /usr/local/bin/docker-compose
  4. Test installation
    docker-compose --version

docker-compose File:

build:
Configuration options that are applied at build time.
context:     Define build context.
dockerfile: File from which image needs to be build.
image:
Image to start the container from.
- If the image does not exist, Compose attempts to pull it.
- In case, you have also specified "build", image will be build from dockerfile and tags it with the "image" tag.
- Default name of the image in absence of "image" would be <project_name>_<service_name>.
container_name: Specify a custom container name, rather than a generated default name.
entrypoint:           Override the default entrypoint.
command:            Override the default command.
ports:
  - Expose ports in form of (HOST:CONTAINER).
  - NOTE: HOST part is optional. In absence of HOST, a random host port will be chosen.
volumes:
  - 
Mount paths or named volumes in form of HOST:CONTAINER.

Example docker-compose file:
version: '2'
services:
  server:
    build:
       context: ./riemann_server
       dockerfile: Dockerfile
    entrypoint: ["/bin/nc"]
   command: ["-luv", "15555"]
   image: server_img
   container_name: server
   ports:
     - "15555:15555/udp"
   volumes:
     - /opt/pg/log:/opt/pg/log
 client:
   build:
     context: ./log_aggregator
     dockerfile: Dockerfile
   image: clinet_img
   container_name: client
   links:
     - server
   entrypoint: ["/bin/nc"]
   command: ["-vu", "server", "15555"]
   depends_on:
     - server
Dockerfiles for both client and server are holding only one line i.e "From ubuntu:14.04.3".
NOTE: For further details, see this.

docker-compose CLI:
You can see help using
docker-compose --help
build:
  • Services are build and tagged as "project_service".
  • In case of change in docker file or directory, run command again to rebuild.

config:   Validate and view the compose file.
create:  Create containers for service.
start:     Start existing containers for service.
up:         Builds, (re)creates, starts, and attaches to containers for a service.
ps:         List containers.
Stop:     Stops running containers without removing them
rm:        Removes stopped service containers.
kill:        Forces running containers to stop by sending a SIGKILL signal
down:    Stop containers and remove containers, networks.

No comments:

Post a Comment