Docker service laravel app not 运行 when not 运行 in manager node
Docker service laravel app not running when not running in manager node
docker-compose.yml
这是我的 docker-compose 文件,用于使用 docker-堆栈在多个实例中部署服务。如您所见,app 服务是 2 个节点中的 laravel 运行ning 和其中一个节点中的数据库 (mysql)。
完整代码库:
https://github.com/taragurung/Ci-CD-docker-swarm
version: '3.4'
networks:
smstake:
ipam:
config:
- subnet: 10.0.10.0/24
services:
db:
image: mysql:5.7
networks:
- smstake
ports:
- "3306"
env_file:
- configuration.env
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASSWORD}
volumes:
- mysql_data:/var/lib/mysql
deploy:
mode: replicated
replicas: 1
app:
image: SMSTAKE_VERSION
ports:
- 8000:80
networks:
- smstake
depends_on:
- db
deploy:
mode: replicated
replicas: 2
我面临的问题
1. 虽然服务处于 运行ning 状态,但当我检查服务日志时,我可以看到只有一个节点的迁移成功,而不是 运行宁在另一个节点。 查看下面的日志
- 当我仅在 管理器节点 中创建 app 服务 运行 时,应用程序运行良好。我可以登录页面并执行所有操作,但当我在任何节点中使用副本创建应用程序服务 运行 时,登录页面会显示,但当尝试登录时,它会重定向到 NOT FOUND 页面
这是尝试在 3 个节点上 运行 时的完整日志。下面是在 2 个节点上 运行ning 时的示例。您可以详细查看迁移问题
https://pastebin.com/wqjxSnv2
使用 docker service logs <smstake_app>
检查服务日志
| Cache cleared successfully.
| Configuration cache cleared!
| Dropped all tables successfully.
| Migration table created successfully.
|
| In Connection.php line 664:
|
| SQLSTATE[42S02]: Base table or view not found: 1146 Table 'smstake.migratio
| ns' doesn't exist (SQL: insert into `migrations` (`migration`, `batch`) val
| ues (2014_10_12_100000_create_password_resets_table, 1))
|
|
| In Connection.php line 452:
|
| SQLSTATE[42S02]: Base table or view not found: 1146 Table 'smstake.migratio
| ns' doesn't exist
|
|
| Laravel development server started: <http://0.0.0.0:80>
| PHP 7.1.16 Development Server started at Thu Apr 5 07:02:22 2018
| [Thu Apr 5 07:03:56 2018] 10.255.0.14:53744 [200]: /js/app.js
| Cache cleared successfully.
| Configuration cache cleared!
| Dropped all tables successfully.
| Migration table created successfully.
| Migrating: 2014_10_12_000000_create_users_table
| Migrated: 2014_10_12_000000_create_users_table
| Migrating: 2014_10_12_100000_create_password_resets_table
| Migrated: 2014_10_12_100000_create_password_resets_table
| Migrating: 2018_01_11_235754_create_groups_table
| Migrated: 2018_01_11_235754_create_groups_table
| Migrating: 2018_01_12_085401_create_contacts_table
| Migrated: 2018_01_12_085401_create_contacts_table
| Migrating: 2018_01_12_140105_create_sender_ids_table
| Migrated: 2018_01_12_140105_create_sender_ids_table
| Migrating: 2018_02_06_152623_create_drafts_table
| Migrated: 2018_02_06_152623_create_drafts_table
| Migrating: 2018_02_21_141346_create_sms_table
| Migrated: 2018_02_21_141346_create_sms_table
| Seeding: UserTableSeeder
| Laravel development server started: <http://0.0.0.0:80>
| PHP 7.1.16 Development Server started at Thu Apr 5 07:03:23 2018
| [Thu Apr 5 07:03:56 2018] 10.255.0.14:53742 [200]: /css/app.css
I don't know if its due to migration problem or what. Sometime I can
login and after few time I get redirected to Not found page again when
clicking on the link inside dashboard.
所以我 运行 您的服务并发现了一些问题。
- mysql 中
docker-compose.yml
中的用户不同。这可能只是为了发布目的
- 在您的
Dockerfile
中,您使用了 ENTRYPOINT
,这导致在迁移服务上也对 运行 执行了相同的命令。我改成了CMD
- 您没有 运行
migration
服务与您的 mysql 数据库在同一网络中。所以 mysql
无法从同一个访问。
这是我使用的最终合成文件
docker-compose.yml
version: '3.4'
networks:
smstake:
services:
db:
image: mysql:5.7
networks:
- smstake
ports:
- "3306"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: smstake
MYSQL_USER: tara
MYSQL_PASSWORD: password
volumes:
- mysql_data:/var/lib/mysql
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.role == manager
app:
image: 127.0.0.1:5000/myimage:latest
ports:
- 8000:80
networks:
- smstake
depends_on:
- db
- migration
deploy:
mode: replicated
replicas: 3
migration:
image: 127.0.0.1:5000/myimage:latest
command: sh -xc "sleep 10 && pwd && php artisan migrate:fresh 2>&1"
networks:
- smstake
depends_on:
- db
deploy:
restart_policy:
condition: on-failure
mode: replicated
replicas: 1
placement:
constraints:
- node.role == manager
volumes:
mysql_data:
Dockerfile
FROM alpine
ENV \
APP_DIR="/project" \
APP_PORT="80"
# the "app" directory (relative to Dockerfile) containers your Laravel app...
##COPY app/ $APP_DIR
# or we can make the volume in compose to say use this directory
RUN apk update && \
apk add curl \
php7 \
php7-opcache \
php7-openssl \
php7-pdo \
php7-json \
php7-phar \
php7-dom \
php7-curl \
php7-mbstring \
php7-tokenizer \
php7-xml \
php7-xmlwriter \
php7-session \
php7-ctype \
php7-mysqli \
php7-pdo \
php7-pdo_mysql\
&& rm -rf /var/cache/apk/*
RUN curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/bin --filename=composer
##RUN cd $APP_DIR && composer install
RUN mkdir /apps
COPY ./project /apps
RUN cd /apps && composer install
WORKDIR /apps
RUN chmod -R 775 storage
RUN chmod -R 775 bootstrap
copy ./run.sh /tmp
CMD ["/tmp/run.sh"]
然后再次 运行 服务。然后迁移顺利
而且该应用程序也运行良好
docker-compose.yml 这是我的 docker-compose 文件,用于使用 docker-堆栈在多个实例中部署服务。如您所见,app 服务是 2 个节点中的 laravel 运行ning 和其中一个节点中的数据库 (mysql)。
完整代码库: https://github.com/taragurung/Ci-CD-docker-swarm
version: '3.4'
networks:
smstake:
ipam:
config:
- subnet: 10.0.10.0/24
services:
db:
image: mysql:5.7
networks:
- smstake
ports:
- "3306"
env_file:
- configuration.env
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASSWORD}
volumes:
- mysql_data:/var/lib/mysql
deploy:
mode: replicated
replicas: 1
app:
image: SMSTAKE_VERSION
ports:
- 8000:80
networks:
- smstake
depends_on:
- db
deploy:
mode: replicated
replicas: 2
我面临的问题 1. 虽然服务处于 运行ning 状态,但当我检查服务日志时,我可以看到只有一个节点的迁移成功,而不是 运行宁在另一个节点。 查看下面的日志
- 当我仅在 管理器节点 中创建 app 服务 运行 时,应用程序运行良好。我可以登录页面并执行所有操作,但当我在任何节点中使用副本创建应用程序服务 运行 时,登录页面会显示,但当尝试登录时,它会重定向到 NOT FOUND 页面
这是尝试在 3 个节点上 运行 时的完整日志。下面是在 2 个节点上 运行ning 时的示例。您可以详细查看迁移问题 https://pastebin.com/wqjxSnv2
使用 docker service logs <smstake_app>
| Cache cleared successfully.
| Configuration cache cleared!
| Dropped all tables successfully.
| Migration table created successfully.
|
| In Connection.php line 664:
|
| SQLSTATE[42S02]: Base table or view not found: 1146 Table 'smstake.migratio
| ns' doesn't exist (SQL: insert into `migrations` (`migration`, `batch`) val
| ues (2014_10_12_100000_create_password_resets_table, 1))
|
|
| In Connection.php line 452:
|
| SQLSTATE[42S02]: Base table or view not found: 1146 Table 'smstake.migratio
| ns' doesn't exist
|
|
| Laravel development server started: <http://0.0.0.0:80>
| PHP 7.1.16 Development Server started at Thu Apr 5 07:02:22 2018
| [Thu Apr 5 07:03:56 2018] 10.255.0.14:53744 [200]: /js/app.js
| Cache cleared successfully.
| Configuration cache cleared!
| Dropped all tables successfully.
| Migration table created successfully.
| Migrating: 2014_10_12_000000_create_users_table
| Migrated: 2014_10_12_000000_create_users_table
| Migrating: 2014_10_12_100000_create_password_resets_table
| Migrated: 2014_10_12_100000_create_password_resets_table
| Migrating: 2018_01_11_235754_create_groups_table
| Migrated: 2018_01_11_235754_create_groups_table
| Migrating: 2018_01_12_085401_create_contacts_table
| Migrated: 2018_01_12_085401_create_contacts_table
| Migrating: 2018_01_12_140105_create_sender_ids_table
| Migrated: 2018_01_12_140105_create_sender_ids_table
| Migrating: 2018_02_06_152623_create_drafts_table
| Migrated: 2018_02_06_152623_create_drafts_table
| Migrating: 2018_02_21_141346_create_sms_table
| Migrated: 2018_02_21_141346_create_sms_table
| Seeding: UserTableSeeder
| Laravel development server started: <http://0.0.0.0:80>
| PHP 7.1.16 Development Server started at Thu Apr 5 07:03:23 2018
| [Thu Apr 5 07:03:56 2018] 10.255.0.14:53742 [200]: /css/app.css
I don't know if its due to migration problem or what. Sometime I can login and after few time I get redirected to Not found page again when clicking on the link inside dashboard.
所以我 运行 您的服务并发现了一些问题。
- mysql 中
docker-compose.yml
中的用户不同。这可能只是为了发布目的 - 在您的
Dockerfile
中,您使用了ENTRYPOINT
,这导致在迁移服务上也对 运行 执行了相同的命令。我改成了CMD - 您没有 运行
migration
服务与您的 mysql 数据库在同一网络中。所以mysql
无法从同一个访问。
这是我使用的最终合成文件
docker-compose.yml
version: '3.4'
networks:
smstake:
services:
db:
image: mysql:5.7
networks:
- smstake
ports:
- "3306"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: smstake
MYSQL_USER: tara
MYSQL_PASSWORD: password
volumes:
- mysql_data:/var/lib/mysql
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.role == manager
app:
image: 127.0.0.1:5000/myimage:latest
ports:
- 8000:80
networks:
- smstake
depends_on:
- db
- migration
deploy:
mode: replicated
replicas: 3
migration:
image: 127.0.0.1:5000/myimage:latest
command: sh -xc "sleep 10 && pwd && php artisan migrate:fresh 2>&1"
networks:
- smstake
depends_on:
- db
deploy:
restart_policy:
condition: on-failure
mode: replicated
replicas: 1
placement:
constraints:
- node.role == manager
volumes:
mysql_data:
Dockerfile
FROM alpine
ENV \
APP_DIR="/project" \
APP_PORT="80"
# the "app" directory (relative to Dockerfile) containers your Laravel app...
##COPY app/ $APP_DIR
# or we can make the volume in compose to say use this directory
RUN apk update && \
apk add curl \
php7 \
php7-opcache \
php7-openssl \
php7-pdo \
php7-json \
php7-phar \
php7-dom \
php7-curl \
php7-mbstring \
php7-tokenizer \
php7-xml \
php7-xmlwriter \
php7-session \
php7-ctype \
php7-mysqli \
php7-pdo \
php7-pdo_mysql\
&& rm -rf /var/cache/apk/*
RUN curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/bin --filename=composer
##RUN cd $APP_DIR && composer install
RUN mkdir /apps
COPY ./project /apps
RUN cd /apps && composer install
WORKDIR /apps
RUN chmod -R 775 storage
RUN chmod -R 775 bootstrap
copy ./run.sh /tmp
CMD ["/tmp/run.sh"]
然后再次 运行 服务。然后迁移顺利
而且该应用程序也运行良好