Task:
- Create a Dockerfile for a simple web application (Django application)
- Build the image using the Dockerfile and run the container
- Verify that the application is working as expected by accessing it in a web browser
- Push the image to a public or private repository (e.g. Docker Hub )
Steps to create a Docker container for a Django application
Log in to your AWS EC2 console on your Ubuntu machine.
Install Docker on your Ubuntu instance by running the following commands:
sudo apt update sudo apt install docker.io
These commands will update the package index and install Docker on your Ubuntu instance.
Add your current user to the Docker group using the following command:
sudo usermod -aG docker $USER
This command adds the current user to the Docker group, allowing the user to run Docker commands without using
sudo
every time.Clone the code from the provided GitHub repository by running the following command:
git clone https://github.com/Dhananjaykul/react_django_demo_app.git
This command will create a new directory named
react_django_demo_app
in your current directory, which contains the source code for the demo application.Navigate to the
react_django_demo_app
directory using thecd
command.Create a new file named
Dockerfile
in this directory using thevim
command, and open it .create the content as per the requirements.txt into the
Dockerfile
and save it.Build the Docker image using the following command:
docker build . -t react-django-app:latest
This command will build a new Docker image named react-django-app from the
Dockerfile
in the current directory (.
).Open your AWS EC2 console and navigate to the "Instances" page.
Select the instance you are running the Docker container on and click on the "Security" tab.
Click on the security group associated with your instance.
Click on the "Edit inbound rules" button.
Add a new rule to allow inbound traffic on port 8000 from all IPv4 addresses by selecting "Custom TCP rule" as the "Type", entering "8000" as the "Port range", selecting "0.0.0.0/0" as the "Source", and leaving the "Description" field blank.
Click on the "Save rules" button to apply the changes.
Once the image is built, you can start a new Docker container using the following command:
docker run -d -p 8000:8000 react-django-app:latest
This command will start a new Docker container from the react-django-app image, and map the container's port 8000 to the host machine's port 8000.
If there are no errors, you should see the Django development server start up in the terminal. You can now access your Django application by opening a web browser and navigating to
http://<your-ec2-instance-public-ip>:8000
.
Note: Replace <your-ec2-instance-public-ip>
with the public IP address of your AWS EC2 instance.
Congratulations! โจ๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐You have successfully installed Docker on your Ubuntu instance, added the current user to the Docker group, created a Docker container for the provided React-Django demo application, and exposed port 8000 for everyone using IPv4 in the inbound rule of your instance's security group.