I have two containers nginx and php how to configure docker-compose so when I make wget http://example.com from inside php container this host should point to nginx container
Map your nginx port to host port.
If you host has name example.com and nginx runs on 8080 port, set up your docker-compose like
nginx:
image: nginx
hostname: nginx
ports:
- "8080:80"
In this case request to http://example.com will be really executed as http://nginx:8080.
I've specified static ips for my containers in docker-compose.yml and added extra_host:
services:
nginx:
image: nginx
ports:
- "8200:80"
- "8201:443"
volumes:
- .:/var/www/html
networks:
test:
ipv4_address: 10.5.0.5
php:
build: ./docker/php
volumes:
- .:/var/www/html
environment:
APP_ENV: "dev"
networks:
test:
ipv4_address: 10.5.0.6
extra_hosts:
- "example.com:10.5.0.5"
networks:
test:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16
Related
i am trying to bind container with nginx to ip 172.16.238.10 , but for some reason docker ignores settings in docker-compose
#my docker-compose file
version: "3.9"
services:
nginx:
build: nginx/
ports:
- 80:80/tcp
volumes:
./dokcer/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
./dokcer/nginx/conf/hosts:/etc/hosts
./docker/project:/var/www/project
networks:
app_net:
ipv4_address: 172.16.238.10
php-fpm:
build: php-fpm/
ports:
- 9000:9000/tcp
volumes:
./dokcer/php-fpm/conf/www.conf:/usr/local/etc/php-fpm.d/www.conf
./dokcer/php-fpm/conf/hosts:/etc/hosts
./dokcer/project:/var/www/project
networks:
app_net:
ipv4_address: 172.16.238.11
networks:
app_net:
ipam:
driver: default
config:
- subnet: "172.16.238.0/24"
after build and launch, I look at the docker_app_net network and see that the nginx container has ip 172.16.238.2 although I expected it to be 172.16.238.10.
what could be the problem? I will be grateful for every answer because I am confused :c
I use docker compose to build up the services, i want to set the mysql to be static, i tried to use networks directive but it didn't work and the errors say the ip already occupied, every this i restart the windows, the mysql ip was changed, sometimes 172.18.0.3,or 172.18.0.4, anyone know how to assign a static ip to the container? here is the yml
version: '3.7'
services:
nginx:
container_name: hki_nginx
image: nginx:latest
ports:
- 80:80
- 4433:443
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf:/etc/nginx/conf.d
- ./src:/var/www
links:
- php
- php72
php:
container_name: hki_php
image: php:5.6-fpm-ext1
volumes:
- ./src:/var/www
- ./php/php.ini:/usr/local/etc/php/php.ini
- ./php/php-fpm.conf:/usr/local/etc/php-fpm.d/www.conf
#- ./php/phpfpm/:/usr/local/etc/php-fpm.d/
php72:
container_name: web_php
image: php:7.2-fpm-ext2
volumes:
- ./src:/var/www
- ./php72/php.ini:/usr/local/etc/php/php.ini
- ./php72/php-fpm.conf:/usr/local/etc/php-fpm.d/www.conf
#- ./php/phpfpm/:/usr/local/etc/php-fpm.d/
mysql:
container_name: hki_mysql
image: mysql:5.7
volumes:
- ./mysql/data:/var/lib/mysql
- ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf
- ./mysql/init:/docker-entrypoint-initdb.d/
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=*Abcd1234
- MYSQL_USER=abc
- MYSQL_PASS=*Abcd1234
#networks:
#default:
#ipv4_address: 172.18.0.3
This is because you commented the part that assigns a static IP to your mysql container
mysql:
...
#networks:
#default:
#ipv4_address: 172.18.0.3
If you take away the #, it will have a static IP.
And you might have forgotten the top-level network section in your docker-compose.yml as the official doc setting static IP states
networks:
app_net:
ipam:
driver: default
config:
- subnet: 172.18.0.0/24
I have a traefik in docker-compose:
version: '3'
networks:
proxy:
driver: bridge
services:
traefik:
container_name: traefik
image: traefik:v1.7.9
command: --api --docker
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./traefik.toml:/etc/traefik/traefik.toml
- ./acme.json:/acme.json
networks:
- proxy
Also have nginx under mydomain.com, and I want to allow only my ip to connect to it:
nginx:
build: ./nginx
networks:
- backend
- traefik_proxy
restart: always
labels:
traefik.enable: "true"
traefik.port: "80"
traefik.frontend.headers.allowedHosts: "1.2.3.4" # MyIp
traefik.frontend.rule: "Host:mysite.com,www.mysite.com"
When I access mysite.com I got Bad Host error, and the IP in headers is my server's ip instead of my real ip.
P.S Docker in swarm mode, but nginx and traefik build using local docker-compose
The solution is to add following directives to nginx docker-compose:
traefik.frontend.whiteList.sourceRange: "1.2.3.4" # my Ip
traefik.frontend.passHostHeader: true
traefik.frontend.whiteList.useXForwardedFor: "true"
I'm trying to use traefik in my docker-compose file. My php app is listening on port 8000
version: '3'
services:
traefik:
image: traefik:1.7.4
container_name: traefik-${PROJECT_NAME}
ports:
- ${TRAEFIK_PORT}:80
- ${TRAEFIK_PORT_HTTPS}:443
- ${TRAEFIK_DASHBOARD_PORT}:8080
volumes:
- ./traefik/traefik.toml:/etc/traefik/traefik.toml
- /var/run/docker.sock:/var/run/docker.sock
networks:
- webgateway
php-fpm:
build:
context: .
dockerfile: Dockerfile-php
container_name: php-fpm-${PROJECT_NAME}
ports:
- 8000
working_dir: /var/www/html/
volumes:
- ../app:/var/www/html
tty: true
env_file:
- ./.env
entrypoint: /entrypoint.sh
networks:
- traefik
networks:
webgateway:
driver: bridge
traefik:
external:
name: traefik_webgateway
volumes:
data-volume: {}
Trefik watch every container
[docker]
domain = "local"
watch = true
All container appear in Traefik dashboard but frontend Host do not match with IP address. I can't access the app.
But when I go directly through the container IP address, it works.
Did I missed something in the configuration?
Found it. I have added host name in my /etc/hosts file.
Works fine with that
Looking to automatically add the nginx container ip address inside my phpfpm container /etc/hosts file.
Inside my yml file, I have a service called phpfpm, and I know you can use extra_hosts attribute to assign values into the /etc/hosts file, however I don't know how to dynamically call place the nginx container IP.
nginx:
build: ./nginx
ports:
- "80:80"
- "443:443"
volumes:
- ../public/:/var/www/html/public/
container_name: nginx
networks:
- backend
phpfpm:
build: ./php-fpm
volumes:
- ../public/:/var/www/html/public/
container_name: phpfpm
extra_hosts:
- "test.local:nginx" <insert nginx ip to test.local>
networks:
- backend
Any thoughts on how to do this?
Containers within a compose file will run on same network and you can just their names. phpfpm and nginx in your case. Also if you need more names for the same service you need to use aliases
nginx:
build: ./nginx
ports:
- "80:80"
- "443:443"
volumes:
- ../public/:/var/www/html/public/
container_name: nginx
networks:
backend:
aliases:
- test.local
phpfpm:
build: ./php-fpm
volumes:
- ../public/:/var/www/html/public/
container_name: phpfpm
networks:
- backend
Why do you need Nginx Ip address? You can call nginx from phpfpm container by hostname nginx