dockerdocker-composedockerfileyii2-basic-app

How to export Yii2 migrations into docker container


I have successfully containerized my basic Yii2 application with docker and it runs on localhost:8000. However, I cannot use the app effectively as most of its data are stored in migration files. Is there a way I could export the migrations into docker after running it? (or during execution)

This is my docker compose file

version: '2'
services:
  php:
    image: yiisoftware/yii2-php:7.1-apache
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ./:/app:delegated
    ports:
      - '8000:80'
    networks:
      - my-network
  db:
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_DATABASE=my-db
      - MYSQL_PASSWORD=password
      - MYSQL_ROOT_PASSWORD=password
    ports:
      - '3306:3306'
    expose:
      - '3306'
    volumes:
      - mydb:/var/lib/mysql
    networks:
     - my-network
  memcached:
    container_name: memcached
    image: memcached:latest
    ports:
        - "0.0.0.0:11211:11211"
volumes:
  restatdb:
networks:
  my-network:
    driver: bridge

and my Dockerfile

FROM alpine:3.4

ADD . /

COPY ./config/web.php ./config/web.php

COPY . /var/www/html

# Let docker create a volume for the session dir.
# This keeps the session files even if the container is rebuilt.
VOLUME /var/www/html/var/sessions


Solution

  • It is possible to run yii commands in docker. First let the yii2 container run in the background or another tab of the terminal. The yii commands can be run using the docker exec on the interactive interface which would let us interact with the running container

    sudo docker exec -i <container-ID> php yii migrate/up

    You can get the container ID using

    sudo docker ps