Docker container is running without port mapping

I created a docker-compose file to build image from dockerfile and then run container this is my code:

Dockerfile

FROM anapsix/alpine-java

VOLUME [ "/var/run/jars/" ]

ADD hello-world.jar /var/run/jars/

EXPOSE 8080
ENTRYPOINT [ "java" ]
CMD ["-?"]

docker-compose.yml

version: '3'
services:
  hello-world-image:
   build: .
   image: hello-world-image    
  hello-world:
   image: hello-world-image
   container_name: hello-world
   ports:
    - "8080:8080"
   volumes:
     - ./logs_ACM:/root/logs_ACM
   command: -jar /var/run/jars/hello-world.jar
   restart: always

docker ps output:

CONTAINER ID   IMAGE               COMMAND                  CREATED         STATUS                                  PORTS     NAMES
103b0a3c30e3   hello-world-image   "java -jar /var/run/…"   5 seconds ago   Restarting (1) Less than a second ago             hello-world

When i check running containers with “docker ps” the port column is empty therefore no port mapping was done even though i specified ports in my docker compose file.
What changes needed to be done on my docker-compose file to solve this issue ?

Hi

what you’re seeing is the “running” container, is your build container, but its not the right way to do it, you should skip the “build” container and instead move the build part, to your main container, like:

version: '3'
services:
  hello-world:
   image: hello-world-image
   build: .
   container_name: hello-world
   ports:
    - "8080:8080"
   volumes:
     - ./logs_ACM:/root/logs_ACM
   command: -jar /var/run/jars/hello-world.jar
   restart: always

And if that fails to run, you need to check the logs for the container to see what its complaning about

check my stackoverflow thread for recent changes Docker container is running without port mapping - Stack Overflow

No sorry, im not going to help you across 2 threads

new version of dockerfile and docker-compose :

FROM anapsix/alpine-java

USER root

RUN mkdir -p /var/run/jars/

COPY spring-petclinic-2.4.2.jar /var/run/jars/

EXPOSE 8081

ENTRYPOINT [ "java" ]

CMD ["-?"]






version: '3'  # '3' means '3.0'
services:
spring-petclinic:
 build: .
# Only if you're planning to `docker-compose push`
# image: registry.example.com/name/hello-world-image:${TAG:-latest}
 ports:
  - "8081:8081"
 volumes:
 # A bind-mount directory to read out log files is a good use of
 # `volumes:`.  This does not require special setup in the Dockerfile.
  - ./logs_ACM:/root/logs_ACM
 command: -jar /var/run/jars/spring-petclinic-2.4.2.jar
mysql:
 image: mysql:5.7
 ports:
  - "3306:3306"
 environment:
  - MYSQL_ROOT_PASSWORD=
  - MYSQL_ALLOW_EMPTY_PASSWORD=true
  - MYSQL_USER=petclinic
  - MYSQL_PASSWORD=petclinic
  - MYSQL_DATABASE=petclinic
 volumes:
  - "./conf.d:/etc/mysql/conf.d:ro"