Docker: Launching Your First Container

Docker: Launching Your First Container

Create an EC2 instance on AWS.

  1. Log in to the AWS Management Console.

  2. Navigate to the EC2 Dashboard and click on the “Launch Instance” button.

  3. Enter a name for your EC2 instance.

  4. Select AMI, instance type and choose a key pair or create a new key pair.

  5. Create or select an existing Security Group and make sure port 22 is open to ssh into the instance.

  6. Click on Launch Instance.

  7. Connect to your instance either by 3rd party software like putty or MobaXterm or we can also connect using AWS EC2 instance connect.

Task 1 : Installing Docker and Running a nginx container.

  • Update the system.
sudo apt update && sudo apt upgrade
  • Install docker and give necessary permissions.
sudo apt install docker.io
sudo usermod -aG docker $USER
sudo chmod 666 /var/run/docker.sock
  • Check Docker version.
docker --version
  • Pulling and running a nginx image from Docker Hub.
docker run nginx
  • Check the running containers.
docker ps

Task 2 : Exposing Docker container to outside world using port mapping.

We have successfully ran docker container , but it is only accessible inside our machine , to access our nginx container from browser we have to do port mapping ( map port of the host to port of the container).

docker run -d -p80:80 nginx

Task 3 : Add port in Inbound Security Group.

To access our container from the browser we need to add port 80 in inbound security groups.

Task 4: Access the running container from browser.

We can access the container from brower using Public IP of AWS instance and port 80.

<Public ip address of EC2>:<Port>

Ex- 20.207.168.210:80

Task 5 : Stop the running containers.

As we can access the nginx container from the browser, Lets see how we can see the running containers and stop them .

  • See all running containers.
docker ps
  • See all running and stopped containers.
docker ps -a

  • Stop a running container ( we can stop a running container by container id ).
docker stop < container-id >
Ex - docker stop e5b001d7141e

Thanks for reading!

Happy Learning!