I have a Mysql server running on the host using default port 3306. I Want to run a MySQL docker container using network host but with a different port.
My configuration is defined in a docker-compose file. After building the image and tried running the container, it starts and shutdown with port conflict notice.
Is there a way to dynamically change the container port before starting up? I don't want to use the network bridge.
If using host networking is a hard requirement, then nothing in Docker space will be able to control or limit what ports the service does or doesn't use. You need to change a service-specific configuration file, environment variable, or command-line argument to make it listen somewhere else.
Particularly for servers that listen on a single TCP port (like most database and HTTP-based servers) the default Docker bridge/NAT setup should work fine; alternate setups like host networking and macvlan are unnecessary. If you're willing to use the standard setup, this is trivial:
version: '3'
services:
mysql:
image: mysql
ports: ['9999:3306'] # listen on host port 9999 instead
docker run --name 'dockername' -e MYSQL_ROOT_PASSWORD='password' -p 1000:3306 -d mysql
docker exec -it 'dockername' mysql -uroot -p
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'password'
flush privileges;
here 1000 is the port at which you want to run your mysql docker container.
Related
I have MariaDB running on docker. Bind address is commented out in the my.cnf file and everything appears set. I know what the IP address is and am currently trying to force the TCP connection (I am using this resource: https://mariadb.com/kb/en/installing-and-using-mariadb-via-docker/). I also have a java program in another docker container that is able to access the container - I just need to access the container myself to load some SQL. Any advice as to why it isn't connecting?
I am running this command right now (with the correct IP):
mysql -h 172.17.0.2 -P 3306 --protocol=TCP -u root -p
The documentation (soon to be fixed), misses -p 3306:3306 on the parameters when the container is run to expose the 3306 in the container to the host.
Recommend for next question explicitly show how the container was run, the docker inspect command used to derive your IP address, and the actual failure the mysql command line showed.
I've set up a docker container running a mysql instance on a remote computer I have. In the past this hasn't been an issue but for some reason I can't get it to work now. I am unsure what the issue might be. I am using docker compose and I can't seem to connect through mysql work bench on a different computer even those the container is running. Here are my details:
docker-compose.yaml
version: '3.7'
services:
api:
image: api
restart: unless-stopped
container_name: api
build: ./node/
ports:
- 3008:3008
mysql:
image: mysql
restart: unless-stopped
container_name: mysql
environment:
MYSQL_DATABASE: pitapaldb
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
build: ./database/
ports:
- 3306:3306
networks:
default:
external:
name: my-net
database/Dockerfile
FROM mysql
COPY init.sql /docker-entrypoint-initdb.d
database/init.sql
CREATE DATABASE mydb;
USE mydb;
SET SQL_SAFE_UPDATES = 0;
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'password';
flush privileges;
CREATE TABLE carts (
id int PRIMARY KEY AUTO_INCREMENT,
lat float,
lon float,
address varchar(255),
status boolean,
city_id int
);
container is definitely running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
784cf75183f4 mysql "docker-entrypoint.s…" 2 minutes ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
But when I try to connect via workbench I get 'unable to connect'. I've tried both username user and root with password password. The IP address I use definitely should work because I have other services operating from it with no issue:
#LoF10 Here is a quick list of things to check:
Can you connect within the docker network on the machine running the MySQL docker? An easy way of testing this is by running a command such as this on your remote machine:
docker run --rm -it --network my-net mysql:5.7 mysql -h mysql -uroot -ppassword
If not, there may be a problem with your MySQL config, MySQL data, or the initialization of the container. These are what #JorgeCampos is suggesting you verify. Since you are pulling directly from MySQL's Docker Hub entry, the config should be set properly to allow remote connections. If good, proceed. FYI, you will know you've connected successfully if you see mysql> on the terminal. To exit: \q.
Can you connect on exposed port on the localhost of the machine running the MySQL docker? An easy way of testing this is by running a command such as this on your remote machine:
docker run --rm -it --network host mysql:5.7 mysql -h 127.0.0.1 -uroot -ppassword
Make sure to use the IP used above and NOT localhost. This is b/c the MySQL client has special handling of the 'localhost' keyword by looking for mysqld locally. Using the 127.0.0.1 forces MySQL to connect via a proper socket connection. If you are not able to connect, then there is a problem with mapping the container's port to your host. If good, proceed.
Assuming both machines are on the same network and the machine that has MySQL Workbench also has docker, can you connect using the IP of the machine running MySQL container e.g. 10.0.0.4? An easy way of testing this is by running a command such as this on your remote machine:
docker run --rm -it mysql:5.7 mysql -h 10.0.0.4 -uroot -ppassword
If not, you may want to verify if you can:
Ping the 10.0.0.4 machine
If there are any firewall rules that prevent its proper exposure to the network. This happens commonly with Windows' default Firewall...
If on AWS, there are a number of reasons why you might not be able to reach if it has been properly assigned a Public Port e.g. Security Groups, Route Tables, Internet Gateway, etc.
Once you are able to proceed from 3 above, then you should be able to connect using MySQL Workbench as you've described.
Hope those help. Any more detailed recommendation will require you sharing more about your local networking setup (OS, Physical/Virtual, how you are determining IP's, etc).
I have a docker container running a Flask application that connects to a mySQL server. The mySQL server is hosted on the host machine at port 3308 on a windows 10 machine.
When executing
docker run -p 5000:5000 -p 3308:3308 -t webui
I receive the error
Ports are not available: listen tcp 0.0.0.0:3308: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
due to the port being used by the mySQL server on the host machine
How do I map the port of the mySQL to the docker container such that the Flask application can access the database?
There are 2 ways to achieve this. The first approach is the recommended one.
The first is to add an entry to /etc/hosts inside the container:
docker run -p 5000:5000 -p 3308:3308 --add-host database:<HOST_IP> -t webui
You need to replace HOST_IP with the network IP of your host. Then you can reference the database inside your container using the name "database" (you can also customize this one).
The second is to bind your container to your host's interface:
docker run -p 5000:5000 -p 3308:3308 --bind 127.0.0.1 -t webui
Then you can refer to your database with 127.0.0.1 inside your container.
The issue was caused by the host name. The mySQL database port did not need to be bound to the container as it did not need to receive any inbound calls, only outbound to the database. Resolved by adding a new entry into the container's /etc/hosts file as described here.
I have pulled an MYSQL image from Docker Hub and ran it by setting necessary credentials, such as username, password etc. Then I have inspected the container by:
docker inspect CONTAINER_ID
where I got the IP address for the MySQL databese. Since the Docker deamon connect through bridge network, my IP addess was: 172.17.0.2. By specifying this IP I can connnect to the Database from the host computer(Fedora). But, how do I connectd to the MySQL database from another machine in the same LAN?
You need to bind the container port to a host machine port. Adding -p to the docker run command:
docker run -p HOST_PORT:CONTAINER_PORT
I.e.:
docker run -p 3306:3306 .......
So MySQL is available from the Fedora's LAN interface.
I'm using the following Prestashop docker: https://hub.docker.com/r/prestashop/prestashop/
It so happens that I already have a mysql server running on the host, because that was included with the whole DirectAdmin panel.
The variable DB_SERVER does nothing if I specify localhost or the IP adres of my server. -p 3306:3306 is also not allowed, since that port is already in use.
How can a Docker container reach the host:3306 mysql server?
If I understand correctly, you have your Prestashop installation inside a container and MySQL server running on the Docker host?
Unless configured differently, both the container and the host are in the same bridge network.
Verify that your container is in the bridge network:
docker inspect <container-name>
"Networks": {
"bridge": {
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
}
}
Notice the gateway IP address - this is the address of the docker host inside the bridge network. Use it as a DB_SERVER variable to connect to the MySQL.
One year later...
Like what #NetworkMeister said, but, IPAddress instead.
docker inspect <container-name> | grep IPAddress
In other words, I used 172.17.0.2 and it worked.
I believe what you are looking to do is to connect your container process with mysql server that is running on that host. To retain using localhost as the assignment of DB_SERVER you can do the following...
docker run --net=host <YOUR_CONTAINER_OPTIONS>
By doing this your container will able to reach the mysql service that is deployed on localhost:3306.