[SOLVED] Container exits after docker-compose up -d

Hello everyone,

if i’m running docker-compose up -d in the folder with my docker-compose.yml, my container is not throwing any error but the state is Exited (0) a minute ago. If I start the Container bydocker run -dit <container-name> command every thing works just great.

What am I doing wrong?

My dockerfile

FROM ubuntu:16.04

ENV HAPROXY_VERSION=1.8

RUN apt-get update 
RUN apt-get upgrade -y
RUN apt-get install -y software-properties-common
RUN add-apt-repository -y ppa:vbernat/haproxy-$HAPROXY_VERSION && add-apt-repository -y ppa:certbot/certbot && apt-get update
RUN apt-get install -y haproxy=$HAPROXY_VERSION.\* certbot nano

RUN service haproxy stop
COPY haproxy.cfg /etc/haproxy/haproxy.cfg

ENTRYPOINT ["/bin/bash"]

CMD ["service", "haproxy", "start"]

EXPOSE 80 443

docker-compose.yml

version: '3'

    services:
      haproxy:
        build: ./docker
        container_name: haproxy
        ports:
          - "80:80"
          - "443:443"
      - "17001:17001"
    #restart: always
    volumes:
      - "haproxy_config:/etc/haproxy/" 
    networks:
      - haproxy_net

volumes:
  haproxy_config:

networks:
  haproxy_net:
    external: true

Many Thanks and greetings

Hi

its because the service command sends haproxy to the background, and when the commmand is done, the container stops, because it completed the task.

So what you want to do, is instead of calling “service haproxy start”, is to run the haproxy executeable.

The official haproxy docker image, runs haproxy like this:

haproxy -c -f /etc/haproxy/haproxy.cfg

But at a side note, im not sure that the “service” command at all will work.

Hi

thank you for your Answer. This makes sens!

I changed the dockerfile to:

....
ENTRYPOINT ["/bin/bash"]

CMD ["haproxy","-f","/etc/haproxy/haproxy.cfg","-p","/run/haproxy.pid"]

EXPOSE 80 443

but now the container exits with:

Successfully built 971fb9001e8f
Successfully tagged haproxy_haproxy:latest
Recreating haproxy_haproxy_1 ... done
Attaching to haproxy_haproxy_1
haproxy_1  | /usr/sbin/haproxy: /usr/sbin/haproxy: cannot execute binary file
haproxy_haproxy_1 exited with code 126

Hi again.

remove the “ENTRYPOINT [”/bin/bash"]" from your dockerfile.

When you define the entrypoint, all commands after will be arguments, in this case to your bash.
read more in detail here: https://docs.docker.com/engine/reference/builder/#entrypoint

Hi

if I do this, the Container is in “Exited” state again.

CONTAINER ID           3fb042f45ac9
IMAGE                  haproxy_haproxy
COMMAND                "haproxy -f /etc/hap…"
CREATED                34 seconds ago
STATUS                 Exited (0) 33 seconds ago
PORTS  
NAMES                  haproxy_haproxy_1

Do i have to start the service in an entrypoint.sh script?

Try and start it with:

docker run -ti IMAGE

Oh i just noticed, why do you mount nothing in to /etc/haproxy ?
Then there will be no haproxy.cfg, your host is always the master when doing volumes

In the Dockerfile I copy the haproxy.cfg to /etc/haproxy/ and I see on the dockerhost the correct haproxy.cfg

xxx@docker:/docker-compose-files/haproxy$ sudo ls -l /var/lib/docker/volumes/haproxy_config/_data
insgesamt 8
drwxr-xr-x 2 root root 4096 Aug 21 09:05 errors
-rw-r--r-- 1 root root 2024 Aug 21 09:30 haproxy.cfg

Hi

many thanks terpz for your help. I’ve found the solution!

with:
CMD ["haproxy","-f","/etc/haproxy/haproxy.cfg","-p","/run/haproxy.pid"]

the haproxy service was running in background mode. With the -db param (disable background mode) everything is running fine!

CMD ["haproxy","-f","/etc/haproxy/haproxy.cfg","-p","/run/haproxy.pid","-db"]

Many thanks and greetings

1 Like