James Lin
James Lin

Reputation: 26568

How to host multiple environments for a project using docker in the same machine?

I have a typical web stack that consists of nginx + django + database components.

I have set them up in different docker containers with docker-compose and it's running fine.

services:
  billing_app_dev:
    image: jameslin/billing_app:latest
    expose:
      - 8000
  billing_postgres:
    image: postgres:10.5
    restart: always
    volumes:
      - pg_data:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
  billing_nginx:
    image: jameslin/billing_nginx:${TAG}
    volumes:
      - app_files:/static
    links:
      - 'billing_app'
    ports:
      - 80:80

Now I am wondering how I can set up DEV and QA environments on a single machine. I can change the django and database containers to listen to different ports, but looks like I cannot run nginx containers individually since port 80 can only be listened by one container.

I will have to share the nginx container for those 2 environments which doesn't seem very clean.

Are there any better ideas if running 2 VMs is not possible?

Upvotes: 0

Views: 390

Answers (2)

atline
atline

Reputation: 31654

I think what you needed is virtual ip or maybe called ip aliasing. Even you just have one network card, you can still set 2 ips on it.

Then, you can set 2 different nginx container on host, and use different ip but same 80 port.

Something like follows:

cd /etc/sysconfig/network-script/
cp ifcfg-eth0 ifcfg-eth0:1
vi ifcfg-eth0:1

# Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)
DEVICE=eth0:1 ----> sub network card
HWADDR=00:0C:29:45:62:3B
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.109.108 ----> configure a new different ip
NETMASK=255.255.255.0

Detail refers to Create Multiple IP Addresses to One Single Network Interface

For nginx, from nginx guide, you had to change your nginx docker to modify listen 80 to listen your_ip:80, then it will not listen on all ip address.

Upvotes: 0

Nel Xhato
Nel Xhato

Reputation: 11

I have 3 apache containers and 1 nginx running in the same server, so pretty sure is not a issue.

For each stack of webserver + database i have a different docker-compose file, in this way docker will create a different network for each stack, avoiding possible problems with simultaneous port, and you only will have to bind your nginx in different ports of your server, because, you only can bind one service to one port. still, each container is a separated "machine", so even over the same network they can use the same port.

if you really need run all your services in the port 80 and 443 of your server, may be you will need to put a nginx running in those ports like a reverse proxy calling in the internal docker network those services, is a option but i never try it before over docker internal network.

Upvotes: 1

Related Questions