Herkese merhaba!

Uzun yıllardır bol miktarda kişisel zaman ve enerji harcayarak bilgimizi hepinizle paylaşıyoruz. Ancak şu andan itibaren bu blogu çalışır durumda tutabilmek için yardımınıza ihtiyacımız var. Yapmanız gereken tek şey, sitedeki reklamlardan birine tıklamak olacaktır, aksi takdirde hosting vb. masraflar nedeniyle maalesef yayından kaldırılacaktır. Teşekkürler.

Bu örneğimizde DEV, STAG ve PROD ortamları için docker-compose-*.yml dosyaları yaratacağız. Dosyalar ortak ve kendilerine özgü ayar/değişkenler barındıracak.


Kurulum



Ortam değişkenleri



Aksini belirtmediğiniz sürece, Docker varsayılan olarak "docker-compose.yml" ve "docker-compose.override.yml" dosyalarını kullanacağından, docker-compose up komutu DEV ortamını yaratır. Diğer ortamların nasıl yaratılacağını daha sonradan göreceğiz.


Yapı


ubuntu@linux:~/football$ tree -a
.
├── docker
│   ├── docker-compose.override.yml # DEV (default)
│   ├── docker-compose.prod.yml # PROD
│   ├── docker-compose.stag.yml # STAG
│   ├── docker-compose.yml # Common
│   ├── .env
│   └── nginx
│   ├── Dockerfile # DEV (default)
│   ├── Dockerfile.prod # PROD
│   └── Dockerfile.stag # STAG
├── index.html
└── logs
└── nginx
├── access.log
└── error.log

4 directories, 11 files

Dosyalar


index.html


ubuntu@linux:~/football$ cat index.html
Hello!

docker/.env


ubuntu@linux:~/football$ cat docker/.env
COMPOSE_PROJECT_NAME=football

NGINX_IP=192.168.0.11
NETWORK_SUBNET=192.168.0.0/24

docker/docker-compose.yml


ubuntu@linux:~/football$ cat docker/docker-compose.yml
version: '3'

services:
nginx_img:
container_name: ${COMPOSE_PROJECT_NAME}_nginx_con
build:
context: ./nginx
networks:
public_net:
ipv4_address: ${NGINX_IP}

networks:
public_net:
driver: bridge
ipam:
driver: default
config:
- subnet: ${NETWORK_SUBNET}

docker/docker-compose.override.yml


ubuntu@linux:~/football$ cat docker/docker-compose.override.yml
version: '3'

services:
nginx_img:
build:
dockerfile: Dockerfile
ports:
- 1000:80
volumes:
- ..:/usr/share/nginx/html
- ../logs/nginx:/var/log/nginx
environment:
- ENV=dev

docker/docker-compose.stag.yml


ubuntu@linux:~/football$ cat docker/docker-compose.stag.yml
version: '3'

services:
nginx_img:
build:
dockerfile: Dockerfile.stag
ports:
- 80:80
volumes:
- ..:/usr/share/nginx/html
environment:
- ENV=stag

docker/docker-compose.prod.yml


ubuntu@linux:~/football$ cat docker/docker-compose.prod.yml
version: '3'

services:
nginx_img:
build:
dockerfile: Dockerfile.prod
ports:
- 80:80
- 443:443
volumes:
- ..:/usr/share/nginx/html
environment:
- ENV=prod

docker/nginx/Dockerfile


ubuntu@linux:~/football$ cat docker/nginx/Dockerfile
FROM nginx:1.13.8

RUN echo 'Dev Dockerfile'

docker/nginx/Dockerfile.stag


ubuntu@linux:~/football$ cat docker/nginx/Dockerfile.stag
FROM nginx:1.13.8

RUN echo 'Stag Dockerfile'

docker/nginx/Dockerfile.prod


ubuntu@linux:~/football$ cat docker/nginx/Dockerfile.prod
FROM nginx:1.13.8

RUN echo 'Prod Dockerfile'

Doğrulama


DEV


ubuntu@linux:~/football/docker$ docker-compose config
networks:
public_net:
driver: bridge
ipam:
config:
- subnet: 192.168.0.0/24
driver: default
services:
nginx_img:
build:
context: /home/ubuntu/football/docker/nginx
dockerfile: Dockerfile
container_name: football_nginx_con
environment:
ENV: dev
networks:
public_net:
ipv4_address: 192.168.0.11
ports:
- 1000:80/tcp
volumes:
- /home/ubuntu/football:/usr/share/nginx/html:rw
- /home/ubuntu/football/logs/nginx:/var/log/nginx:rw
version: '3.0'

STAG


ubuntu@linux:~/football/docker$ NGINX_IP=192.168.0.88 docker-compose -f docker-compose.yml -f docker-compose.stag.yml config
networks:
public_net:
driver: bridge
ipam:
config:
- subnet: 192.168.0.0/24
driver: default
services:
nginx_img:
build:
context: /home/ubuntu/football/docker/nginx
dockerfile: Dockerfile.stag
container_name: football_nginx_con
environment:
ENV: stag
networks:
public_net:
ipv4_address: 192.168.0.88
ports:
- 80:80/tcp
volumes:
- /home/ubuntu/football:/usr/share/nginx/html:rw
version: '3.0'

PROD


ubuntu@linux:~/football/docker$ NGINX_IP=192.168.2.99 docker-compose -f docker-compose.yml -f docker-compose.prod.yml config
networks:
public_net:
driver: bridge
ipam:
config:
- subnet: 192.168.0.0/24
driver: default
services:
nginx_img:
build:
context: /home/ubuntu/football/docker/nginx
dockerfile: Dockerfile.prod
container_name: football_nginx_con
environment:
ENV: prod
networks:
public_net:
ipv4_address: 192.168.2.99
ports:
- 80:80/tcp
- 443:443/tcp
volumes:
- /home/ubuntu/football:/usr/share/nginx/html:rw
version: '3.0'

Kurulum


DEV


ubuntu@linux:~/football/docker$ docker-compose up -d --build

Dev Dockerfile

ubuntu@linux:~$ docker exec -it football_nginx_con printenv ENV
dev

ubuntu@linux:~$ curl 192.168.0.11
Hello!

STAG


ubuntu@linux:~/football/docker$ NGINX_IP=192.168.0.88 docker-compose -f docker-compose.yml -f docker-compose.stag.yml up -d --build

Stag Dockerfile

ubuntu@linux:~$ curl 192.168.0.88
Hello!

ubuntu@linux:~$ docker exec -it football_nginx_con printenv ENV
stag

PROD


ubuntu@linux:~/football/docker$ NGINX_IP=192.168.0.99 docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d --build

Prod Dockerfile

ubuntu@linux:~$ curl 192.168.0.99
Hello!

ubuntu@linux:~$ docker exec -it football_nginx_con printenv ENV
prod