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

  • How to copy a folder from docker to other folder?
  • Permission denied when running a container (docker 1.12.5)
  • Can I build Docker contains for a Raspberry Pi on a AMD64 machine?
  • Migrate WordPress data to Docker Container?
  • Confusion while deploying docker-composer image
  • how to bring an instance from one subnet to the other which is having a docker containers running in that instance?
  • 5621fc9b438d mysql/mysql-server:latest “/entrypoint.sh 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

     @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost/mydb");
        dataSource.setUsername("mysql-db");
        dataSource.setPassword("root");
        return dataSource;
    }
    

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

  • docker repository name component must match
  • chown in docker not changing user to root
  • How to use ipaddreses instead of container names in docker compse networking
  • Where are docker images stored by boot2docker?
  • How to connect external mysql server in docker container
  • Reverse proxy nginx and Express server returning 502
  • 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.