Return-portal containerization to AWS ECS EC2 cluster
created return-portal database into dev-finance-db with the same schema as on local developers machines
export schema with mysqldump into an .sql file and run the script on destination db
created ECS cluster app-stack on AWS Dev account
make sure the attached IAM role has access to the required AWS services
modify Dockerfile and docker-compose.yml for docker container configuration
docker-compose.yml will be used only for building and running containers (MySQL, app, nginx) on docker local machine
Dockerfile will use php-fpm:alpine image to build an image for our container
configured nginx configuration file /config/nginx/conf.d/app.conf
this configuration file will be copied over to /etc/nginx/conf.d location with docker-compose.yml for local env and Dockerfile on AWS ECS env
nginx will proxy route traffic to our app:8000
created AWS ECR new repository return-portal-dev-ecr. Connect your local terminal to this new ECR repo:
aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin AWSAccountID.dkr.ecr.us-east-2.amazonaws.com
build local app docker image
docker build -t return-portal:latest . = this will create an image for ARM64 architecture which will make Docker container to fail on ECS cluster.
docker buildx build --platform=linux/amd64 -t return-portal-dev-ecr . = this command will build an image for AMD64.
build local nginx docker image (optional or you can build image through docker-compose.yml)
docker run -p 127.0.0.1:8000:80 --name returnportal_nginx -it return-portal-nginx
run docker-compose to build your app and webserver
docker-compose up
docker-compose up —build ==> this is building new images and updates our app as well
add mysql build into your docker-compose.yml
docker-compose up = to bring all containers up
docker-compose exec return-portal-app php artisan migrate = to test database connection and tables creation
created start-script.sh script, where we start php-fpm in the background, run ‘php artisan migrate’ and then bring php-fpm back in the foreground:
start-script.sh has been added to Dockerfile
tty needs to be enabled in docker-compose.yml so we have a terminal to run the above script
Note: to run the app you have to run:
- npm run dev = Laravel app will start package.json `dev` script
- npm run build = runs production script
- docker-compose up = will start docker containers so you can open the app in your browser
============================
On the local machine:
Install ‘composer’ and then all packages required by your project:
$brew install composer = this command will have to go to the ‘Build’ stage
$composer install —ignore-platform-reqs = this command will have to go to the ‘Build’ stage
Clear node module, re-install node and run production environment:
$rm -rf node_modules/ && npm install && npm run prod = this command will have to go to ‘Build’ stage
We need to run the following command so that the app will be able to update the database with new entries, create a queue for client requests and listen on WebSockets port 6001:
$php artisan migrate
$php artisan queue:work
$php artisan websockets:serve
$docker-compose exec app php artisan migrate ==> this will run ‘php artisan migrate’ inside the docker app container
$docker-compose build —no-cache = deletes cache before build
$docker-compose down = deletes all existing images
$docker build -t return portal-nginx:latest -f Dockerfile_nginx .
$docker exec 76756fd6ebcd php artisan websockets:serve = container needs to listen on port 6001 for incoming client communication aside the port 9000 where it listens for URL requests
Open up MySQL container terminal and type:
$mysql -h 7c0fe2ee16c1 -u root -p password;
$show databases;
$use return_portal;
$show tables;
$show tables from return_portal;
$show tables like ‘permissions%’;
$INSERT INTO `return_portal`.`users` (`id`, `name`, `email`, `email_verified_at`, `password`, `remember_token`, `created_at`, `updated_at`) VALUES ('1', 'nameOfuser', 'UserEmailAddress', '2022-10-03 00:00:00', 'PasswordForAuthentication', 'tokenForGettingAccess', '2022-10-03 00:00:00', '2022-10-03 00:00:00');
$INSERT INTO `entities` VALUES (1,'CompanyName1','2022-10-06 23:36:53','2022-10-06 23:36:53','G'),(2,'CompanyName2','2022-10-06 23:36:53','2022-10-06 23:36:53','W'),(3,'CompanyName3','2022-10-06 23:36:53','2022-10-06 23:36:53','B');
============================
docker network ls
docker image ls
docker run -d --network=bridge --name=app rportal-app
docker run -d -p 8000:80 --network=bridge --name=nginxserver rportal-nginx
docker exec -it nginxserver ping rportal-app
$docker exec 76756fd6ebcd php artisan migrate:fresh --force = run this artisan migration from container host instance
===========================
FROM nginx:alpine
COPY ./config/nginx/conf.d/app.conf /etc/nginx/conf.d/default.conf
COPY . /var/www
=============================