access my docker mysql container in java class

I’m leaning docker and trying to connect a db in my java application.

I already create the container and started it

  • docker-compose scale does not work with haproxy
  • Fedora 22: ERROR: No module named '_rpmb' while building docker
  • Installing packages for Rstudio Docker
  • Access Docker socket within container
  • get node port from test running in selenium docker hub
  • oh-my-zsh installation returns non zero code
  • 5621fc9b438d mysql/mysql-server:latest “/ mysq About an hour ago Up About an hour 3306/tcp mysql-db

    I also accessed the container and create one database by bash. I’m having problem to connect the db on Java class.

    My Java method

    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        return dataSource;

    How can I connect and use my docker mysql database on Java application?

  • Installing elasticsearch docker image fails on command not found
  • Overriding default umask for docker daemon
  • Elastic Beanstalk with multiple environments and URL routing between them
  • docker-compose env_file available in script but not directy in run command
  • How to start Docker for Mac daemon on boot?
  • Why https is forced?
  • One Solution collect form web for “access my docker mysql container in java class”

    Make sure you have open your port 3306 to the outside with -p 3306:3306 (not just -p 3306). By default, only linked container will be able to access your mysql port.

    Finally, if you are running Docker with a docker-machine, you will need to speficy the ip generated for the Docker VM rather than localhost.

    Good luck.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.