This is a simple express app with mysql database deployed with docker.
-
Docker Compose (only if you will use it instead of docker).
-
MySQL Server (only if you want to run this project on you local machine).
-
Yarn installed (globally recommended) or use
sudo npm i -g yarn
to install it.
-
-
Follow the instructions to setup a MySQL server on your local machine.
-
Use workbench or mysql shell to run the following script
CREATE DATABASE `express-app-with-docker-db`;
to create an empty to be used in our express app.
-
Go to the "sequelize.js" file located in the path "src/data/" and make sure all credentials are right.
Important, any changes in this credentials requires a change to the files "Dockerfile" and "docker-compose.yml" .
-
-
-
Run command
yarn start
-
You should see this message
Express server is up & running on port 8080
-
-
-
-
If you changed any credentials in the step database setup, you should update these changes in the files "Dockerfile" and "docker-compose.yml" .
-
Also you should update the file "Dockerfile" and use your host machine ip address instead of mine(172.19.0.1).
-
More details are included in the "Dockerfile" and "docker-compose.yml" files.
-
To Build a Docker Image and Deploy to a Docker Container, run command
docker-compose build
-
To run a docker container for every service in the built docker image (in our case two services web & db), run the command
docker-compose up -d
You will get two new containers with the name express-app-with-docker_web_1 and express-app-with-docker_db_1
-
To get the ip address for the newly created express app docker container, run command
docker inspect express-app-with-docker_web_1 | grep Address
Now you should have the following message
"LinkLocalIPv6Address": "", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "GlobalIPv6Address": "", "IPAddress": "", "MacAddress": "", "IPAddress": "172.19.0.3", "GlobalIPv6Address": "", "MacAddress": "02:42:ac:13:00:03"
The value of the
"IPAddress": "172.19.0.3"
is the ip address for th newly created docker container. -
To test the app, open your browser on
http://172.19.0.3:8080
orhttp://localhost:8080
You should get this message
[ { "id": 1, "name": "Samsung Galaxy S5", "price": "4500.00" }, { "id": 2, "name": "Samsung Galaxy S6", "price": "5000.00" }, { "id": 3, "name": "Huawei P10 Lite", "price": "5200.00" }, { "id": 4, "name": "Huawei P30", "price": "6500.00" }, { "id": 5, "name": "Huawei P30 Lite", "price": "5800.00" } ]
Or you may get a message indicates that the database server is not ready yet, don't worry and try again in 10 seconds.
-
-