Create app and db instances on different nodes

Hi. I am new to Docker and came across Docker compose today.
Was just wondering how can I create app instance on server1 and database instance on server2 using docker compose ?

All the examples, i have seen till now have created the app and database on the same node. They just use different ports. I would like to know if there is any way to separate these instances.

Can anyone help me in understanding how docker is implemented in real life scenario or atleast share few resources which could help me understand the real life examples of docker ?

Hi.

The way I would do it, is:
1- Create a docker swarm cluster
2- Use the following compose file (Example):

version: '3'
services:
  db:
    image: mariadb:latest
    volumes:
      - sql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: dbroot
      MYSQL_DATABASE: dbdb
      MYSQL_USER: dbuser
      MYSQL_PASSWORD: dbpass
    deploy:
      placement:
        constraints:
          - node.hostname == NODE01
      replicas: 1

  wordpress:
    image: wordpress:latest
    ports:
      - "80:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: dbuser
      WORDPRESS_DB_PASSWORD: dbpass
    deploy:
      placement:
        constraints:
          - node.hostname == NODE02
      replicas: 1

volumes:
  sql-data:

3- Execute: docker stack deploy -c the-compose-file.yml web

But then we are moving to a bit more advanced docker, but i would read up on swarm mode :slight_smile:

1 Like

Pure Docker can’t do this on its own. @terpz’s suggestion of using Swarm is probably the easiest way.

Back when dinosaurs roamed the earth, Google had just announced Kubernetes 0.1, and there wasn’t docker exec yet, I did this sort of thing more manually:

ssh to the machine that will run the database server. docker run -p5432:5432 postgres (or whatever the actual database is).

ssh to the machine that will run the application. From its point of view, the first machine that you ssh’d to is running a database server; it doesn’t know or care that it’s in Docker. So now you can docker run -p 80:8080 -e PGHOST=dbhost.example.com myimage.

There are various ways to automate and script this, like using Ansible to do the “ssh to the machine and launch a Docker container” step.

1 Like

Thanks for the reply terpz and dmaze.