Sorry I am brand new to docker and web development in general but I made basic docker compose server that hosts my local PHP file and displays some text. It works fine with the local host but I bought some domains and was wondering how I change from connecting to localhost to a domain so anyone can connect to it. My IP is already set up for outside connect and works for my ssh server so I do not need to do that. I just can't seem to find any results when I try to look it up. So I just need to know what to change in my docker compose files or settings to make go a domain instead.
Here is my docker-compose file:
services:
product-service:
build: ./product
volumes:
- ./product:/usr/src/app
ports:
- 5001:80
website:
image: php:apache
volumes:
- ./website:/var/www/html
ports:
- 5000:80
depends_on:
- product-service
Without more knowledge about your code logic, I'm not sure if I can fully make it run just by this answer. But I guess I can give an abstract checklist.
Due to your docker-compose config, the port 5001, 5000 already being opened to localhost. If these ports are already configured to open to the external network, you can already type yourdomain.com:5000 to access it.
If you just want to access by typing yourdomain.com (without port). I assume your website service will serve it:
Open port 80 and connect it with port 80 of the website service:
website:
image: php:apache
volumes:
- ./website:/var/www/html
ports:
- 80:80
depends_on:
- product-service
Make sure any AJAX call to product-service API will be called to yourdomain.com:5001:
From inside the website service, it can call to product-serivce API by using localhost:5001. But from user browser, any AJAX call will be counted as external call, so any AJAX call to this service must be configure as yourdomain.com:5001
Related
I'm currently trying to dockerize my app for local development. For a bit of context, it is using Magento.
I have a configuration file where I'm used to set 127.0.0.1 as MySQL hostname as the web app is running on the same host as MariaDB.
Initially, I've tried to link my container within my docker-compose file using 'links' (below an extract of my docker-compose setting at this point)
mariadb:
image: mariadb:5.5
php-fpm:
build: docker/php-fpm
links:
- "mariadb:mysql"
At this point, MariaDB was reachable by setting mysql as hostname in my configuration file instead of 127.0.0.1. However, I wanted to keep 127.0.0.1.
After a bit of digging, I've found this blog post where it is explained how to set up containers so that it can be reached through localhost or 127.0.0.1
This is working as I'm expecting but it has a flaw.
Without Docker, I'm able to run PHP scripts which leverage magento core modules by loading it. But with Docker and the blog post configuration, I'm not able to do it anymore as Magento is weirdly expecting for a DB hostname called "mysql".
Is there anyway through docker-compose to have a container be reachable with localhost and an hostname?
Without Docker, if I install MariaDB on my host machine, I am able to connect to its instance through 127.0.0.1:3306 or mysql://. I want to get a similar behaviour.
As said by #Sai Kumar you can connect the docker containers to the host network and then can use localhost to access services. But the problem is the port will be reserved by that container and will not be available till it is deleted.
But from your question, the following sentence caught my attention
Without Docker, I'm able to run PHP scripts which leverage magento
core modules by loading it. But with Docker and the blog post
configuration, I'm not able to do it anymore as Magento is weirdly
expecting for a DB hostname called "mysql"
So If I understand properly Magento is expecting to connect to MySQL with mysql as hostname instead of localhost. If so this can be easily solved.
How?
So in docker, there is a concept called service discovery. I've explained the same in many of my answers. Basically what it does is it resolves IP of containers by container hostname/aliases.So, instead of connecting between containers using IP address you can connect between them by using their hostname such that even if container restarts(which results in change of IP), Docker will take care of resolving it to respective container.
This works only with user-defined networks. So what you can do is create a bridge network and connect both Magento and Mysql to it. and give the container_name as mysql for mysql or you can also use alias as mentioned here. So putting it all together a sample docker compose will be
version: '3'
services:
mariadb:
image: mariadb:5.5
container_name: mysql #This can also be used but I always prefer using aliases
networks:
test:
aliases:
- mysql #Any container connected to test network can access this simply by using mysql as hostname
php-fpm:
build: docker/php-fpm
networks:
test:
aliases:
- php-fpm
networks:
test:
external: true
More references
1. Networking in Compose.
2. Docker Networking.
Yes you can connect through your db with localhost or 127.0.0.1 But this is only possible when you create docker-compose network in host mode.
But when you set your docker network in host mode then containerising concept will fail. So you have to choose host or bridge network mode
You can find networking in docker-compose
network_mode: "host"
Our website-framework(s) are designed to work on both xampp and docker environments. We are recognizing our database hosts by host name/IP-address (dev, test, staging, live env). People who are working with xampp are using https://localhost, so they get the environment variable called Development. People who are working with docker are using https://docker as their host. They get the env-variable called Development/Docker. We need this differentiation because inside the php applications our xampp users are connecting to their mysql service with host localhost. Docker users have to connect via host called mysql (this is the container name of the mysql-service).
Because of the last occurred problems (not relevant to be mentioned here) we want a unique solution for both user-groups concerning the database connection: Docker users should be able to connect to their mysql service with host localhost.
docker-compose.yaml (shortened for better overview):
version: '2'
services:
#######################################
# PHP application Docker container
#######################################
app:
build:
context: .
dockerfile: Dockerfile.development
links:
- mail
- mysql
- redis
ports:
- "80:80"
- "443:443"
- "10022:22"
- "3307:3306"
volumes:
- ./app/:/app/
- ./:/docker/
cap_add:
- SYS_PTRACE
env_file:
- etc/environment.yml
- etc/environment.development.yml
environment:
- POSTFIX_RELAYHOST=[mail]:1025
#######################################
# MySQL server
#######################################
mysql:
build:
context: docker/mysql/
dockerfile: MariaDB-10.Dockerfile
ports:
- "3306"
volumes:
- mysql:/var/lib/mysql
env_file:
- etc/environment.yml
- etc/environment.development.yml
#######################################
# phpMyAdmin
#######################################
# /// #
#######################################
# Mail
#######################################
# /// #
#######################################
# Redis
#######################################
# /// #
# Volumes
volumes:
mysql:
phpmyadmin:
redis:
I tried a lot and played with the docker-compose but didn't find a solution for weeks. Tried with links, networks and so on. I think my docker skills are exhausted by now...
I also added to the mysql.conf:
bind-address = 0.0.0.0
Any ideas?
It is because of docker's networking structure.
docker creates 3 base interfaces. docker0 , host and none.
each container uses docker0 by default. then each container will have a virtual network interface for communicating between containers. so you can use your database with mysql address.
if you want to connect to database with address localhost you should config docker to use host network mode (you can do it by adding one line to defenition of your app service in docker compose file). it will be able to connect to every app which is running on your system. By the way you would loose communication with other container by their name. maybe you loose your connection with your redis (which is connected with redis address)
In this network mode, every dependency should be deployed or be exposed to your localhost.
I am developing a Laravel application. I am trying to use Laravel Websocket in my application, https://docs.beyondco.de/laravel-websockets. I am using Docker/ docker-compose.yml. Since the Laravel Websocket run locally on port 6001, I am having problem with integrating it with docker-compose. Searching solution I found this link, https://github.com/laradock/laradock/issues/2002. I tried it but not working. Here is what I did.
I created a folder called workspace under the project root directory. Inside that folder, I created a file called, Dockerfile.
This is the content of Dockerfile
EXPOSE 6001
In the docker-compose.yml file, I added this content.
workspace:
port:
- 6001:6001
My docker-compose.yml file looks something like this
version: "3"
services:
workspace:
port:
- 6001:6001
apache:
container_name: web_one_apache
image: webdevops/apache:ubuntu-16.04
environment:
WEB_DOCUMENT_ROOT: /var/www/public
WEB_ALIAS_DOMAIN: web-one.localhost
WEB_PHP_SOCKET: php-fpm:9000
volumes: # Only shared dirs to apache (to be served)
- ./public:/var/www/public:cached
- ./storage:/var/www/storage:cached
networks:
- web-one-network
ports:
- "80:80"
- "443:443"
php-fpm:
container_name: web-one-php
image: php-fpm-laravel:7.2-minimal
volumes:
- ./:/var/www/
- ./ci:/var/www/ci:cached
- ./vendor:/var/www/vendor:delegated
- ./storage:/var/www/storage:delegated
- ./node_modules:/var/www/node_modules:cached
- ~/.ssh:/root/.ssh:cached
- ~/.composer/cache:/root/.composer/cache:delegated
networks:
- web-one-network
When I run "docker-compose up --build -d", it is giving me the following error.
ERROR: The Compose file './docker-compose.yml' is invalid because:
Unsupported config option for services.workspace: 'port' (did you mean 'ports'?)
What is wrong and how can I fix it? How can I use Laravel Web Socket with docker-compose?
I tried changing from 'port' to 'ports', then I got the following error message instead.
ERROR: The Compose file is invalid because:
Service workspace has neither an image nor a build context specified. At least one must be provided.
Your Dockerfile is wrong. A Dockerfile must start with a FROM <image> directive as explained in the documentation. In your case it might be sufficient to run an existing php:<version>-cli image though, avoiding the whole Dockerfile stuff:
workspace:
image: php:7.3-cli
command: ["php", "artisan", "websockets:serve"]
Of course you will also need to add a volume with the application code and a suitable network. If you add a reverse proxy like Nginx in front of your services, you don't need to export the ports on your workspace either. Services may access other services as long as they are in the same network.
I am new to Docker.
I have read that it is better to keep an app per container.
I need to run web app (LAMP stack). So I have found the container for running PHP + Apache.
Now I need to set up a mysql container. But I am not sure what is the correct way to do that.
I read that it is possible to connect multiple containers together.
The idea is to make it trasnparent to the container running PHP + Apache when it tries to connect to mysql database locally.
But redirect all local connections to another container.
Another idea I have is to provide environment variable with host where should all connections go. In this case I need to have publicly accessible mysql server, but I need to keep it private and accessible only locally.
Could you please suggest a better option to choose in my case ?
Thank you
Use docker-compose:
For example, start from this docker-compose.yml. Put it in the same dir as your php Dockerfile:
version: "3"
services:
web:
build: .
ports:
- 8000:80
depends_on:
- db
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=something
volumes:
- ./mysql-data:/var/lib/mysql
Then:
docker-compose up
So thanks to Docker network, you can point from your PHP as this: db:3306.
I have a docker compose file which combines nginx and php like this:
nginx:
image: nginx
ports:
- "80:80"
- "2443:2443"
links:
- phpfpm
volumes:
- ./nginx/anonymous.conf:/etc/nginx/conf.d/anonymous.conf
- ./logs/nginx-error.log:/var/log/nginx/error.log
- ./logs/nginx-access.log:/var/log/nginx/access.log
- ./public:/usr/share/nginx/html
phpfpm:
image: php:fpm
expose:
- "2443"
volumes:
- ./public:/usr/share/nginx/html
I can see my website i.e index.php page on browser with the virtual host i have already made lets say anonymous.com
Now inside my phpfpm container i started a socket server based on Ratchet which is listening to port 2443
// bin/server.php
$webSocketServer = new WsServer(new Chat());
$server = IoServer::factory(
new HttpServer($webSocketServer), 2443);
$server->run();
This is how i run my server inside phpfpm container
php /usr/share/nginx/html/bin/server.php
My understanding is, since i have already exposed 2443 and my ngnix and phpfpm containers are linked. I would be able to connect to my socket server running on phpfpm container by going to telnet anonymous.com 2443
But its not getting connected. Here is the output
$ telnet anonymous.com 2443
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
Please note that previously when i had both nginx and php-fpm were on single container, every thing was working fine. So i am sure that there is nothing wrong with PHP. It just i cannot figure out how to access the socket server from outside world.
Thanks
Update
If i use my phpfpm container ip and use it with port 2443 through browser, its working fine. but the thing is i cannot rely on container ip as its all dynamic.
I had a similar issue (same output) using a socket server based on Ratchet. Mine was only a PHP container (= no nginx/apache/...).
It was because of the 3rd parameter given to the factory :
$server = IoServer::factory(
new HttpServer($webSocketServer), 2443, '127.0.0.1'
);
Removing 127.0.0.1 (and then use the default 0.0.0.0 value) solved the problem.
You should use the name of the service as the hostname. So to connect from nginx to phpfpm use phpfpm:2443