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 configure docker containers so that I can access them outside the host
  • Kitematic docker config yml
  • Why is my node child_process terminating early?
  • How to setup continuous integration - deployment: bitbucket, drone.io, docker hub, swarm?
  • How to copy files from shared directory in multiple Dockerfile?
  • Airbnb Airflow using all system resources
  • 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?

  • How to show the run command of a docker container
  • start apache in start container in windows docker-compose system
  • Docker: Direct subdomains to specific containers
  • Local-to-remote replication times out or replicates only few documents CouchDb
  • System.InvalidOperationException: Unable to resolve project 'HelloMvc6' from /ap p/src/HelloMvc6
  • docker-compose: Modify environment variable
  • 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.