Skip to content

-------- docker compose -----------

  • docker compose s a hand tool to run multicontainer application wi Docker
  • multicontainer app on a single node! --> how to spawn images
  • orchestrate a set of container minimizing the configuration of each container on each host and the effort to launch each separately (i don't need to define properties for runtime for each single container)
  • hi level configuration defined in a file structure -definition of spawing such multicontainer applications are contained in a single file or couple of fole (one to specify what those containers are and one to binding the requirement) -it is a great tool for dvlp and stagineg enviroment Use docker compose (3 steps) 1) create docker file --> image to use, set env variables, directories that should contain the codebase 2) define services required in the docker-compose.yml file --> volume, ports, rights, directorie, bindings 3) run docker-compose up to spin the applciation

yaml file are not markup language - it is a human readable definition and it is becoming significant important to define script languages - docker compose help to manage the entire lifecycle of an application - manage the multichannel enviroment that we desire to spawn - start, stop and rebuild services - view status of running services - stream the log output of running services - run a one-off cmd on a services

  • docker compose spawn application only within the context of a single host --> is not possible perform clustering with compose --> context should be a single host
  • docker compose is perfect for pre-production enviroment where testing is performed on a single node, but in production we need to manage clustering multiple host and multiple container

install docker compose on ubuntu

-not support windows os - docker installation and engine on required - grant permission for write/read ops --> best be root (cmd: su root) --> otherwise I need to chmod +x on the docker compose folder 1) grab the docker compose from github 2) using the pip utility python based utility --> pip install -U docker-compose (at first time is necessary update the system - do it automatically) 3)verify that it works typing docker-compose --version --> it will return the same version of docker 4) pip unistall docker-compose

Incapsulate docker images in a docker file using docker compose

  • first information should be the base image used to spawn the containers
  • an image consist of a rich prebuild application/software stack ready for the launch (i.e. ubuntu or python)
  • docker compose add a new layer on the top of the base image used for each modification that we define in the services defined in the union file structure that we can modify after created
  • an image need to be an image defined on the docker repository that docker engine can pull and update or modify
  • FROM ubuntu --> we want contruct a new layer on top on ubuntu stack (could be one or multiple images)
  • ADD ./code --> add the current directory into the path /code of the image
  • WORKDIR /code --> set the working directory
  • ENV PYTHONBUFFERED 1 --> enviroment variable definition
  • RUN pip install -r requirement.txt --> perform an installation
  • CMD python app.py --> command prompt to execute the application

Definition of services in docker compose

  • docker services, dependecies are defined in a config file named docker-compose.yml
  • separate the dockerimage from the binding, port and volumes are very popular today and it is a good strategy
  • docker compose use yml to define the basic docker services required for an application to load and execute containers
  • each service defined in the compose file must specify exactly one image or build tags --> docker compose spawn multicontainer enviroment so each image must have the corresponding service entry
  • only few keys are mandatory
  • option defined in the dockerfile are automatically retrieved by the compose file
  • docker ports--> [container_port]:[local host port]
  • volumes --> data folder that can be reused -links--> links: -redis --> if we want link a container to the each other
  • build from the docker file in the current directory in which the upser is operatig
  • mount the current directory on the host to "/code" inside the container allows to modify the code without having to rebuild the image
  • docker file --> which images, which folder to create
  • yml file --> bind services with the component defined in the Dockerfile

//DOCKERFILE EXAMPLE FOR DJANGO FROM python:2.7 ENV PYTHONBUFFERED 1 RUM mkdir /code WORKDIR /code ADD required.txt /code/ RUN pip install -r requirement.txt ADD . /code/

//DOCKERCOMPOSE YML FILE FOR DJANGO

db:
   image: postgres
web:
  build: .
  command: python manage.py runserver 0.0.0:8000
  volumes:
  - .:/code
  ports:
  -"8000.8000"
  links:
  - db

//REQUIREMENTS.TXT

Django
psycopg2 //driver that python use with postgres

//put all together

docker-compose run web django-admin.py startproject composeexample .