Change default 0.0.0.0 from docker to specific - docker

I've managed to run my docker container installing php / composer / phpmyadmin.
But as a result for example phpmyadmin is accessible through the url : http://0.0.0.0:8080/
How can I change the default 0.0.0.0 by something with more sense like http://myapp:8080
I guess I have to add something into my hosts; but I also guess I have to update my docker so he knew he has to read it.
Here is my docker-compose.yml
version: '3'
services:
symfony:
build:
context: .
dockerfile: docker/Dockerfile
image: project-manager
ports:
- 80:80
db:
image: mysql
ports:
- 3306:3306
volumes:
- "./.data/db:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8080:80"
links:
- db

So actually 127.0.0.1 already respond as 0.0.0.0 listen to everything so as we just have to setup in the host what we need as usual. 127.0.0.1 myapp

Related

How to automatically get an available port?

I'm working with docker containers for some projects and to save time i clone the docker composer file for my other projects.
The problem I have is that the ports for my mysql_database and apache_service are fixed values.
Example:
version: "3.2"
services:
apache_service:
build:
context: './docker/apache/'
links:
- mysql_service:mysql_service
depends_on:
- mysql_service
ports:
- "8080:80" # "random_port:80"
volumes:
- ./:/var/www/
mysql_service:
build:
context : ./
dockerfile : ./docker/mysql/Dockerfile
command: [
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci',
'--default-authentication-plugin=mysql_native_password',
]
restart:
always
volumes:
- ./docker/initdb:/docker-entrypoint-initdb.d
- ./docker/mysql/logs:/var/log/mysql
ports:
- "4306:3306" # "random_port:3306"
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
container_name:
mysql_service
When i copy the docker-composer file and write docker-composer up i alwayas have to change the ports previously...
How i could automatily get an avaliable port for this services?
Use 0 as the host port: this way you will get the first available (random) port from the operating system; to later get the actual used port you can use the docker port <container> command.

What to use as base url in magento2 when using docker?

I have simple docker orchestration to serve Magento files
here is my docker-compose.yml:
version: "3"
services:
php:
build: docker/php
volumes:
- .:/var/www/html
db:
image: mariadb:10.4
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: magento
volumes:
- ./docker/mysql/databases:/var/lib/mysql
nginx:
image: nginx:alpine
ports:
- 8009:80
links:
- php:phpfpm
volumes:
- ./docker/nginx/conf.d:/etc/nginx/conf.d
- ./docker/nginx/magento.conf:/etc/nginx/magento.conf
- .:/var/www/html
elasticsearch:
image: bitnami/elasticsearch:7
ports:
- 9200:9200
volumes:
- ./docker/elasticsearch/data:/bitnami/elasticsearch/data
as you see, I want to serve the store on my host machine port 8009.
My question is, what should I set base-url when using setup:install command and installing my Magento store?
If I set it to something like localhost or 127.0.0.1, when accessing localhost:8009 it redirects me to 127.0.0.1 which is obviously the wrong location
When you use to customer port in docker you want to use a base URL is URL: port.
example =>
base_url=https://localhost:8009/

Docker ports not being exposed properly

version: '3'
services:
app:
build: .
ports:
- "8000:8000"
volumes:
- .:/srv/redditaurus
environment:
- REDDIT_KEY=${REDDIT_KEY}
- REDDIT_SECRET=${REDDIT_SECRET}
links:
- mysql:mysql
mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
# volumes:
# - ./mysql:/var/lib/mysql/
nginx:
image: nginx
ports:
- "80:80"
This is my docker-compose.yml. The weirdest thing is happening. I can visit localhost:8000 and get the redditaurus app without any issue. However, if I try to do the same thing with localhost:80, or localhost:3306 from a mysql terminal, I'll get access denied or ERR_EMPTY_RESPONSE.
If I try 0.0.0.0:80, I get the default nginx page, so that's okay, but why won't localhost work?
MySQL refuses to be served on either localhost or 0.0.0.0. I've tried accessing it from Sequel Pro, from inside a linked container, and from my host machine's console, and nothing can get into it. If I exec into the SQL container, I can log in just fine, so it's not a password issue.
Why can't I get to my containers normally? :(
You have missing some configuration properties. try this
version: '3'
services:
app:
build: .
ports:
- "8000:8000"
volumes:
- .:/srv/redditaurus
environment:
- REDDIT_KEY=${REDDIT_KEY}
- REDDIT_SECRET=${REDDIT_SECRET}
links:
- mysql:mysql
mysql:
image: mysql
entrypoint: ['/entrypoint.sh', '--default-authentication-plugin=mysql_native_password']
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ALLOW_EMPTY_PASSWORD: "YES"
ports:
- "3306:3306"
nginx:
image: nginx
ports:
- "80:80"
if you want to connect mysql via terminal. run this
mysql -uroot -proot —protocol tcp
Next thing is your nginx binding with 80 is work correct.
Problem in here is not docker-compose. It can be in your os configurations.
I used mysql:5.7 tag in docker-compose, and that allowed the container to work. I guess the latest branch has some issue with my local env.
Still not sure what's up with nginx, but it's not an issue.

connect to mysql container from another container

I'm trying to connect to a mysql container from within another container on the same network specified by a compose file.
version: "2"
services:
web:
build:
context: .
dockerfile: nginx/Dockerfile
ports:
- "8080:80"
volumes:
- ./data:/srv
php:
build:
context: .
dockerfile: php-fpm/Dockerfile
volumes:
- ./data:/srv
mysql:
image: mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_USER=dummy_user
- MYSQL_PASSWORD=12345
I'm not really sure what the connection parameters would be if I'm trying to connect to the mysql container from the php container.
Specifically, what are the host and port for the mysql container from within another container of the same docker-compose network?
I've tried host: mysql port: 3306, but that doesn't seem to work.
You should link the containers. Add ports section to mysql container and links section to php container.
version: "2"
services:
web:
build:
context: .
dockerfile: nginx/Dockerfile
ports:
- "8080:80"
volumes:
- ./data:/srv
php:
build:
context: .
dockerfile: php-fpm/Dockerfile
links:
- mysql:mysql
volumes:
- ./data:/srv
mysql:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_USER=dummy_user
- MYSQL_PASSWORD=12345
With that configuration you'll be able to access mysql container from php with mysql:3306
A bit of documentation: https://docs.docker.com/compose/networking/#updating-containers
You don't share port for mysql
add to your docker-compose.yml to mysql service:
ports:
- "3306:3306"
UPD
try to set environments of your mysql container
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: dev
MYSQL_PASSWORD: 123456
MYSQL_DATABASE: myapp
and
$link = mysqli_connect("mysql", "dev", "123456", "myapp");
should works fine

connect to mysql database from docker container

I have this docker file and it is working as expected. I have php application that connects to mysql on localhost.
# cat Dockerfile
FROM tutum/lamp:latest
RUN rm -fr /app
ADD crm_220 /app/
ADD crmbox.sql /
ADD mysql-setup.sh /mysql-setup.sh
EXPOSE 80 3306
CMD ["/run.sh"]
When I tried to run the database as separate container, my php application is still pointing to localhost. When I connect to the "web" container, I am not able to connect to "mysql1" container.
# cat docker-compose.yml
web:
build: .
restart: always
volumes:
- .:/app/
ports:
- "8000:8000"
- "80:80"
links:
- mysql1:mysql
mysql1:
image: mysql:latest
volumes:
- "/var/lib/mysql:/var/lib/mysql"
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: secretpass
How does my php application connect to mysql from another container?
This is similar to the question asked here...
Connect to mysql in a docker container from the host
I do not want to connect to mysql from host machine, I need to connect from another container.
At first you shouldn't expose mysql 3306 port if you not want to call it from host machine. At second links are deprecated now. You can use network instead. I not sure about compose v.1 but in v.2 all containers in common docker-compose file are in one network (more about networks) and can be resolved by name each other. Example of docker-compose v.2 file:
version: '2'
services:
web:
build: .
restart: always
volumes:
- .:/app/
ports:
- "8000:8000"
- "80:80"
mysql1:
image: mysql:latest
volumes:
- "/var/lib/mysql:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: secretpass
With such configuration you can resolve mysql container by name mysql1 inside web container.
For me, the name resolutions is never happening. Here is my docker file, and I was hoping to connect from app host to mysql, where the name is mysql and passed as an env variable to the other container - DB_HOST=mysql
version: "2"
services:
app:
build:
context: ./
dockerfile: /src/main/docker/Dockerfile
image: crossblogs
environment:
- DB_HOST=mysql
- DB_PORT=3306
ports:
- 8080:8080
depends_on:
- mysql
mysql:
image: mysql:5.7.20
environment:
- MYSQL_USER=root
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_DATABASE=crossblogs
ports:
- 3306:3306
command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8 --explicit_defaults_for_timestamp

Resources