Docker Community Forums

Share and learn in the Docker community.

Run docker exec in compose file


(Offerlam) #1

Hi all,

Im trying to make a docker-compose file which runs librenms and mysql.

currently my compose file looks like this:

version: '3.3'
services:
  librenms:
    image: jarischaefer/docker-librenms
    restart: always
    environment:
      - APP_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      - DB_HOST=172.16.98.10
      - DB_NAME=librenms
      - DB_USER=librenms
      - DB_PASS=XXXXXXXXXXXXXXXXXXXXX
      - BASE_URL=http://172.16.98.10
      - ENABLE_SYSLOG=True
    volumes:
      - /docker/containers/librenms/logs/:/opt/librenms/logs
    ports:
      - "80:80"
      - "514:514"
      - "514:514/udp"
    depends_on:
      - db

  db:
    image: mysql:latest
    restart: always
    environment:
      - MYSQL_DATABASE=librenms
      - MYSQL_USER=librenms
      - MYSQL_PASSWORD=XXXXXXXXXXXXXXXXXXX
      - MYSQL_ROOT_PASSWORD=XXXXXXXXXXXXXXXXXXXXXXXXXX
    ports:
      - "3306:3306"
    expose:
      - "3306"
    volumes:
      - /docker/containers/mysql/db/:/var/lib/mysql

The composer runs fine. My only problem is when i log in to the website it gives me “Unhandled MYSQL error [2054]”

When i read the documentation for librenms found here:

it says i need to run:
Creating the tables
docker exec librenms setup_database
Creating an initial admin user
docker exec librenms create_admin

So my question is how best to do this? if it can be done at all?
Can i somehow automate a docker exec in my compose file or?
THANKS!

Casper


(Gary Forghetti) #2

Hi.

I took a look for you.

I pulled the jarischaefer/docker-librenms:latest docker image

🐳  gforghetti:[~] $ docker image pull jarischaefer/docker-librenms:latest
latest: Pulling from jarischaefer/docker-librenms
aee2c84f2e19: Pull complete
5ada4eb72e49: Pull complete
3618d14c5656: Pull complete
efff3e3f4d7b: Pull complete
95120f088738: Pull complete
afb74362c78e: Pull complete
15a8379f86de: Pull complete
1fa135a841b2: Pull complete
Digest: sha256:5288f5bcf281dac6f236def6a34aacfce4789dfebbd9f41e65dedf6f87256dcd
Status: Downloaded newer image for jarischaefer/docker-librenms:latest

Inspected it to get the Entrypoint and Cmd that runs when you start a container from that image.

🐳  gforghetti:[~] $ docker image inspect jarischaefer/docker-librenms:latest | jq -r '.[] | .Config.Cmd, .Config.Entrypoint'
[
  "/sbin/my_init"
]
null

No Entrypoint. The Cmd is /sbin/my_init.
So when a container runs from that image, the /sbin/my_init command/script runs.

Now I brought up a container from that image and got a shell prompt. I then searched for the create_admin and setup_database commands/scripts. They are both in /usr/local/bin

🐳  gforghetti:[~] $ docker container run -it --name test jarischaefer/docker-librenms:latest bash
root@3d946c2b0af1:/# find / -name create_admin
/usr/local/bin/create_admin
root@3d946c2b0af1:/# find / -name setup_database
/usr/local/bin/setup_database
root@3d946c2b0af1:/#

So what you can do is build your own customer image from the jarischaefer/docker-librenms:latest docker image and then replace the jarischaefer/docker-librenms:latest docker image in your docker-compose.yml with your custom image.

Create a Dockerfile, the FROM statement specifies thejarischaefer/docker-librenms:latestdocker image. The code aCMDstatement in theDockerfileto run all three commands:/sbin/my_init && /usr/local/bin/setup_database && /usr/local/bin/create_admin`

Now I did not do it myself. Not sure if those last 2 commands/scripts “prompt” or accept command arguments.
You will have to run them manually and see what they do.

So what you should do first is run your docker-compose as it is. Once the stack comes up run a docker container exec command into the librenms container and bring up a shell prompt (just like I did previously).
Then run those 2 commands manually and see what happens.

/usr/local/bin/setup_database
/usr/local/bin/create_admin