Running Web App from Docker Using EC2 Instance

Photo by Ian Taylor on Unsplash

Running Web App from Docker Using EC2 Instance

(fastest way)

  1. Launch EC2 Instance and Install Docker using EC2 template

    Click on Launch Instance on EC2 Dashboard

    Select Ubuntu from Quick start option

    Don't forget to enable HTTPS and HTTP traffic from Internet to allow anyone to connect from our instance.

    Under Advanced details fill the User Data according to Ubuntu Operating system. This contains the commands which AWS executes before starting the operating system at launch time.

    When we connect to our instance in our shell docker --version gives the details about docker.

    1. Copy project from github to our ec2 instance (local machine)

      Using git clone <repo-url> we copy repository in our ec2 instance. All the files will be displayed.

      In this directory only we will create Dockerfile. This would be the home directory to our Docker container.

      The Dockerfile looks something like this

      Dockerfile -> Docker Image -> Docker Container

  2. Create a Dockerfile using the keywords and arguments
    FROM specifies name of docker image along with its tag. There is a dockerhub which contains all the docker images. We could have used linux docker image but we chose node as it is higher level base image than linux.
    WORKDIR specifies working directory for the container. The image would be creating a container, this option specifies app folder would be created inside docker container which would contain application content

    After executing docker exec to get inside container, we can see /app has been created inside it.
    COPY would simply copy file from Docker root (same location as Dockerfile) to the created container. It runs on the host machine and copies to container OS.
    RUN runs command with shell inside base linux layer in the container. We can have multiple RUN commands.
    EXPOSE would simply allow the port to connect to outer world, here 8000 is HTTP port
    CMD executes commands from shell/bash in the container to start the application. CMD is executed only one time when container is created from image.

    After creating the Dockerfile we have to save it with the exact name -> Dockerfile

  3. Create an Docker Image from Dockerfile

    After we create a Dockerfile we can build image out of it using the following command

    docker build -t my-app:1.0 .

    This command basically creates a docker image my-app out of a Dockerfile. We can also view the created image using docker images

    Jenkins actually creates an image from the Dockerfile.

  4. Run the container from Docker Image

    Till the step 3 we have created a docker image, in the last step we would create a docker container and expose our IP and port to world so others can access our app using URL

    We look for our image details using docker images and then using docker run -p we did port mapping so that world could access our web application.

    Also under Security groups -> inbound rules we allow port 8000 in custom TCP so that world could access port 8000 of our host EC2 instance.

    We finally, go to our web browser type in <public-ip> followed by <port> and from any browser can access the web application.

    References

    1. TechWorld with Nana for detailed Docker Tutorial

    2. Train With Shubham for ZeroToHero

    3. Hash13 for AWS services & userdata