Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose won't keep base container running

I have a Dockerfile which builds a Debian image and installs Apache, Php, MySql.

In general, if I run docker commands:

docker build --tag my-project-image:2.0 .

And then

docker run -dit --name my-project-container \ 
    --mount type=bind,source=$(pwd),destination=/var/www/html \
    -p 80:80 --rm my-project-image:2.0

My container stays running and all the services I need for the development are up and running, so no issue with that.

I tried to move this config into a docker-compose but faced odd trouble. I believe I am making a very common mistake but I searched a lot tried lots of things but can't make it work.

To cut it short, when I run docker-compose up my image is built, it creates a container, runs all scripts and it exists instantly. The problem is that I want to keep that container running since I have a service like Apache and MySQL there.

I have two services one is lamp for apache, MySQL, and PHP and the other one is node just to run npm scripts.

node container stays up and running but lamp closes instantly, unfortunately.

This is my Dockerfile

FROM debian:latest
ENV DOC_ROOT=/var/www/html
WORKDIR ${DOC_ROOT}
RUN apt-get update
RUN apt-get --assume-yes upgrade
RUN apt-get --assume-yes install apache2
RUN apt-get --assume-yes install curl php-curl
RUN apt-get --assume-yes install php
RUN apt-get --assume-yes install php-mysql
RUN apt-get --assume-yes install composer
RUN apt-get --assume-yes install php-xdebug
RUN apt-get --assume-yes install default-mysql-server
RUN a2enmod rewrite
COPY ./ ${DOC_ROOT}
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
COPY config/000-default.conf /etc/apache2/sites-available/

EXPOSE 80

Here is my docker-compose.yaml file content

version: "3.2"
services:
  lamp:
    container_name: lamp-stack
    build: .
    ports:
      - 80:80
    volumes:
    - .:/var/www/html
    command: >
      /bin/sh -c "service apache2 start && \
        service mysql start && \
        mysql < migrations/migrations.sql && \
        mysql < migrations/development.sql && \
        bash"
  node:
    container_name: node-builder
    image: node:12-alpine3.9
    depends_on:
      - lamp
    working_dir: /var/www/html
    volumes:
    - .:/var/www/html
    command: >
      /bin/ash -c "npm run build:dev > ./.logs/npm/npm-build.log && \
        npm run watch:sass > ./.logs/npm/sass-watch.log"


This is the output while I run docker-compose up

enter image description here

I have included bash as the last command in the end but it seems it is not working in docker-compose as I thought it supposed to. It keeps my container running only when I am not using docker-compose command :( .

Any ideas what I might be doing wrong?

like image 286
n1md7 Avatar asked Nov 29 '25 08:11

n1md7


1 Answers

Try to add those lines in your docker-compose:

stdin_open: true # equivalent of -i
tty: true        # equivalent of -t
like image 85
flo flo Avatar answered Dec 01 '25 22:12

flo flo