Docker gogs connection is refused by mysql container - mysql

I have some trouble with setting my local git repository. I am new to docker, so the problem may be naive but I still can't find it.
So my idea is:
I wanted to create a container with gogs (gogs/gogs image) and connect it to mysql container.
To do so I have created docker-compose.yml file.
version: '3'
services:
db:
image: mysql
ports:
- "10023:3306"
environment:
- MYSQL_ROOT_PASSWORD=root!
ui:
image: phpmyadmin/phpmyadmin
ports:
- "8989:80"
links:
- db:mysql
repo:
image: gogs/gogs
ports:
- "10022:22"
- "10080:3000"
volumes:
- /tmp/gogs:/data gogs/gogs
links:
- db:mysql
I all put phpmyadmin in my setup, this way I can easily test if mysql is up and respond to other containers.
Sadly this environment does't work, when get to gogs install page on localhost:10080 and try to create a new repo, it says that tcp connection has been refused. This is the output of the error message:
dial tcp 127.0.0.1:10023: getsockopt: connection refused
This is strange, because I can access to mysql container through phpmyadmin. I also was able to create gogs database.
Do anybody had this issue before?

Don't use localhost or 127.0.0.1 from inside the container, use the service name as defined in your docker-compose.
dial tcp db:10023
docker-compose networking.

Thank a lot to gogsdoc_db_1. His/her answer is perfect. I will try to explain what I have done wrong, so maybe this would help new entry in docker such as myself.
As bluescores, have said. You shouldn't use localhost or 127.0.0.1 inside you docker container.
Why not?
Basically, when you use docker-compose it automatically create shared network for your app, which means you shouldn't use the published port.
In may case I must use port 3306, instead of 10023 to connect to my database container.
I should still use port 10023 if I want to connect to mysql from my local machine.
so my configuration now is
P.S.
Do not forget to create the database, before you install gogs

Related

Can't access MySQL Docker container remotely

I have the following docker-compose.yml configuration:
version: "3.9"
services:
db:
image: "mysql:8.0.23"
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=somethingsomething
volumes:
- /home/gutta-server/Servers/mysql/hvb_db:/var/lib/mysql
ports:
- 3306:3306
expose:
- 3306
adminer:
image: "adminer:4.8.0-standalone"
restart: always
ports:
- 7000:8080
I have also opened ports 3306 and 7000 on the router that the host is connected to. Going to <my-domain>:7000 in my browser, from an external network, I am able to log in to Adminer and manage my database. I have also ran ufw allow 3306 on the host.
It's also worth noting that running telnet localhost 3306 on the host server (the one that actually runs the container) works, and confirms to me that the local port mapping is working just fine.
However, running the same command, just as telnet <my-domain> 3306 from a remote computer, not on the same network, yields the following:
Trying <my-domain's-ip>...
telnet: Unable to connect to remote host: Resource temporarily unavailable
Therefore, something must be wrong with the remote connection, not the actual database itself.
I feel like I've scoured SO dry, but having experience with these forums, surely someone will links this exact issue :p Anyway, here are the things I've tried:
Port forwarding port 3306 in router configuration (I have other port forwards too, which work perfectly fine, shouldn't be a router or ISP issue)
Allowing port 3306 though the host's firewall (ufw allow 3306)
Add bind-address = 0.0.0.0 to the container's /etc/mysql/my.cnf file and restarting the container (docker-compose restart doesn't remove this entry, stopping and starting does. I would solve this with a volume if it actually solved the issue, but it didn't)
Completely disable ufw (after suggestion in comments)
I literally can't think of a single other thing that could possibly be wrong, and I can't find anything more on the internet, either..
I really hope I've provided enough information here, please do let me know if anything else is needed.

Why won't TeamCity connect to MySQL when using docker-compose?

I'm trying to get TeamCity server running using docker-compose. Here's my compose file:
version: '3'
services:
db:
image: mysql
container_name: teamcity-db
restart: unless-stopped
env_file: .env
environment:
- MYSQL_DATABASE=teamcity
volumes:
- mysql:/var/lib/mysql
command: '--default-authentication-plugin=mysql_native_password'
teamcity:
depends_on:
- db
image: jetbrains/teamcity-server
container_name: teamcity
restart: unless-stopped
volumes:
- datadir:/data/teamcity_server/datadir
- logs:/opt/teamcity/logs
ports:
- "8111:8111"
volumes:
mysql:
datadir:
logs:
I've been successful getting wordpress set up using a very similar technique, and I can run phpMyAdmin and link it to the MySQL container and see the database, so its there.
When I browse to the teamcity web address, it shows me the initial setup screen as expected. I tell it to use MySQL and I put in 'root' as teh username and my MySQL root password. Teamcity then shows this:
I'm sure it's something simple but I just can't see what's wrong. Any ideas?
Solved! Here is my solution and some other learnings.
The problem was that I was telling TeamCity to use 'localhost' as the database server URL. This seems intuitive because all the services are on the same machine, but is incorrect. It is as if each container is its own host and so 'localhost' is specific to each container. 'localhost' inside a container refers to the container itself, not the host machine or any other container. So 'localhost' on the teamcity service refers to the teamcity server, not the database server, and that's why it couldn't connect.
The correct address for the database server based on my docker-compose.yml file is db (the service name of the database container). The service name becomes the host name for that container and docker resolves these as DNS names correctly within the composed group.
Also note: the default virtual network is created implicitly by docker-compose and allows all of the containers in the composed group to communicate with each other. The name of this network derives from the folder where the docker-compose.yml file is located (in my case ~/projects/teamcity) so I get a network called teamcity_default. All servers on this private vitual network are visible to each other with no further configuration needed.
The teamcity server container explicitly exposes port 8111 on the host's network interface, so it is the only container visible to the outside world. You do not need to (and probably should not) expose ports if you only need the servers to talk to each other. For example, the database server does not need to have a ports entry because it is automatically exposed on the private inter-container network. This is great for security because all the back-end services are hidden from the physical LAN and therefore the Internet.

Docker-Compose Services Not Communicating

Docker noob alert. Hope this isn't a dumb question but I cannot seem to figure out what is going on. I am trying to create a docker-compose file which creates a mysql db with a mounted volume and a go webserver app that connects to the mysql db.
Here is my docker-compose file:
services:
db:
image: mysql:8.0.2
environment:
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: northernairport
ports:
- "3306:3306"
volumes:
- /data:/var/lib/mysql
web:
depends_on:
- db
build: .
ports:
- "8080:8080"
My go application can't seem to connect to my mysql db though, I thought the "depends_on" would ensure this was possible.
Error I get:
panic: dial tcp 127.0.0.1:3306: getsockopt: connection refused
Can anyone tell me what I am doing wrong here? Thanks.
The depends_on only controls the build and startup order for the services.
Your actual issue is more likely that you are using the wrong address from your web application to your database. I see that you have not defined any networks, so you are using the default network created for your application by docker-compose. This will publish each service by name on the default network's DNS.
So, your web application should probably be using db:3306 as the database address, not localhost:3306 or 127.0.0.1:3306 as indicated in the error message.
The ports part is used to map container ports with host in following format ports (HOST:CONTAINER). Which means that you are trying to access host's machine, configure web app to connect to db:3306 instead.

Connecting to MySql server running in a Docker Container

I have a little problem, I can't seem to connect to mysql server community edition running inside a docker container.
I can easily connect to mysql server using the cli by using:
docker exec -it mysqlserver mysql -uroot -p
But if I try to connect with any other database connector, like DataGrip or MySql Workbench, I get an access denied.
But I changed nothing in the configuration files. I set ip as localhost, using the default 3306 port that the container exposed. username I keep as root and the password is exactly the same but it still keeps failing.
Am I missing something, not understanding anything properly?
Some help would be greatly appreciated!
Extra info: I am using MacOS, running the container with Docker for Mac and I am using as of this moment the latest MySql database version.
your container doesn't contain other connectors, try to publish port when you run your container with docker run --name mysql -p 127.0.0.1:3306:3306 .... to link port. And you can connect you to the container with your local cli. Try this doc
Below is the docker-compose.yml that I used to start up a MariaDB instance for testing some queries. MariaDB is API-compatible with MySQL, so no difference.
version: "2"
services:
db:
image: bitnami/mariadb:latest
volumes:
- ./mariadb/data:/bitnami
ports:
- "9001:3306"
environment:
MARIADB_ROOT_PASSWORD: ChangeMeIfYouWant
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- "9010:80"
links:
- db:db
The image is downloaded and the container created and started via docker-compose up. Afterwards, I can easily connect to it using JetBrains DataGrip.
host: localhost
port: 9001
user: root
password: ChangeMeIfYouWant

How to connect to a MySQL Docker Container via SSH?

When I try to tunnel via SSH to the Host Mashine (vServer) and then try to connect via the internal docker Container-IP then I can't connect to MySQL.
This is my docker-compose file.
version: '2'
services:
mysql:
build: ./mysql
environment:
MYSQL_ROOT_PASSWORD: test
volumes:
- ./db:/var/lib/mysql
The only solution I found was to forward the MySQL-Port of the mysql container to the Host-Mashine.
version: '2'
services:
mysql:
build: ./mysql
environment:
MYSQL_ROOT_PASSWORD: test
volumes:
- ./db:/var/lib/mysql
ports:
- 3306:3306
Then I am able to connect via the Host IP to MySQL but this is without SSH its direct via TCP and the port.
This is a No-Go for me to bring the MySQL Service into the internet.
Reasons can be found here https://security.stackexchange.com/questions/63881/is-it-not-safe-to-open-mysqls-port-to-the-internet why it is not a good practice to bring your mysql port into the internet.
So what is a good practice to connect to my docker mysql container with SSH but keep the mysql ports closed?
One simple way is to bind the MySQL port only to the localhost address. That assumes the host has a mysql client available outside of Docker.
ports:
- 127.0.0.1:3306:3306
You could also omit the ports section completely (no port binding at all), and use the mysql client that's already inside the container.
docker-compose exec mysql bash
Then run the mysql command inside the container to do whatever queries you want to do.
An easy way to achieve this is to forward the ssh port of the docker conatiner to some port on your host, i.e.
ports:
- 22:<some free host port>
and then access the container via ssh to the host port you used. Note, that it is a bad idea to use port 22, since that will cause a conflict with the ssh server running on your host.