How to map a virtual host to a container in docker? - nginx

I am trying to setup a LEMP local development environment using Docker. Following is my docker-composer.yml:
version: '2'
services:
apache:
build:
context: ./docker/apache-php7
dockerfile: Dockerfile
image: foo/apache-php7
volumes:
- .:/var/www/html
ports:
- "80:80"
- "443:443"
networks:
- localnet
node:
build:
context: ./docker/node
dockerfile: Dockerfile
image: foo/node
volumes:
- .:/var/www/html
networks:
- localnet
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "root"
MYSQL_USER: "root"
MYSQL_PASSWORD: "root"
volumes:
- mysqldata:/var/lib/mysql
networks:
- localnet
redis:
image: redis:alpine
volumes:
- redisdata:/data
networks:
- localnet
networks:
localnet:
driver: "bridge"
volumes:
mysqldata:
driver: "local"
redisdata:
driver: "local"
I can access the site by going to http://localhost. Instead of accessing the project using http://localhost, I am trying to map it to http://foo.dev but for the life of me I just can't figure out how I can achieve that? One example I have seen is this but it's using a different image and it is also using nginx.

I am assuming that the foo/apache-php7 image is forked from nimmis/apache-php7, which is based on Ubuntu 16.04, so by default your Apache will answer the same way to HTTP requests, irrespective of host name.
So, the only thing you need to do is to set up A/AAAA records in the foo.dev DNS zone pointing at your server.
It looks like the zone needs some attention in any case:
% host foo.dev
foo.dev has address 127.0.53.53
foo.dev mail is handled by 10 your-dns-needs-immediate-attention.dev.
The address record points to a localhost address, which won't work until you're actually on the same host. You'll need a globally routed IP address there.

All I needed to do was simply add the ServerName directive in the apache conf file for the site in question. In my case, it was the 000-default.conf. I didn't even need to update the hosts file as I am marking Gerd's answer as the answer as that is what directed me to this:
ServerName foo.dev
I then updated my hosts file with the following:
127.0.0.1 foo.dev www.foo.dev

Related

Can ping but not wget between Docker containers

I am using docker-compose to set up a NextJS-app that fetches data from the Wordpress REST-API running in separate containers.
Problem is, I get ECONNREFUSED when I try to fetch or WGET the wordpress-container http://wordpress:8000 from the NextJS-container. I can ping wordpress:8000 without any problems.
If I use Postman or try to fetch the REST-API from another host (i.e. not the machine running docker-compose) using the public ip, it works perfectly.
I'm suspecting some docker configuration issue, but I'm quite lost as the pinging works but not the wget.
Anyone with an idea on what the culprit could be?
My docker-compose.yml:
version: '2'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: ***REMOVED***
MYSQL_DATABASE: ***REMOVED***
MYSQL_USER: ***REMOVED***
MYSQL_PASSWORD: ***REMOVED***
networks:
- back
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./wp-content:/var/www/html/wp-content
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: ***REMOVED****
networks:
- back
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8080:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: ***REMOVED***
networks:
- back
next-app:
depends_on:
- wordpress
build:
context: ./next-app
dockerfile: Dockerfile
volumes:
- './next-app:/usr/src/app'
- '/usr/src/app/node_modules'
ports:
- '80:3000'
networks:
- back
express-server:
build:
context: ./express-server
dockerfile: Dockerfile
ports:
- '3001:3001'
networks:
back:
driver: bridge
volumes:
db_data:
From inside the NextJS-container you need to use port 80. Port 8000 it's from your (host) machine. So use http://wordpress:80 from inside the docker containers.
ports:
- "8000:80"
In you docker-compose file just says: "Map my local (host machine) port 8000 to containers ports 80", but inside the docker network, it's still port 80
You can ping, because ping doesn't use ports. Ports that we are talking about are TCP/UDP ports, see https://en.wikipedia.org/wiki/Port_(computer_networking). But ping uses ICMP (Internet Control Message Protocol), which doesn't use ports at all, see https://en.wikipedia.org/wiki/Ping_(networking_utility)

WordPress not running on port 80

I have an Ubuntu 16 machine running docker and docker-compose
the docker-compose YML looks like this:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: abware
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
when I run: docker-compose up -d
it installs MySql+WP images and runs the containers and everything is just fine.
then, when I try to access my web site by navigating to: www.my-host.com:8000
it shows the WordPress website but when I navigate to the same URL with port 80 it's not working...
How do I make WordPress to work on the default port 80?
The section:
ports:
- "8000:80"
in the YML maps the host port 8000 to the container port 80. Changing that to 80:80 and rebuilding the container (as mentioned by #Kilan) should resolve the issue.
when you do this ports:
- "8000:80"
you mapped the port 8000 of your host on the port 80 of your container. It's normal if you can't navigate from your host with port 80.
Replace with 80:80, but before this, be sure that this port is free on your host.

Accessing wordpress in aws ec2 on port 80. Cannot connect to port 80

I am hosting a wordpress website on an aws ec2 (Amazon Linux AMI 2018.03.0).
I use docker-compose to spin up the website, database and phpmyadmin.
version: '3.3'
volumes:
data:
networks:
back:
services:
db:
image: mysql:5.7
volumes:
- data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: wordpress
MYSQL_USER: test
MYSQL_PASSWORD: test
networks:
- back
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- 80:80
- 443:443
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
- ./html:/var/www/html
container_name: site
networks:
- back
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 3001:80
environment:
PMA_HOST: db
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
networks:
- back
As you see I am mapping port 80 in my container to port 80 of my host. This should make the website available on my IPv4 Public IP. I think I have inbound access correctly configured in my security group.
I cannot access my website on port 80. When I change the yml file to map port 80 of my container to 8080 on my host it does work.
What do I have to do to make the website available on port 80?
Continued:
I ran docker compose as root with:
sudo which docker-composeup -d
The website was still unreachable.
I removed docker-compose from /usr/local/bin and installed it in /usr/bin.
Then I ran sudo docker-compose up -d
The website was still unreachable.
Still I think it indeed has something to do with running it as root. I can find the website when I run it on anything above port 1024. I cannot see it when running it on a port below 1024.
In which directory should I install docker-compose to run it as root?
I am sure the docker containers are coming up. I can see this as output from docker ps:
You are probably not running docker compose as root. Basically ports below 1024 are privileged ports and hence you need to be root to run processes that listen on these ports.
Run docker compose using using sudo and you should be able to access the site using port 80 and your hosts public IPV4 Address.

Reverse proxy nginx with Docker

I have a website with a Docker container.
So I use the nginx reverse proxy docker with let's encrypt
I follow this tutorial
But my website is not working with https.
There is my docker compose file:
services:
nginx:
image: pixelfordinner/nginx
container_name: pixelcloud-nginx_proxy-nginx
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- "./volumes/conf.d:/etc/nginx/conf.d:ro"
- "./volumes/vhost.d:/etc/nginx/vhost.d:ro"
- "./volumes/certs:/etc/nginx/certs:ro"
- "/usr/share/nginx/html"
nginx-proxy:
image: jwilder/docker-gen
container_name: nginx-proxy
depends_on:
- nginx
volumes_from:
- nginx
volumes:
- "/var/run/docker.sock:/tmp/docker.sock:ro"
- "./data/templates:/etc/docker-gen/templates:ro"
- "./volumes/conf.d:/etc/nginx/conf.d:rw"
entrypoint: /usr/local/bin/docker-gen -notify-sighup pixelcloud-nginx_proxy-nginx -watch -wait 5s:30s /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
letsencrypt-nginx-proxy:
restart: always
image: jrcs/letsencrypt-nginx-proxy-companion
container_name: ssl
depends_on:
- nginx
- nginx-proxy
volumes_from:
- nginx
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "./volumes/vhost.d:/etc/nginx/vhost.d:rw"
- "./volumes/certs:/etc/nginx/certs:rw"
environment:
- "NGINX_DOCKER_GEN_CONTAINER=pixelcloud-nginx_proxy-docker_gen"
wordpress:
image: wordpress
environment:
- VIRTUAL_HOST=foo.example.com
- LETSENCRYPT_HOST=foo.example.com
- LETSENCRYPT_EMAIL= mail#example.com
I download the nginx.tmpl file from github, and copy it into /data/templates/nginx.tmpl
I don't inderstand what it is not working
Thanks for help!
UPDATE: I managed to setup https instead of http on wordpress.
But I have a Gitlab instance with docker compose. On the login page, the https is working, but when I login and go on the project homepage, there is https but the connection is not secure.
I would like this: https://gitlab.exemple.com
It looks like you are missing the jwilder/docker-gen container that is required by jrcs/letsencrypt-nginx-proxy-companion.
See documentation: https://github.com/JrCs/docker-letsencrypt-nginx-proxy-companion#separate-containers-recommended-method
See example: https://github.com/fatk/docker-letsencrypt-nginx-proxy-companion-examples/blob/master/docker-compose/v2/simple-site/docker-compose.yml
It means you will have to add a nginx.tmpl file and mount it to your jwilder/docker-gen container.
You will also need to share the /etc/nginx/vhost.d volume from the nginx-proxy container.
Update:
The container_name of your service running the docker-gen image should be pixelcloud-nginx_proxy-docker_gen as it needs to match the NGINX_DOCKER_GEN_CONTAINER environment variable. So you should have:
nginx-proxy:
image: jwilder/docker-gen
container_name: pixelcloud-nginx_proxy-docker_gen

Nginx, Wordpress, and SQL

I currently am trying to learn how to use docker and was wondering if there is a way to make a Docker stack that includes Wordpress, SQL, and Nginx.
Right now I want to have 3 containers running, 1 for each and use nginx as a reverse proxy for my wordpress app.
However, every time I attempt to get this stack up and running through a composer file, only Wordpress and SQL get linked, but not the Nginx.
version: '2'
services:
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:fpm
links:
- db
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
nginx:
restart: always
image: nginx
ports:
- "80:80"
This is all I have in my docker-compose.yml
Your basic approach should work. I have a feeling there is a configuration issue somewhere, possibly with nginx that is preventing it from working as you intend.
You can try this similar docker-compose.yml file as a sample to see how it may differ from what you are doing:
docker-compose.yml
version: '2'
services:
php:
image: phpmyadmin/phpmyadmin
links:
- mysql:db
depends_on:
- mysql
mysql:
image: k0st/alpine-mariadb
volumes:
- ./data/mysql:/var/lib/mysql
environment:
- MYSQL_DATABASE=mydb
- MYSQL_USER=myuser
- MYSQL_PASSWORD=mypass
nginx:
image: nginx:stable-alpine
ports:
- "81:80"
volumes:
- ./nginx/log:/var/log/nginx
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/files:/var/www/nginx:ro
depends_on:
- php
nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile off;
server {
listen 80;
location / {
proxy_pass http://php;
proxy_set_header Host $host;
proxy_redirect off;
}
}
}
The nginx config is simplified but that should work for testing -- basically all it's doing is proxying the php app. Maps to port 81 to avoid conflicts on the host. (Note this is just a rough demo, would need to be fleshed out for any use more than that.)
Regarding linking, you can see that if you run: docker-compose exec mysql ping -c2 nginx to ping from the mysql container to the nginx container, you will succeed even though there are no links specified between these containers. Docker Compose will maintain those links in the default network for you.
If you like, you can fetch a working version from this repo here and run docker-compose up, and (assuming you don't have anything running on port 81) see results on http://localhost:81/ (or whatever your corresponding hostname/IP is).
For more info on Docker Compose networking see:
https://docs.docker.com/compose/networking/
By default Compose sets up a single network for your app. Each
container for a service joins the default network and is both
reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
Links allow you to define extra aliases by which a service is
reachable from another service. They are not required to enable
services to communicate - by default, any service can reach any other
service at that service’s name.
You could go with the jwilder-nginx docker image. It is using docker-gen to detect containers, and will register them in nginx.conf.
This should work, if you add "VIRTUAL_HOST" the domain will be added to nginx.conf. Please note: You don't have to expose ports on WordPress with this Setup. jwilder-nginx will use default port to forward traffic.
version: '2'
services:
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:fpm
links:
- db
- nginx
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
VIRTUAL_HOST: myblog.mydomain.de
nginx:
restart: always
image: jwilder/nginx-proxy
ports:
- "80:80"

Resources