Create DockerImage for python application :
To use an Amazon Elastic Compute Cloud (EC2) instance to run Docker containers, you'll need to perform the following steps:
Launch an EC2 Instance:
Go to the AWS Management Console and navigate to the EC2 service.
Click the "Launch Instance" button to create a new EC2 instance.
Select an Amazon Machine Image (AMI). You can choose an Amazon Ubuntu.
Choose an instance type based on your application's requirements. The t2.micro instance is a good starting point for small-scale testing.
Launch the instance.
Connect to the EC2 Instance:
Select the running instance and click on connect > ec2 instance connect > connect.
Write the following commands when connect to ec2 instance in order to install docker on instance.
sudo apt update
: This command updates the package list on your Ubuntu system, ensuring that you have the latest information about available packages.sudo apt install
docker.io
: This command installs Docker on your Ubuntu instance. Thedocker.io
package is the Docker package available in Ubuntu's official repositories.whoami
: This command prints the username of the currently logged-in user. It's used to capture the current user's username and store it in the$USER
variable.sudo usermod -aG docker $USER
: This command adds the current user to thedocker
group, allowing the user to run Docker commands without needing to usesudo
each time. The$USER
variable is used to reference the current user's username.systemctl restart docker
: This command restarts the Docker service to ensure that the changes made in step 4 take effect immediately.reboot docker
: This command reboots the EC2 instance. Rebooting can be useful to ensure that any changes to group memberships take effect without needing to log out and back in.
Here are the steps to create a Docker image for a Python application:
Create a flask application named
app.py
.Create a Dockerfile: Create a file named
Dockerfile
(with no file extension) in your project directory. This file will contain instructions for building your Docker image.This command tells Docker to build an image with the name "flask-app" and the "latest" tag using the Dockerfile in the current directory (specified by the dot
.
)docker build -t flask-app:latest
docker run -d flask-app:latest
Docker ps