Docker-Compose not able to copy haproxy.cfg
Asked Answered
W

2

12

My problem is that I have a docker-compose.yml file and an haproxy.cfg file and I want docker-compose to copy the haproxy.cfg file to the docker container. As per the post Docker composer copy files I can use volumes to do it but in my case I'm getting the below error. Can anybody help me achieve this.

Below is the code and everything

docker-compose.yml

version: "3.3"
services:
 ###After all services are up, we are initializing the gateway
 gateway:
  container_name: gateway-haproxy
  image: haproxy
  volumes:
    - .:/usr/local/etc/haproxy
  ports:
   - 80:80
  network_mode: "host"

Folder Structure

enter image description here

Command output

root@ubuntu:/home/karunesh/Desktop/Stuff/SelfStudy/DevOps/docker# docker-compose up
Creating gateway-haproxy ... 
Creating gateway-haproxy ... done
Attaching to gateway-haproxy
gateway-haproxy | <7>haproxy-systemd-wrapper: executing /usr/local/sbin/haproxy -p /run/haproxy.pid -f /usr/local/etc/haproxy/haproxy.cfg -Ds 
gateway-haproxy | [ALERT] 219/163305 (6) : [/usr/local/sbin/haproxy.main()] No enabled listener found (check for 'bind' directives) ! Exiting.
gateway-haproxy | <5>haproxy-systemd-wrapper: exit, haproxy RC=1
gateway-haproxy exited with code 1
Woundwort answered 8/8, 2017 at 16:44 Comment(4)
You cant copy a file into a container using docker-compose. This has to be done inside of the Dockerfile itselfJustinajustine
I dont have the file...i am supposed to use official haproxy image...is there any way I can do it...Woundwort
@Justinajustine I've done it before, see my answer belowLurleen
@Lurleen yeah, wasn't 100% sure of what he wanted because this is only useful when you mount the volume. If he ever wanted to work outside of his environment it would be different if he wanted to build.Justinajustine
L
15

Try this:

volumes:
  - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro

Instead of mounting the whole directory, this will only mount haproxy.cfg. The ro is an abbreviation for read-only, and its usage guarantees the container won't modify it after it gets mounted.

Lurleen answered 8/8, 2017 at 17:51 Comment(0)
J
6

In order to add additional files to the container, you have to build on top of the existing image from haproxy.

For example, your Dockerfile should look like this:

FROM haproxy:latest
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg

Then you can update your docker compose file accordingly.

If you plan on using this for local development, just mount the file(s), see @MatTheWhale's answer

See more at the official haproxy Docker page

Justinajustine answered 8/8, 2017 at 17:17 Comment(1)
Thanks a lot for this...but is there anyway that we can do it without using the docker file...let us assume I have the docker image present and I have made some changes in the haproxy.cfg. Instead of updating an image can I not directly copy the local haproxy.cfg to the location in the image...Woundwort

© 2022 - 2024 — McMap. All rights reserved.