Agentic AI with Java: Live Cohort
DockerDocker Setup & Practice

Creating An EC2 Instance And Installing Docker

How to launch an Ubuntu EC2 instance, connect to it, install Docker, and run a test container.

An EC2 instance is a virtual server in AWS.

For Docker practice, EC2 is useful because it lets you install Docker on a real Linux machine and run containers the way you would on a server.

This lesson uses Ubuntu as the operating system because it is beginner friendly and common in cloud practice.

Launch an EC2 instance

Open the AWS Management Console and go to EC2.

Create a new instance with these basic choices:

SettingSuggested value
Namedocker-practice-server
AMIUbuntu Server
Instance typeFree-tier eligible small instance
Key pairCreate or select an existing key pair
Security groupAllow SSH from your IP

Keep the setup simple for practice.

Security group rules

To connect using SSH, port 22 must be allowed from your IP address.

Later, when you run a web application in Docker, you may also need to allow the application port, such as 8080, 3000, or 80.

PortUse
22SSH connection
80HTTP web traffic
3000Common React/Node practice port
8080Common Java/Spring Boot practice port

Do not open all ports to everyone for practice. Add only the ports you need.

Connect to the instance

After the instance starts, copy its public IP address.

From your terminal, connect using SSH:

ssh -i my-key.pem ubuntu@public-ip-address

Replace my-key.pem with your key file name and public-ip-address with the actual EC2 public IP.

Once connected, update the package list:

sudo apt update

Install Docker

Install Docker using Ubuntu packages:

sudo apt install docker.io -y

Start Docker and enable it to start automatically:

sudo systemctl start docker
sudo systemctl enable docker

Check the installed Docker version:

docker --version

Run Docker without sudo

By default, Docker commands may require sudo.

You can add the current user to the Docker group:

sudo usermod -aG docker $USER

Then log out and connect again for the group change to apply.

After reconnecting, test:

docker run hello-world

If this works without sudo, your user can run Docker commands.

Run a simple web container

Try running Nginx:

docker run -d -p 80:80 nginx

Then open the EC2 public IP in your browser:

http://public-ip-address

If the security group allows port 80, you should see the Nginx welcome page.

Clean up

After practice, stop and remove the container:

docker ps
docker stop container-id
docker rm container-id

When you are done with the EC2 instance, stop or terminate it from the AWS console.

Do not leave EC2 instances running after practice unless you intentionally need them. Stopped and terminated are different: stopped keeps the instance saved, terminated deletes it.

Written By: Shiva Srivastava

How is this guide?

Last updated on