Docker Community Forums

Share and learn in the Docker community.

Container not mapped with port

I am trying to create two containers with docker dompose.
This is my Dockerfile:

FROM anapsix/alpine-java


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

and this is my docker-compose:

portainer:
    image: portainer/portainer
    ports:
      - "9000:9000"
    command: -H unix:///var/run/docker.sock
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - portainer_data:/data
    restart: always

jenkins_api:
 image: jenkins_api_image
 container_name: jenkins_api
 ports:
 - "8081:8081"
 volumes:
 - ./logs:/root/logs
 command: -jar jenkins_api.jar
 restart: always

and these are the linux commands to do so :

sudo -S docker ps -qa | xargs docker stop
sudo docker ps -aq | xargs docker rm 
sudo docker images -q | grep -v c45785c254c5 |  grep -v 580c0e4e98b0 | xargs docker rmi 
sudo docker volume ls | xargs docker volume rm 
cd jenkins_api
sudo docker build -t jenkins_api_image .
docker-compose up -d

The portainer container is created with " 9000:9000 9000:9000" as published ports but the jenkins_api container is not created with 8081 port.
Can this be due to problem with jar or something is wrong with my docker config ?

When you say that the jenkins_api container is not created with 8081 port, do you mean it shows up in docker ps but without the port exposed? Or do you mean when you try to reach the port it doesn’t work in some fashion? Can you be more specific?

Can you post any logs that happen after docker-compose up -d? Can you also post docker ps -a and docker logs jenkins_api?

I did a test with this docker-compose.yml

portainer:
  image: portainer/portainer
  ports:
    - "9000:9000"
  command: -H unix:///var/run/docker.sock
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - portainer_data:/data
  restart: always

jenkins_api:
 image: alpine # jenkins_api_image
 container_name: jenkins_api
 ports:
 - "8081:8081"
 volumes:
 - ./logs:/root/logs
 command: sleep 99999 #-jar jenkins_api.jar
 restart: always

and it resulted in everything working from what I could tell

⛵  docker-desktop in ~ on ☁️  default
❯ docker-compose up -d
Creating alexhokanson_portainer_1 ... done
Creating jenkins_api              ... done

⛵  docker-desktop in ~ on ☁️  default
❯ docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                                       NAMES
d921bd4365b3   alpine                "sleep 99999"            6 seconds ago   Up 2 seconds   0.0.0.0:8081->8081/tcp, :::8081->8081/tcp   jenkins_api
ce27fc8afbef   portainer/portainer   "/portainer -H unix:…"   6 seconds ago   Up 3 seconds   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp   alexhokanson_portainer_1

⛵  docker-desktop in ~ on ☁️  default
❯ # both containers are running and both have the expected exposed ports

⛵  docker-desktop in ~ on ☁️  default
❯ docker logs jenkins_api

⛵  docker-desktop in ~ on ☁️  default
❯ # I didn't expect logs since we're only doing a sleep command
1 Like

it shows up in docker ps but without the port exposed:
-docker ps

3458c98ec242   jenkins_api_image     "java -jar jenkins_a…"   4 hours ago   Restarting (1) 55 seconds ago                                               jenkins_api

-“docker logs jenkins_api” gave me “Unable to access jarfile jenkins_api.jar”

It looks like the port is not being exposed because the container is stuck in a “boot loop”. e.g. it is always restarting, so it never becomes healthy and allows Docker to expose a port on it.

I have an example here. I created a container like this: docker run --entrypoint=bash --restart=always -p 8080:8080 -d ubuntu -c 'sleep 5 && exit 1'

Then I kept checking docker ps and here is the output over the next 5 seconds (the sleep time)

❯ docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS         PORTS                                       NAMES
ee268f3441ef   ubuntu                          "bash -c 'sleep 5 &&…"   4 seconds ago   Up 2 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   nifty_ellis

❯ docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS         PORTS                                       NAMES
ee268f3441ef   ubuntu                          "bash -c 'sleep 5 &&…"   5 seconds ago   Up 3 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   nifty_ellis

❯ docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS         PORTS                                       NAMES
ee268f3441ef   ubuntu                          "bash -c 'sleep 5 &&…"   6 seconds ago   Up 4 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   nifty_ellis

❯ docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS         PORTS                                       NAMES
ee268f3441ef   ubuntu                          "bash -c 'sleep 5 &&…"   7 seconds ago   Up 5 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   nifty_ellis

❯ docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS                        PORTS     NAMES
ee268f3441ef   ubuntu                          "bash -c 'sleep 5 &&…"   8 seconds ago   Restarting (1) 1 second ago             nifty_ellis

❯ docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS                         PORTS     NAMES
ee268f3441ef   ubuntu                          "bash -c 'sleep 5 &&…"   9 seconds ago   Restarting (1) 2 seconds ago             nifty_ellis

❯ docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED          STATUS                  PORTS                                       NAMES
ee268f3441ef   ubuntu                          "bash -c 'sleep 5 &&…"   10 seconds ago   Up Less than a second   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   nifty_ellis

Notice how upon restart it doesn’t show the ports and then they come back after the pod has been up for a bit. I recommend fixing the restart issue and then seeing if the ports stay bound to the container.

1 Like

yes it got stuck on boot loop and it was caused by the jar file running command.I think the problem is with the location of the jar being in the same level of the dockerfile and docker-compose i changed it to be :

FROM anapsix/alpine-java

USER root

RUN mkdir -p /var/run/jars/

COPY jenkins_api.jar /var/run/jars/

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

it worked !

1 Like