schw4ndi
schw4ndi

Reputation: 231

Connect to MySQL Server inside Docker container in an Microsoft Azure VM

I created an Ubuntu 15.10 VM on Microsoft Azure. On the Server i created a docker container with MySQL running (IP: 172.17.0.2)

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
4255233555db        mysql               "/entrypoint.sh mysql"   16 hours ago        Up 16 hours         0.0.0.0:3306->3306/tcp   compose_mysql_1

I can connect over ssh to the VM and then access the MySQL-Server running in the docker container.

I now wanna have access to the MySQL-Server from the outside so that my WebApp on Microsoft Azure (same Ressource Group) can connect to the database.

I already forwarded the port 3306 of the VM to 172.17.0.2:3306.

With MySQL-Workbench from my local PC i can't connect to the MySQL-Server. In fact i am not sure which credentials i need to provide to connect. I tried it with the public ip of the VM and the root password of the MySQL-Server. But shouldn't i somewhere also provide the password and user for the VM itself?

Upvotes: 1

Views: 4946

Answers (2)

Moslem Ben Dhaou
Moslem Ben Dhaou

Reputation: 7005

Other than the firewall on the actual VM, Azure manage ports security using a separate VPC Network security group. By default, the Network group associated with your VM does not allow inbound TCP connections on port 3306. You have to add that manually:

  1. Go to Network interfaces

Pic 1

  1. Select your network interface

enter image description here

  1. Select Network security group

enter image description here

  1. Select your group (usually only one here)

enter image description here

  1. Select Inbound security rules

enter image description here

  1. Click Add and Fill as follow

enter image description here

  1. Finally apply the changes and wait for 1-2 minutes (time for the new settings to propagate correctly, although it should be instantly)

enter image description here

Magic!

Upvotes: 2

michaelbahr
michaelbahr

Reputation: 4953

Alternative to directly using official MySQL images

I also had troubles getting the official MySQL image running and accessible. sameersbn/docker-mysql provides an nice and easy wrapper that lets you specify user and database to be set up.

My docker-compose file has the following section:

db:
  image: sameersbn/mysql:latest
  volumes:
    - /opt/mysql/data:/var/lib/mysql
  environment:
    - DB_USER=${DB_USER}
    - DB_PASS=${DB_PASS}
    - DB_NAME=theDatabaseName
  ports:
    - "3306:3306"

The details:

  • image - The above mentioned wrapper.
  • volumes - Mapping the data on the persistent host.
  • environment - Variables that define the database setup. Either give plain text as with DB_NAME or access environment variables through the ${...} syntax.
  • ports - Map out the port 3306 on the host so it may be accessed remotely.

Upvotes: 0

Related Questions