Deploying application as Docker Container

Deploying application as Docker Container

Deploy an application with docker in 10 simple steps

Docker

Docker is a platform that packages an application and all its dependencies together in the form of a container. The containerization aspect ensures that the application works in any environment.

How to deploy an application with docker

  1. Create an EC2 instance in the AWS console.

  2. Check if the docker services are installed on the system
    systemctl status docker

  3. Now, install docker by using the below command
    sudo apt-get install docker

    systemctl status docker

  4. Adding current user and docker to a group to avoid adding sudo every time to run docker commands
    sudo usermod -aG docker $USER

  5. Pull a git repo that you want to deploy with docker.
    git clone https://github.com/LondheShubham153/react_django_demo_app

  6. Now go inside react_django_demo_app
    and list the contents to know the requirements to run the application

    .py files indicate that it requires Python, and Django is also used to run the app.

    Now, we need to write a Dockerfile to build the image which in turn is used to run the container.
    vi Dockerfile

    Syntax of Dockerfile

    Environment Variable

    Function

    FROM

    A FROM statement defines which image to download and start from.
    Example: To download the Python image from the docker hub.
    FROM python:3.9

    COPY

    Copy files from the local system from source to destination
    Example: Copy all the files from the current directory and pasting in the same docker.
    COPY . .

    RUN

    The RUN statement defines running a command through the shell, waiting for it to finish and saving the result.
    Example: To install the requirement.txt via PIP
    RUN pip install -r requirements.txt

    CMD

    CMD specifies the whole command to run.
    Example: To run the Django development server from anywhere on port 8000
    CMD ["python","manage.py","runserver","0.0.0.0:8000"]

  7. Now you need to build an image from Dockerfile by the following command
    docker build . -t react-django-app:latest

    Note: . ---> dot refers to the Dockerfile location which is current directory
    -t ---> it is for tagging the image

  8. Now the final step is to run the container.
    To run the container you need to give the below command:
    docker run -d -p 8000:8000 react-django-app:latest

  9. Now in your EC2 instance edit the inbound rules in the security group for port 8000

  10. Now you can run the application with the help of the public IP address of your instance at port 8000

    Woo Hoo!!!
    It's done.
    You have finally deployed an application with the help of Docker.

    Please drop a comment if you like the article.