How to set up docker image in EC2 instance and run that
- User
How to set up docker image in EC2 instance and run that
- Assistant
Setting up a Docker image on an Amazon EC2 instance involves several steps: launching an EC2 instance, installing Docker on it, pulling the desired Docker image, and running a container from that image. Below is a comprehensive guide to help you through the process.
Prerequisites
- AWS Account: Ensure you have access to an AWS account with permissions to create EC2 instances.
- Understanding of Docker: Basic knowledge of Docker concepts like images and containers.
- SSH Client: You'll need an SSH client to connect to your EC2 instance. For Windows, you might use PuTTY; for macOS and Linux, you can use the terminal.
Step 1: Launch an EC2 Instance
-
Navigate to the AWS Management Console:
- Log in to your AWS account.
- Go to the EC2 Dashboard.
-
Launch an Instance:
- Click on "Launch Instance".
-
Choose an Amazon Machine Image (AMI):
- Select an OS for your instance. For this guide, we'll use Amazon Linux 2 AMI as it is optimized for AWS and is configured to work well with Docker.
-
Choose an Instance Type:
- Select an instance type according to your needs. For basic testing, t2.micro (which is eligible for the free tier) is sufficient.
-
Configure Instance Details:
- You can leave the default settings or adjust them according to your requirements.
-
Add Storage:
- Default storage is usually sufficient, but ensure you have enough space for the Docker image and containers you plan to run.
-
Add Tags (Optional):
- Tags are useful for identifying your instances.
-
Configure Security Group:
- Create a new security group or select an existing one.
- Important: Add rules to allow SSH (port 22) from your IP address.
- If your Docker container will serve web traffic, add HTTP (port 80) and/or HTTPS (port 443) rules.
-
Review and Launch:
- Review your settings.
- Click on "Launch".
-
Select a Key Pair:
- Choose an existing key pair or create a new one.
- Important: Download the private key (
.pemfile) and keep it secure. You'll need it to SSH into your instance.
Step 2: Connect to Your EC2 Instance via SSH
-
Set Permissions for Your Key Pair File:
- In your terminal, navigate to the directory containing your
.pemfile. - Run:
chmod 400 your-key-pair.pem
- In your terminal, navigate to the directory containing your
-
Connect to the Instance:
- Obtain the Public DNS or Public IP of your instance from the EC2 dashboard.
- Run:
ssh -i "your-key-pair.pem" ec2-user@your-instance-public-dns- Replace
your-key-pair.pemwith your key pair file name. - Replace
your-instance-public-dnswith your instance's public DNS.
- Replace
Step 3: Install Docker on the EC2 Instance
Once connected to your instance:
-
Update the Installed Packages and Package Cache:
sudo yum update -y -
Install Docker:
sudo amazon-linux-extras install docker -y -
Start the Docker Service:
sudo service docker start -
Add the ec2-user to the Docker Group:
- This allows you to run Docker commands without
sudo.
sudo usermod -a -G docker ec2-user- Important: You'll need to log out and log back in for this change to take effect.
- This allows you to run Docker commands without
-
Log Out and Log Back In:
- Exit the SSH session:
exit - Reconnect using SSH.
- Exit the SSH session:
-
Verify Docker Installation:
docker info- This command should display information about your Docker installation.
Step 4: Pull a Docker Image
You can now pull any Docker image from Docker Hub or other repositories.
- Pull an Image from Docker Hub:
- For example, to pull the latest Nginx image:
docker pull nginx:latest - Replace
nginx:latestwith the image name and tag you wish to use.
- For example, to pull the latest Nginx image:
Step 5: Run a Docker Container
-
Run a Container from the Image:
- For example, to run the Nginx container:
docker run -d --name mynginx -p 80:80 nginx:latest-d: Runs the container in detached mode (in the background).--name mynginx: Names the container "mynginx".-p 80:80: Maps port 80 of the host to port 80 of the container.
- For example, to run the Nginx container:
-
Verify the Container is Running:
docker ps- This should list your running container.
-
Test the Application:
- Open a web browser and navigate to your EC2 instance's Public IP.
- You should see the default Nginx welcome page.
- If you cannot access it, ensure that your security group allows inbound traffic on port 80.
- Open a web browser and navigate to your EC2 instance's Public IP.
Step 6: Manage Your Docker Containers
- View Running Containers:
docker ps - Stop a Container:
docker stop mynginx - Remove a Container:
docker rm mynginx - Remove an Image:
docker rmi nginx:latest
Additional Considerations
-
Data Persistence:
- Data created inside the container will be lost when the container is removed.
- Use Docker volumes to persist data:
docker run -d --name mynginx -p 80:80 -v /host/path:/container/path nginx:latest
-
Environment Variables:
- You can pass environment variables to the container:
docker run -d --name mycontainer -e ENV_VAR=value imagename
- You can pass environment variables to the container:
-
Docker Compose:
- For more complex setups involving multiple containers, consider installing Docker Compose.
Install Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/download/2.0.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose- Verify the installation:
docker-compose version
Security Best Practices
-
Least Privilege Principle:
- Only open necessary ports in your security groups.
- Limit SSH access to specific IP addresses.
-
Regular Updates:
- Keep your Docker images and containers updated to receive security patches.
-
Use IAM Roles:
- Assign IAM roles to your EC2 instance for accessing other AWS services securely, without embedding credentials.
Clean Up Resources
-
Stop and Remove Containers:
docker stop mycontainer docker rm mycontainer -
Terminate EC2 Instance:
- From the AWS EC2 Dashboard, select your instance and click on "Terminate instance" to avoid incurring charges.
Conclusion
You've now set up a Docker environment on an AWS EC2 instance, pulled a Docker image, and run a container. This foundational setup allows you to deploy containerized applications on AWS. For production environments, consider additional factors like orchestration (using Kubernetes or ECS), scaling, logging, and monitoring.
References