Creating a service and deploying using docker
Following the Lab 03 we are going to create a service and attach it to our application
- It will be an update in our application
Prerequisite
The updated folder hierarchy will look like -
my-project/
├── app.py
├── dockerfile
├── requirements.txt
└── template
└── login.html
Points to remember
- There will be change in
app.py
only becauyse it contains the backend and we are adding a new serviceof whihc our application is unaware of.
Update the directory
Navigate inside your working directory (my-project
in my case)
mkdir template
touch template/login.html
Create/Edit a login page
Create a login page or find the entire code here (opens in a new tab)
nano template/login.html
Create/Edit backend (app.py
)
Write your logic or find the entire code here (opens in a new tab)
nano app.py
Re-build and run dockerfile
To build an image using the created dockerfile
docker build -t my-app:v2 .
To avoid the naming conflict either delete the existing image or tag that image with version (used in this scenario)
The docker image of our created application is created.
To Verify
docker images --filter reference=my-app:v2
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
my-app latest 0cf333799ced 2 seconds ago 136MB
Step 6: Create a docker container using my-app
image
docker run -p 5000:5000 --name production my-app
The container is running at port 5000
.
To Verify
docker ps -a
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5ed7ca66713 my-app "-p 5000:80" 2 minutes ago Created 5000/tcp prod
Step 7: Open your local web browser and open the following accordingly -
Command | Description |
---|---|
Local Virtual Machine | http://127.0.0.1:5000 or http://localhost:5000 or http://<YOUR_VM_IP_ADDRESS>:5000 |
AWS EC2 | http://<PUBLIC_IP_ADDRESS>:5000 |
Run using script
wget https://xanderbilla.s3.ap-south-1.amazonaws.com/Semester_V/resources/Lab_04.sh > /dev/null 2>&1
chmod +x Lab_04.sh
./Lab_04.sh
Output:
[OK] Create project directory
[OK] Create required files
[OK] Write code in app.py
[OK] Write requirements in requirements.txt
[OK] Write code in dockerfile
[OK] Build and run Docker image
Additional Notes
-
If you're using AWS EC2 make sure to update your Security group inbound rule that allow Port 5000 to
0.0.0.0/0
-
Other useful docker commands that can be used are
Command | Description |
---|---|
docker run <IMAGE_NAME> | Create a docker container. |
docker ps | Lists only the running containers. |
docker ps -a | Lists all containers, including running, stopped, and exited ones |
docker stop <CONTAINER_ID> | Stop the running docker container |
docker rm <CONTAINER_ID | Lists only the running containers. |
docker rmi -f <IMAGE_ID> | Lists only the running containers. |