skip to Main Content

I have problem with running db migration on when container is up.

Problems:

  • cant set app key because gitlab-ci didn’t copy .env file (getting err in gitlab ci console), so setting key needs to happen later
  • running migration with wait-for-it because container exits with success code 0 (migrations is up)

I will put code only for my db and web container.

db:
    container_name: db
    image: mysql:5.7.22
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_PASSWORD: ${DB_PASSWORD}
    ports:
      - 3306:3306
    volumes:
      - mysql-data:/var/lib/mysql
    networks:
      - my-network

backend:
    image: registry image
    container_name: "backend"
    build:
      context: ./backend
      dockerfile: Dockerfile
    depends_on:
      - db
    ports:
      - 1020:80
    networks:
      - my-network

gitlab-ci:

build-backend:
  tags:
    - vps
  variables:
    GIT_CLEAN_FLAGS: none
  stage: dockerize
  image: docker:latest
  services:
    - docker:dind
  dependencies: []
  script:
    - docker build -t backend backend
    - cp .env ./backend/.env
    - cd backend
    - docker build -t $CI_REGISTRY_IMAGE/backend:$CI_COMMIT_BRANCH .
    - docker tag $CI_REGISTRY_IMAGE/backend:$CI_COMMIT_BRANCH $CI_REGISTRY_IMAGE/frontend:$CI_COMMIT_REF_NAME
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker push $CI_REGISTRY_IMAGE/backend:$CI_COMMIT_REF_NAME

…deploying code

I’m using https://github.com/vishnubob/wait-for-it

Dockerfile:

FROM webdevops/php-nginx:7.4-alpine

# Install Laravel framework system requirements (https://laravel.com/docs/8.x/deployment#optimizing-configuration-loading)
RUN apk add oniguruma-dev postgresql-dev libxml2-dev
RUN docker-php-ext-install 
        bcmath 
        ctype 
        fileinfo 
        json 
        mbstring 
        pdo_mysql 
        pdo_pgsql 
        tokenizer 
        xml

# Copy Composer binary from the Composer official Docker image
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
ENV WEB_DOCUMENT_ROOT /var/www/html/public
ENV APP_ENV production
WORKDIR /var/www/html
COPY . .
RUN composer install --no-interaction --optimize-autoloader

RUN chown -R root:root .

RUN chmod -R ugo+rw storage

RUN chmod 777 wait-for-it.sh
RUN chmod 777 migrate.sh
EXPOSE 80
CMD ["./wait-for-it.sh", "db:3306", "--", "./migrate.sh"]

migrate.sh:

#!/bin/sh

php artisan key:generate
# Optimizing Configuration loading
php artisan config:cache
# Optimizing Route loading
php artisan route:cache
# Optimizing View loading
php artisan view:cache
echo "finished cashes"
php artisan migrate --force &
exec "$@"

So how can I solve exit code 0, meant to say how to prevent container from stopping?
Thanks

2

Answers


  1. Chosen as BEST ANSWER

    Solution is to use Supervisor which is useful for having all the jobs in background and wont close your container while running migrations. I can't believe that this configuration from this repo is working perfectly it saves my time. I spent more than 7 days in searching for the best solution, but this guy who posted this saves me!

    Refer to this repo please https://github.com/harshalone/laravel-9-production-ready

    I didn't have to change a line of code, hope you don't have too. Simply works!


  2. for anyone struggling with running migrations but your database isn’t up before your Laravel application, just want to mention that I’ve used wait-for-it script and changed last line of code in my Dockerfile like this:

    CMD ["/var/www/docker/wait-for-it.sh", "db:3306", "--", "/var/www/docker/run.sh"]
    

    So now my migrations will first wait for database to be up and running.
    Just put wait-for-it.sh inside of your docker folder or use it from github directly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search