How do I attach VisualVM to a simple Java process running in a Docker container

Actually I wanted a solution working for JEE containers, specifically for Glassfish, but after I tried many combinations of settings and did not succeed, I reduced the setup to the simplest possible case.

Here is my Hello World daemon started in a Docker container. I want to attach jconsole or VisulaVM to it. Everything is on the same machine.

  • could not read Username for '': No such device or address
  • How to set busybox vi auto wrap when a line too long?
  • How to find Docker container's layer ID and reuse it in another container
  • Docker PHP permissions
  • Add docker host into docker network
  • Download docker container from cloud9 service
  • public class Main {
      public static void main(String[] args) {
        while (true) {
          try {
            System.out.println("Hello, World");
          } catch (InterruptedException e) {


    FROM java:8
    COPY . /usr/src/myapp
    WORKDIR /usr/src/myapp
    RUN javac
    CMD ["java", "Main"]

    Building: docker build -t hello-world-daemon .

    Running: docker run -it --rm --name hwd hello-world-daemon


    • what JVM parameters should be added to CMD command line?
    • what ports should be exposed and published?
    • what network mode should Docker container be using?

    I do not show my failed attempts here so that correct answers will not be biased. This should be a pretty common problem, yet I could not find a working solution.

    Update. Worked solution

    This Dockerfile works

    FROM java:8
    COPY . /usr/src/myapp
    WORKDIR /usr/src/myapp
    RUN javac
    CMD ["java", \
    "", \
    "", \
    "", \
    "", \
    "", "Main"]
    EXPOSE 9010

    in combination with the docker run command

    docker run -it --rm --name hwd -p 9010:9010 hello-world-daemon

    VisualVM connects via right click Local->Add JMX Connection, and then entering localhost:9010, or through adding a remote host.

    JConsole connects via selecting a Remote process with localhost:9010.

    When defining the connection as remote, any interface listed by ifconfig can be used. For instance, docker0 interface with address works. The container’s address works too.

  • Docker, How to replace capistrano tasks in docker
  • Is it possible to dockerize Internet Explorer?
  • Docker CD workflow - making docker hosts pull new images and deploy them
  • Remove existing image when using `docker commit`?
  • Is it a docker best practice to use volume for the code?
  • ELK Stack using Docker on EC2 Container Service
  • 2 Solutions collect form web for “How do I attach VisualVM to a simple Java process running in a Docker container”

    At first you should run you application with these JVM params:

    Then you should expose port for docker:

    EXPOSE 9010

    Also specify port binding with docker run command:

    docker run -p 9010:9010 -it --rm --name hwd hello-world-daemon

    After that you can connect with Jconsole to local 9010 port and manage application run in Docker.

    I followed an other SO response to a similar question and it worked.

    I started my Java process inside the container by adding those JVM params:<port> \ \ \<port> \

    and started the Docker container specifying -e HOST_HOSTNAME=$HOSTNAME -p <port> to the docker run command.

    Then I’ve been able to access to this remote Java app from my local JVisualVm by adding a remote JMX connection (“File” > “Add a JMX Connection…”) and specifying <dockerhostname>:<port> in the “Connection” input, and checking “Do not require SSL connection”.

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