0

I am using this docker-compose file:

    version: '3.8'

# Services
services:

  # Nginx Service
  nginx:
    image: nginx:1.21
    ports:
      - 80:80
    volumes:
      - ./src:/var/www/php
      - ./.docker/nginx/conf.d:/etc/nginx/conf.d
    depends_on:
      - php

  # PHP Service
  php:
    build: ./.docker/php
    working_dir: /var/www/php
    volumes:
      - ./src:/var/www/php
    command: /bin/bash -c "./install.sh"
    depends_on:
      mysql:
        condition: service_healthy

  # MySQL Service
  mysql:
    image: mysql/mysql-server:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ROOT_HOST: "%"
      MYSQL_DATABASE: demo
    volumes:
      - ./.docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
      - mysqldata:/var/lib/mysql
    healthcheck:
      test: mysqladmin ping -h 127.0.0.1 -u root --password=$$MYSQL_ROOT_PASSWORD
      interval: 2s
      retries: 10

  # PhpMyAdmin Service
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:5
    ports:
      - 8080:80
    environment:
      PMA_HOST: mysql
    depends_on:
      mysql:
        condition: service_healthy
# Volumes
volumes:

  mysqldata:    

I am trying to run a bash script (install.sh) after the container is created to run apt-get update install wget etc, but the php container fails when I try to run it. My bash script is:

#!/bin/bash
mkdir testdir && apt-get update && apt-get install wget -y

(this file is here: ./src/install.sh)

It creates the folder correctly and the logs suggest it is trying to install wget (but never seems to finish) but the container never starts correctly.

If I remove the command: /bin/bash -c "./install.sh" line everything works correctly (but wget is not installed).

I have tried moving the command to a Dockerfile as a RUN command but it never seems to run

Any ideas why this is happening?

Thanks

sulman
  • 2,431
  • 7
  • 40
  • 62
  • 4
    The command in your docker-compose file replaces any `CMD` you might have in the Dockerfile for the image, so after installing wget, there's nothing more to do and the container stops. – Hans Kilian Jun 08 '22 at 15:38
  • If you would like scripts AND commands to run on container start I'd suggest creating and entrypoint script and at the bottom of the script, include the command to start your container services. – vaughngx4 Jun 08 '22 at 16:35

1 Answers1

0

As Hans Kilian said in the comments, docker-compose commands replace anything set by CMD or ENTRYPOINT. These commands are necessary for the container to function, and thus it never does anything more than installing wget.

You appear to be trying to run a file located under "./install.sh," which is not an absolute path. Try running the command using the absolute path of the file, as dockerfiles do not, in my experience, recognize changing directory after each command, so:

RUN cd /xyz
RUN /bin/bash -c "./install.sh"

does not have the same result as

RUN /bin/bash -c "/xyz/install.sh"

(where /xyz is the directory where install.sh is located)

Additionally, make sure the file is marked as executable with chmod when it is copied into your container.

However, if all you desire to do is create a directory and install wget, I would simply do this in the Dockerfile:

RUN mkdir testdir
RUN apt-get update && apt-get install -y wget
Paradoc
  • 181
  • 1
  • 6
  • 1
    (`RUN` and `CMD` will automatically add a `sh -c` wrapper if their command is not a JSON array, so you can just `RUN /xyz/install.sh`; you never need to `RUN bash -c '...'`.) – David Maze Jun 08 '22 at 17:26