Docker ports not being exposed properly - docker

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.

Related

Change default 0.0.0.0 from docker to specific

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

Cannot log in to the MySQL server through browser

Each time when I try to access the phpmyadmin from browser I receive this error:
"Cannot log in to the MySQL server"
I tried to change networks, restart docker.
version: '3'
services:
web:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./public_html:/public_html
- ./conf.d:/etc/nginx/conf.d
networks:
- nginxphp
php:
image: php:7.1.11-fpm-alpine
volumes:
- ./public_html:/public_html
expose:
- 9000
networks:
- nginxphp
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: root
MYSQL_PASSWORD: root
depends_on:
- db
ports:
- "8080:80"
networks:
nginxphp:
Cannot log in to the MySQL server
mysqli_real_connect(): The server requested authentication method unknown to the client [caching_sha2_password]
mysqli_real_connect(): (HY000/2054): The server requested authentication method unknown to the client
Disclaimer: I'm not a Docker user!
Your didn't mention if you're using the browser on the same computer as the sever or remotely. You'll need access to the mysql server (mysqld) through a terminal (command prompt). If this is a new install, it must be on the computer that is running mysql server.
In the Docker mysql page:
"The default configuration for MySQL can be found in /etc/mysql/my.cnf, which may include additional directories such as /etc/mysql/conf.d or /etc/mysql/mysql.conf.d."
Try looking in the /etc/mysql/my.cnf file on the server you're trying access. first. You're looking for:
bind-address = x.x.x.x
This is the address that the mysql server will talk to ("bound to"). Its typically "localhost" or "127.0.0.1".
To eliminate the error message like you see, I had to do two things:
1) change 'bind-address to 0.0.0.0'
this allows the server to connect to any computer. However, THIS IS A SECURITY RISK. Once you get it working, go read about bind addresses on the mysql website and set it appropriately.
2) Create an new account user#ipaddr where user is the new username and IPAddress is the ip4 address of the computer your trying to connect from. i.e.:
CREATE USER 'user'#'192.168.1.68' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'user'#'192.168.1.68';
Now try accessing mysql through the terminal program using the new username on the computer with the ip you entered above by typing:
mysql -uuser -ppassword -hx.x.x.x
Hopefully, this will help point you in the right direction. There's a ton of information about bind addresses and security on the web.
Because the phpmyadmin container has connected to the default host localhost. But your mysql server is located in other container (it means you cannot connect to mysql server by using localhost). So in the phpmyadmin service, you have to set PMA_HOST=db. See full env variables: https://hub.docker.com/r/phpmyadmin/phpmyadmin/
Full docker-compose.yml:
version: '3'
services:
web:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./public_html:/public_html
- ./conf.d:/etc/nginx/conf.d
networks:
- nginxphp
php:
image: php:7.1.11-fpm-alpine
volumes:
- ./public_html:/public_html
expose:
- 9000
networks:
- nginxphp
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: root
MYSQL_PASSWORD: root
depends_on:
- db
ports:
- "8080:80"
networks:
nginxphp:
If you are using phpmyadmin with latest mysql version you will have some login issues:
Cannot log in to the MySQL server mysqli_real_connect():
The server requested authentication method unknown to the client [caching_sha2_password] mysqli_real_connect(): (HY000/2054):
The server requested authentication method unknown to the client
The solution is to downgrade to mysql 5.7 or another.
I tested with mysql 5.7 and it works for me.
If you want to can test with another versions and let the community know.
Below is the docker-compose file that builds and run Nginx + php 7.1 + mysql 5.7 +phpmyadmin
version: '3'
services:
web:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./public_html:/public_html
- ./conf.d:/etc/nginx/conf.d
networks:
- nginxphp
php:
image: php:7.1.11-fpm-alpine
volumes:
- ./public_html:/public_html
expose:
- 9000
networks:
- nginxphp
db:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
depends_on:
- db
ports:
- "8080:80"
networks:
nginxphp:
Hope this will save some time to someone!

Dockerize Phoenix Application and use a virtual host

I'm trying to setup my environment to develop Phoenix apps using Docker.
Unitil this point everything is great, except the VIRTUAL_HOST part, I'd like to access my app by visiting app.dev instead of localhost:4000.
I'm using this docker-compose.yml file :
version: '2'
services:
proxy:
image: jwilder/nginx-proxy
ports:
- 80:80
postgres:
image: postgres:latest
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
web:
build: .
command: mix phx.server
volumes:
- .:/app
ports:
- 4000:4000
depends_on:
- postgres
environment:
- MIX_ENV=dev
- VIRTUAL_HOST=app.dev
- VIRTUAL_PORT=4000
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
links:
- postgres
when I try to access the app.dev I'm getting site can't be reached.
edit #1
For using VIRTUAL_HOST, do I really need the reverse proxy for this ? or a simple dns or something will be enough ?
edit #2
Ok, that's strange, when I curl the app.dev I get the html content, but I can't access it (app.dev) from the browser.
You don't need nginx, you just need to add app.dev to your /etc/hosts file.
127.0.0.1 app.dev

Can't to connect to postgres container

I define postgres server in docker-compose.yml:
db:
image: postgres:9.5
expose:
- 5432
Then in another docker container I tried to connect to this postgres container. But it gives an error with warning:
Is the server running on host "db" (172.22.0.2) and accepting
data-service_1 | TCP/IP connections on port 5432?
Why container can't to connect to another by provided information (host="db" and port=5432)?
PS
Full docker-compose.yml:
version: "2"
services:
data-service:
build: .
depends_on:
- db
ports:
- "50051:50051"
db:
image: postgres:9.5
depends_on:
- data-volume
environment:
- POSTGRES_USER=cobrain
- POSTGRES_PASSWORD=a
- POSTGRES_DB=datasets
ports:
- "8000:5432"
expose:
- 5432
volumes_from:
- data-volume
# - container:postgres9.5-data
restart: always
data-volume:
image: busybox
command: echo "I'm data container"
volumes:
- /var/lib/postgresql/data
Solution #1. Same file.
To be able to access the db container, you have to define your other containers in context of docker-compose.yml. When containers are started, each container gets all other containers mapped in /etc/hosts.
Just do
version: '2'
services:
web:
image: your/image
db:
image: postgres:9.5
If you do not wish to put your other containers into the same docker-compose.yml, there are other solutions:
Solution #2. IP
Do docker inspect <name of your db container> and look for IPAddress directive in the result listing. Use that IPAddress as host to connect to.
Solution #3. Networks
Make your containers join same network. For that, under each service, define:
services:
db:
networks:
- myNetwork
Don't forget to change db for each container you are starting.
I usually go with the first solution during development. I use apache+php as one container and pgsql as another one, a separate DB for every project. I do not start more than one setting of docker-compose.yml, so in this case defining both containers in one .yml config is perfect.
the depends on is not correct. i would try to use other paramters like LINKS and environment:
version: "2"
services:
data-service:
build: .
links:
- db
ports:
- "50051:50051"
volumes_from: ["db"]
environment:
DATABASE_HOST: db
db:
image: postgres:9.5
environment:
- POSTGRES_USER=cobrain
- POSTGRES_PASSWORD=a
- POSTGRES_DB=datasets
ports:
- "8000:5432"
expose:
- 5432
#volumes_from:
#- data-volume
# - container:postgres9.5-data
restart: always
data-volume:
image: busybox
command: echo "I'm data container"
volumes:
- /var/lib/postgresql/data
this one works for me (not postgres but mysql)

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