5: Docker Compose

  Docker

< 4 Docker Images | 6 Docker Registry >

47% Complete

26: Docker Compose

https://www.udemy.com/learn-docker/learn/lecture/7894026#content

Runs a YAML file with the instructions required to build a container or stack.

--link ContainerName:ContainerAlias

Creates a local alias in the container’s /etc/hosts file with the IP address of the container you are linking

172.17.0.3 ContainerAlias

Docker Compose Versions

Version 1

  • No networking controls
  • No startup sequence or dependancies
redis:
  image: redis
db:
  image: postgres:9.4
vote:
  image: voting-app
  ports:
    -  5000:80
  links:
    - redis

Version 2

  • Requires the version to be listed in the file.
  • No need for links. Version 2 creates a bridged network that all containers are connected to.
  • Includes ‘depends_on’ property to show what containers are required before starting.
version: 2
services:
 redis:
  image: redis
 db:
  image: postgres:9.4
 vote:
  image: voting-app
  ports:
   - 5000:80
  depends_on:
   - redis

Version 3 (Current version)

  • Requires the version to be listed in the file.
  • Supports Docker Swarm
version: 3
services:
 redis:
  image: redis
  networks:
   - back-end db:
 db:
  image: postgres:9.4
  networks:
   - back-end
 vote:
  image: voting-app
  networks:
   - front-end
   - backend
  ports:
   - 5000:80
 result:
  image: result
  networks:
   - front-end
   - back-end
networks:
 front-end:
 back-end:

28: Voting App

https://www.udemy.com/learn-docker/learn/lecture/15829900#overview

http://docker01.tas.local:5000/

  • 5 containers, linking between containers, running in a specific order.
  • Most containers built manually using pre-built Dockerfile(s)
  • Vote on port 5000, result displayed on 5001
  • Due to link constraints, containers had to be ran in order and some required special names for the links to work.

Download the code from Git

git clone https://github.com/dockersamples/example-voting-app.git
cd example-voting-ap

Create and run the Submit Vote app

cd vote
# Dockerfile already exists, just build it.
docker build . -t voting-app
# Run the container
docker run -p 5000:80 voting-app

Note:

  • Container was not ran detached so you can see the logs

After the container is running access it via a web browser

  • http://docker01.tas.local:5000
  • Click one of the links and watch the log. It will try to connect to redis, but will crash because the service is not yet running.

Run Redis

Just need to pull this down, no mods necessary
docker run -d --name=redis redis

Note:

  • The file still crashes. Since the containers have not been linked, they do not resolve
docker run -p 5000:80 --link redis:redis voting-app
  • Now, clicking links works without errors.

Run PostGres

This might seem out of order, but this database is required for the worker to run.

docker run -d --name=db postgres:9.4
# run crashes - rerun attached to see errors
docker rm db
docker run --name=db= postgres:9.4
Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD for the superuser. Use
       "-e POSTGRES_PASSWORD=password" to set it in "docker run".

       You may also use POSTGRES_HOST_AUTH_METHOD=trust to allow all connections
       without a password. This is *not* recommended. See PostgreSQL
       documentation about "trust":
       https://www.postgresql.org/docs/current/auth-trust.html
# Remove the Image
docker rm db
docker run --name=db -e POSTGRES_HOST_AUTH_METHOD=trust postgres:9.4

Build and Deploy the Worker

12:00

cd ../worker
docker build . -t worker-app
# guessing this will need to link to both dbs
docker run -d --link redis:redis --link db:db --name=worker worker-app

Build and deploy the Resulting App

cd ../result
docker build . -t result-app
docker run -d --link db:db -p 5001:80 --name result result-app

Final results:

29: Docker Compose

https://www.udemy.com/learn-docker/learn/lecture/15829906#overview

Install Docker Compose

Docker compose not included by default, so needs to be installed.

  • https://docs.docker.com/compose/
  • sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  • sudo chmod +x /usr/local/bin/docker-compose

Stop and Remove all running containers

docker stop $(docker ps -q)
docker rm $(docker ps -aq)

Create the Docker Compose YAML file

nano docker-compose.yml

redis:
  image: redis
db:
  image: postgres:9.4
vote:
  image: voting-app
  ports:
    - 5000:80
  links:
    - redis
worker:
  image: worker-app
  links:
    - db
    - redis
result:
  image: result-app
  ports:
    - 5001:80
  links:
    - db

Run the file

docker-compose up

 

30: 31: Pratice

LEAVE A COMMENT