Hey I don't know if the title is right, but I'm working on my project. Tools that I use are symfony and docker. When I start pgsql database on my other machines, they're using port 5432, but I just installed fresh linux on my new computer and it uses port 49153 it took me quiet some time to figure out that the port was the problem. Same thing happens if I start new project don't change anything and pg database still runs on port 49153, it's a bit annoying while working on few machines. So is it possible to force docker to setup database on port 5432 for all of my future projects or everytime I have to change port in .env file?
My docker-compose.yml
services:
database:
image: postgres:${POSTGRES_VERSION:-14}-alpine
environment:
POSTGRES_DB: ${POSTGRES_DB:-app}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-!ChangeMe!}
POSTGRES_USER: ${POSTGRES_USER:-app}
volumes:
- db-data:/var/lib/postgresql/data:rw
.env file
DATABASE_URL="postgresql://app:!ChangeMe!#127.0.0.1:5432/app?serverVersion=14&charset=utf8"
For exemple if you want to map your pgsql database :
From the local docker container port 5432
To your local env 5432 port.
Add
ports:
- "5432:5432"
I forgot which one is the container and which one is the local but you will find out pretty easier now.
services:
database:
image: postgres:${POSTGRES_VERSION:-14}-alpine
environment:
POSTGRES_DB: ${POSTGRES_DB:-app}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-!ChangeMe!}
POSTGRES_USER: ${POSTGRES_USER:-app}
volumes:
- db-data:/var/lib/postgresql/data:rw
ports:
- "5432:5432"
Related
I have a very simple docker-compose.yml:
version: '3.1'
services:
db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: my_password
POSTGRES_DB: demo
And start it with: docker-compose up -d
It starts well:
Creating network "bootcamp_default" with the default driver
Creating bootcamp_db_1 ... done
Name: bootcamp_db_1
Command: docker-entrypoint.sh postgres
State: up
Ports: 0.0.0.0:5432->5432/tcp,:::5432->5432/tcp
When I go into the container I see that demo db exists, but when I try to connect to it any utils say that demo db is not existed.
How can I connect to it?
Solved the problem by moving the project to Ubuntu system. It was caused by OS settings.
This is my docker-compose.yml
services:
db:
container_name: postgres
image: postgres:latest
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: test_db
pgadmin:
container_name: pgadmin
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#test.com
PGADMIN_DEFAULT_PASSWORD: admin123
ports:
- 8002:80
Pg-admin is running in port 8002
To connect pgsql with pgadmin I have used below credential to add new server in pgadmin, I am getting below error,
How do I will map this connection ?
In docker panel it's showing postgres is running
My operating system : Mac m1.
When you using more than one container and there are related, localhost not works.
Instead of localhost set the ip of the host in which your postgress is running.
Read these topics to understand ip vs localhost in docker
https://stackoverflow.com/a/52213178/3957754
https://stackoverflow.com/a/63207679/3957754
I build a website using Strapi and Gatsby, everythings works well when I try to connect to a remote database, but I'm trying to create a db inside a container and so far no luck.
Essentially, what I did is create the following docker-compose:
version: '3'
services:
backend:
container_name: myapp_backend
build: ./backend/
ports:
- '3002:3002'
volumes:
- ./backend:/usr/src/myapp/backend
- /usr/src/myapp/backend/node_modules
environment:
- APP_NAME=myapp_backend
- DATABASE_CLIENT=mysql
- DATABASE_HOST=db
- DATABASE_PORT=3307
- DATABASE_NAME=myapp_db
- DATABASE_USERNAME=johnny
- DATABASE_PASSWORD=stecchino
- DATABASE_SSL=false
- DATABASE_AUTHENTICATION_DATABASE=myapp_db
- HOST=localhost
depends_on:
- db
restart: always
db:
container_name: myapp_mysql
image: mysql:5.7
volumes:
- ./db.sql:/docker-entrypoint-initdb.d/db.sql
restart: always
ports:
- 3307:3307
environment:
MYSQL_ROOT_PASSWORD: 5!JF6!FgAkvt
MYSQL_DATABASE: myapp_db
MYSQL_USER: johnny
MYSQL_PASSWORD: stecchino
command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci --init-connect='SET NAMES UTF8;' --innodb-flush-log-at-trx-commit=0
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: 'myapp_phpmyadmin'
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3307
ports:
- '8081:80'
volumes:
- /sessions
depends_on:
- db
frontend:
container_name: myapp_frontend
build: ./frontend/
ports:
- '3001:3001'
depends_on:
- backend
volumes:
- ./frontend:/usr/src/myapp/frontend
the backend service contains the Strapi application, the db service contains the mysql instance which runs on the port 3307 'cause 3306 is already in use.
Then I have also installed phpmyadmin, and last but not least the Gastby site. When I run using docker-compose up --build, and try to access to phpmyadmin using:
http://localhost:8081/index.php
with the following credentials:
user: johnny
pwd: stecchino
I get:
MySQL mysqli::real_connect():(HY000/2002): Connection refused
now, what I did for fix that situation is pass the port 3306 instead of 3307 to backend and phpmyadmin service. And magically, everything works. But why? I have mapped container and host to 3307...
There are 2 things happening here.
Mysql is running on port 3306.
This is because you never told the mysql container to run on port 3307. The default configuration is running on 3306.
phpadmin can connect to mysql at port 3306.
Of course it can. This is because when you define multiple services within the same docker-compose file, they start on the same network. This means that they can see and connect to each other's internal ports without the need for external port binding like 3306:3306
I would suggest to keep port bindings only for services that you want access outside the docker environment (like the UI), and for internal components just expose the port like this
expose:
- 3306
Both answers are useful, I am particularly fond of Manish's answer
I wanted to add some additional wording:
There are the internal docker networks which nothing from the outside can gain access to. From inside any given service (or container), you can reach every other service (or container) via:
<service-name>:<port>/path/of/resources
<container-name>:<port>/path/of/resources
In order to access resources inside the docker network from outside of docker, whether that is from your host environment, or farther upstream on the internet, the docker daemon needs to bind to host ports, and then forward information received on those ports to a docker service (and ultimately a docker container).
In your docker-compose.yml when you do the 3307:3307 you are telling the docker daemon to listen on port 3307, and forward to your db service internally on it's port 3307.
However, from what we can all see, mysql is still internally (that is, inside the container) listening for traffic on port 3306. Any containers or services on the same docker networks as your db service (mysql running container(s)) would be able to access mysql via something like:
<driver>:mysql://db:3306/<dbname>
If you wanted all host traffic and docker network traffic to access mysql on port 3307, you would also need to configure mysql to listen on port 3307 instead of 3306. That tidbit of information does not appear to be in your question at the time of writing.
I hope the additional information helps! It's a topic I chat often about when talking docker with folks.
Because 3306 is the exposed port by the official Dockerfile.
What you can do is to map the port that is running MySQL to another port on your host: 3307:3306 for instance (always host:container)
I was reading a, here, here, here, here, here
none of which answers my question.
What I want to do?
I have a mysql docker-compose image and want to connect to it from my ubuntu host using
localhost:3306
this does not work
it does work if I use
0.0.0.0:3306
which is not what I want to do. Why do I want to do all of that? because I have to start to work on an oooold legacy app, that has an old mysql version. Now I have mysql 8.0 on my computer and dont want to downgrade just for that one project. The legacy code has about 1000 references to localhost:3306 in it. Now I could refactor all that, create a config file etc... but better would be, if I could make it work so that localhost:3306 actually accesses my mysql docker-compose container. Is that possible? What do i have to add to my docker-compose yaml file?
my mysql docker-compose yaml file is this:
version: '3.3'
services:
sciodb:
container_name: sciodb
image: mysql:5.6
restart: always
environment:
MYSQL_DATABASE: 'db'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'myuser'
# You can use whatever password you like
MYSQL_PASSWORD: 'test1234'
# Password for root access
MYSQL_ROOT_PASSWORD: 'test1234'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3306:3306'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- /home/myuser/nmyapp_db:/var/lib/mysql
- /media/sf_vmwareshare:/var/vmwareshare
I'm trying to use a docker-compose.yml for launching mariabd and phpmyadmin. When I edit something on phpmyadmin it kicks me out to login page.
db:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: Pass123
restart: always
volumes:
- "./.data/db:/var/lib/mysql/:rw"
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:mysql
ports:
- 8181:80
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: Pass123
PMA_HOST: mysql
I've tried with a volume container with busybox to keep data of mysql, changed mariabd for mysql image. But I don't get with the solution. What should I do to solve this?
Thanks in advance
The set of environmental variables supported by the phpmyadmin/phpmyadmin Docker image is different from that of the mariadb image. Try replacing the MYSQL_USERNAME and MYSQL_ROOT_PASSWORD variables of your phpmyadmin service with PMA_USER and PMA_PASSWORD, respectively.
I don't understand the meaning of the link
links:
- db:mysql
The configuration file of phpmyadmin/phpmyadmin (/www/config.inc.php) say by default the host name of database server if 'db' :
$hosts = array('db');
As you name the database server 'db' then link should be write likez this :
links:
- db
If your database name container is not 'db', you should add the environment variable PMA_HOST= (or PMA_HOSTS if multi db servers) with the right name
All over environment variables are useless (even in db config I think)