Why can't I access to my local docker-compose from Browser? - docker

I've run my docker-compose file trying to dockerize pgadmin for Postgres but my browser cannot connect to pgadmin on url localhost:8080.
This is the docker-compose file that I am running
version: '3'
services:
db:
container_name: postgres_container
image: postgres
restart: always
environment:
POSTGRES_DB: postgres_db
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
PGDATA: /var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
- db-data:/var/lib/postgresql/data
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4:5.5
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: secret
PGADMIN_LISTEN_PORT: 80
ports:
- "8080:80"
volumes:
- pgadmin-data:/var/lib/pgadmin
volumes:
db-data:
pgadmin-data:
This is my docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c6a6a588f639 dpage/pgadmin4:5.5 "/entrypoint.sh" 3 hours ago Up 9 minutes 443/tcp, 0.0.0.0:8080->80/tcp pgadmin4_container
ad6fe3349717 postgres "docker-entrypoint.s" 3 hours ago Up 9 minutes 0.0.0.0:5432->5432/tcp postgres_container
When I try to connect to it from browser to localhost:8080 it says Connection attempt failed

I am using almost the same docker compose file on Windows 10 with WSL 2 and can connect immediately using Firefox build 100 browser to localhost:8080.
My difference is that the image for pgadmin4 in the compose file is image: dpage/pgadmin4
the latest not v5.5

I solved this problem. Turns out that DOCKER_HOST variable was set to 192.168.99.100:2376. You can see it by running command echo $DOCKER_HOST
I just ran my docker container into this port instead of localhost and everything worked fine.
docker run -d -p 192.168.99.100:9411:9411 openzipkin/zipkin
And I was able to access my docker container through the browser by url http://192.168.99.100:9411/ Thank you very much everyone

Related

Can't connect to Docker Container on Windows

I have a docker-compose file that looks like this
version: "3"
services:
db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: michael
POSTGRES_PASSWORD: pass123
admin:
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
PGADMIN_CONFIG_SERVER_MODE: 'False'
ports:
- "5050:5050"
I run docker-compose up -d and I can see my apps running from Docker Desktop. I cannot however connect to my pgadmin instance at port 5050 using localhost. Any ideas?
Docker container of pgAdmin by default runs on port 80 as per the documentation here https://www.pgadmin.org/docs/pgadmin4/latest/container_deployment.html
You are exposing port 5050 through the mapping. Either add a environment variable PGADMIN_LISTEN_PORT to the docker_compose to make pgAdmin run on port 5050
OR
change port mapping to 5050:80 for the pgAdmin service
Check the docker inspect or docker ps results to ensure that you have your port exposed correctly
Try to connect to it using the public IP

How to communicate mysql & mysqladmin docker containers [duplicate]

This question already has answers here:
[Docker]: Connecting PHPMyAdmin to MySQL doesnt work
(6 answers)
Closed 3 years ago.
I want to spawn MySQL & PHPMyAdmin docker containers. Mysql container can be accessed via 3306 port & PHPMyAdmin can be accessed through 8280 port.
My question is, how a PHP application can be configured to access the MySQL docker container on 3306 port
and the PHPMyAdmin can be configured for MySQL.
Thanks.
Start MySQL server
docker run --name mysql -e MYSQL_ROOT_PASSWORD=password -d mysql
Start phpAdmin with link to mysql container
docker run --name myadmin -d --link mysql:db -p 8080:80 phpmyadmin/phpmyadmin
PHPAdmin application will be serving on localhost:80. MySQL credentials will be root/password.
We can now use docker-compose for this solution which is more portable.
You can use the offical image for MySQL and PHPMyAdmin.
version: '3.1'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: example
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8081:80
To access it from php container, just add your php container in the docker-compose, so the connection string should be like
host:db
user: root
pass: example
You can use Adminer a database management tool.
You need the below configuration in docker-compose.yml for Mysql and adminer.
mysqldb:
container_name: mysqldb
image: mysql
restart: always
environment:
- MYSQL_USER= 'xyz'
- MYSQL_PASSWORD='pwd0123456789'
- MYSQL_DB= 'testdb'
networks:
- main
adminer:
container_name: adminer
image: adminer
restart: always
ports:
- 4041:8080
networks:
- main-network
depends_on:
- mysqldb

Mapping ports in docker-compose file doesn't work. Network unreachable

I'm trying to map a port from my container, to a port on the host following the docs but it doesn't appear to be working.
After I run docker-compose -f development.yml up --force-recreate I get no errors. But if I try to reach the frontend service using localhost:8081 the network is unreachable.
I used docker inspect to view the IP and tried to ping that and still nothing.
Here is the docker-compose file I am using. And I doing anything wrong?
development.yml
version: '3'
services:
frontend:
image: nginx:latest
ports:
- "8081:80"
volumes:
- ./frontend/public:/var/www/html
api:
image: richarvey/nginx-php-fpm:latest
ports:
- "8080:80"
restart: always
volumes:
- ./api:/var/www/html
environment:
APPLICATION_ENV: development
ERRORS: 1
REMOVE_FILES: 0
links:
- db
- mq
db:
image: mariadb
restart: always
volumes:
- ./data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: dEvE10pMeNtMoDeBr0
mq:
image: rabbitmq:latest
restart: always
environment:
RABBITMQ_DEFAULT_USER: developer
RABBITMQ_DEFAULT_PASS: dEvE10pMeNtMoDeBr0
You are using docker toolbox. Docker toolbox uses docker machine. In Windows with docker toolbox, you are running under a virtualbox with its own IP, so localhost is not where your containers live. You will need to go 192.168.99.100:8081 to find your frontend.
As per the documentation on docker machine(https://docs.docker.com/machine/get-started/#run-containers-and-experiment-with-machine-commands):
$ docker-machine ip default
192.168.99.100

docker-compose creating multiple set of services

I'm trying to create 3 mattermost services on 1 AWS EC2 machine.
Let me explain further with more texts:
When I run docker-compose up -d, I get a service whose structure is like this:
How can I modify the docker related scripts so that I can create 3 sets of service?
I've tried docker-compose up --scale app=3 --scale web=3 --scale db=3. But I can't find any way to specify different port for each of the App container.
The only solution I've found is:
Create 3 copies of mattermost-docker folder.
Change the App port and database connection information.
Run docker-compose up -d 3 times in mattermost-docker1, mattermost-docker2, and mattermost-docker3 separately.
But this solution creates a lot of duplicated files. I don't like it.
Anyone knows how to create 3 sets of mattermost services?
You need to specify a port range in docker compose:
For example, for 10 container scalling:
version: '3'
services:
web:
...
ports:
- "80-90:443"
app:
...
ports:
- "8000-8010"
Note that you don't need to change port inside container (443, 444, 445). You can use the same, and furthermore that's recommended, because although you use different containers in a port range, is easier if they use the same nginx configuration.
This starts sets of services using 3 separate databases (nginx and mongo used as example).
version: '3'
services:
web1:
container_name: web1
image: nginx:latest
ports:
- 8080:8080
app1:
container_name: app1
image: nginx:latest
ports:
- "8081:8081"
db1:
container_name: db1
image: mongo
ports:
- 27017
web2:
container_name: web2
image: nginx:latest
ports:
- 8082:8082
app2:
container_name: app2
image: nginx:latest
ports:
- "8083:8083"
db2:
container_name: db2
image: mongo
ports:
- 27018
web3:
container_name: web3
image: nginx:latest
ports:
- 8084:8084
app3:
container_name: app3
image: nginx:latest
ports:
- "8085:8085"
db3:
container_name: db3
image: mongo
ports:
- 27019
Local Test:
NAMES STATUS PORTS IMAGE
db2 Up About a minute 27017/tcp, 0.0.0.0:32803->27018/tcp mongo
web1 Up About a minute 80/tcp, 0.0.0.0:32802->8080/tcp nginx:latest
db1 Up About a minute 0.0.0.0:32801->27017/tcp mongo
app1 Up About a minute 80/tcp, 0.0.0.0:32800->8081/tcp nginx:latest
app3 Up About a minute 80/tcp, 0.0.0.0:32798->8085/tcp nginx:latest
db3 Up About a minute 27017/tcp, 0.0.0.0:32799->27019/tcp mongo
app2 Up About a minute 80/tcp, 0.0.0.0:32797->8083/tcp nginx:latest
web3 Up About a minute 80/tcp, 0.0.0.0:32796->8084/tcp nginx:latest
web2 Up About a minute 80/tcp, 0.0.0.0:32795->8082/tcp nginx:latest

How to name a volume using a docker-compose.yml file?

I'm new to Docker and I'm trying to find out how to set the name of the created data volume. Currently the directory is automatically named as a long hash under /var/libs/docker which is far from user friendly.
I'm attempting to set up a development environment for MODX as shown here:
https://github.com/modxcms/docker-modx
Currently my docker-compose.yml file is as follows:
web:
image: modx
links:
- db:mysql
ports:
- 80:80
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
ports:
- 3306:3306
command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION
myadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8080:8080
This works perfectly but I'm unsure as to how to name the data volume that I would edit directly with my IDE.
(As a side question, does it have to be created under /var/libs/docker ? Or is there a way of setting it to a directory in my home folder?)
Update:
Thanks to the help from #juliano I've updated my docker-compose.yml file to:
version: '2'
services:
web:
image: modx
volumes:
- html:/home/muzzstick/dev/modxdev
links:
- db:mysql
ports:
- 80:80
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
ports:
- 3306:3306
command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION
myadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8080:8080
volumes:
html:
external: false
Unfortunately this seems to stop the web container from running.
db and myadmin containers show they're running ok.
There weren't any errors... if I type docker start docker_web_1 it appears to start but docker ps -a shows it exited as soon as it started.
Update 2
Running docker-compose up -d appears to run without issue. But then as you can see below, the web container exits as soon as it's created.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1dd6d8ac94e modx "/entrypoint.sh apach" 10 seconds ago Exited (1) 5 seconds ago docker_web_1
ee812ae858dc phpmyadmin/phpmyadmin "/run.sh phpmyadmin" 10 seconds ago Up 5 seconds 80/tcp, 0.0.0.0:8080->8080/tcp docker_myadmin_1
db496134e0cf mysql "docker-entrypoint.sh" 11 seconds ago Up 10 seconds 0.0.0.0:3306->3306/tcp docker_db_1
Update 3
OK the error logs for this container shows:
error: missing MODX_DB_HOST and MYSQL_PORT_3306_TCP environment variables
Did you forget to --link some_mysql_container:mysql or set an external db
with -e MODX_DB_HOST=hostname:port?
This error appears to be originating from https://github.com/modxcms/docker-modx/blob/master/apache/docker-entrypoint.sh#L15-L20
Could it be something like linking is handled differently in docker-compose version 2?
To create a named data volume using the version 2 of compose files you will have a separated area:
version: '2'
services:
db:
image: postgres
volumes:
- amazingvolume:/var/lib/postgresql/data
volumes:
amazingvolume:
external: true
So you can define the volume name (amazingvolume), if it's external or not and under your service (db in this example) you can define which directory you gonna mount.
Just search in the docker documentation for hosted mounted volumes:
version: '2'
services:
web:
image: modx
environment:
- MYSQL_PORT_3306_TCP=3306
- MODX_DB_HOST=mysql:3306
volumes:
- /home/muzzstick/dev/modxdev/html:/var/www/html
links:
- db:mysql
ports:
- 80:80
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
ports:
- 3306:3306
command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION
myadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8080:8080
Change /var/www/html to the directory where the html files will be inside the container. And also create the directory at the left in your host and give read permission to all users.
Regards

Resources