Hello learners,
I'm back with Part 3 of the current Series.
First of all, my sincere thanks and gratitude to Shubham bhaiyaa (TrainwithShubham) for releasing this series. I'm going to document the process for you guys.
Overview:
In this Part, we are going to manage our docker containers with Docker-compose which is an easier and more automated way to operate docker containers.
We already have docker-compose.yml file in our code. You can use that or can create your own.
Step 1: Log in to Dockerhub and push the existing images
You can use below command to perform that.
docker login
Tag the image with the dockerhub username so that dockerhub can recognize that.
docker tag flaskapp:latest <docker username>/flaskapp:latest
Now Push the image to docker hub.
docker push <docker username>/flaskapp:latest
It will now reflect on your docker hub.
Step 2 : Create Docker-compose file
Now create a docker-compose.yml file with your image or you can use the existing one.
Note: If you don't have an docker image and want to build the image first, you should add these lines in your code.
backend:
build:
context: .
We already have an image so we are using the below.
version: '3'
services:
backend:
image: sitabjadocker/flaskapp:latest
ports:
- "5000:5000"
environment:
MYSQL_HOST: mysql
MYSQL_USER: admin
MYSQL_PASSWORD: admin
MYSQL_DB: mydb
depends_on:
- mysql
mysql:
image: mysql:5.7
ports: "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mydb
MYSQL_USER: admin
MYSQL_PASSWORD: admin
volumes:
- ./message.sql:/docker-entrypoint-initdb.d/message.sql # Mount sql script into container's /docker-entrypoint-initdb.d directory to get table automatically created
- mysql-data:/var/lib/mysql # Mount the volume for MySQL data storage
volumes:
mysql-data:
version: '3'
services:
backend:
image: <your docker id/image name:tag>
ports:
- "5000:5000"
environment:
MYSQL_HOST: mysql
MYSQL_USER: admin
MYSQL_PASSWORD: admin
MYSQL_DB: mydb
depends_on:
- mysql
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mydb
MYSQL_USER: admin
MYSQL_PASSWORD: admin
volumes:
- ./message.sql:/docker-entrypoint-initdb.d/message.sql # Mount sql script into container's /docker-entrypoint-initdb.d directory to get table automatically created
- mysql-data:/var/lib/mysql # Mount the volume for MySQL data storage
volumes:
mysql-data:
Step 3: Run Docker Compose Up
Now stop and remove running containers to give it a fresh start.
To start using docker-compose, run the command
docker-compose up -d
Step 4: Test Application:
And now BOOM !! 🚀
We can see our application is running.
💖 Thanks for being with me till the end. I hope that I provided you with some useful knowledge. Don't forget to spread the blog to your friends and community. 💖
Feel free to ask any questions, I'm just a DM away.👍