<p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Docker and Amazon Web Services (AWS) cooperate to make it simple for coders to construct, boat, and run circulated applications. </span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">By joining EC2's flexible infrastructure with Docker containers portability, groups can accelerate improvement, make sending more available, and ensure that everything is similar across all platforms. </span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">It is a powerful tool for cloud-native development, and Docker can run apps in separate containers, no matter what infrastructure is underneath. Starting with running Docker on an EC2 instance is a good idea if you want to use Docker on AWS.</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">This article will tell you the best way to introduce Docker on an AWS EC2 server step by step. It doesn't make any difference if you've never used AWS or Docker; this guide will assist you in the beginning. </span></p><h2 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Step 1: Launch an AWS EC2 Instance</span></h2><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">The first thing we need to do before installing Docker is to start up an EC2 server. To get your EC2 server up and running, do these things:</span></p><h3><span style="font-family:Arial,sans-serif;">Sign in to the AWS Management Console</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">To access the EC2 homepage, sign in to your AWS account and click "EC2" under the "Services" tab.</span></p><h3><span style="font-family:Arial,sans-serif;">Launch a New EC2 Instance</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Click the "Launch Instance" button and pick the Amazon Machine Image (AMI). People usually like the Amazon Linux 2 AMI for Docker installations because it works well with Docker and is designed for AWS.</span></p><h3><span style="font-family:Arial,sans-serif;">Choose an Instance Type</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Find an instance type that works for you. Any t2.micro server from the Free Tier is a good choice for basic Docker testing or development because it has enough resources to run light containers.</span></p><h3><span style="font-family:Arial,sans-serif;">Configure Security Group</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Next, set up the rules for your security group. Add a rule for port 22 to allow SSH access. Depending on the services your containers will offer, you may need to add more rules in the future.</span></p><h3><span style="font-family:Arial,sans-serif;">Key Pair and Review</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">You can use a current key pair or create a new one to access SSH. Lastly, click "Launch" to start the EC2 server. When your instance is ready, use your key pair to connect to it via SSH.</span></p><h2 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Step 2: Update and Install Dependencies</span></h2><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Once linked to your EC2 instance, you should update the system and add any dependencies you need before installing Docker.</span></p><pre><code class="language-plaintext">sudo yum update -y sudo yum install -y yum-utils</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">These commands update the package manager and install utilities that will help us manage repositories and packages for Docker.</span></p><h2 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Step 3: Install Docker</span></h2><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Now that your system is up to date, you can run Docker. Installing Docker on Amazon Linux 2 is simple.</span></p><h3><span style="font-family:Arial,sans-serif;">Add Docker Repository</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">We'll add Docker's official repository to ensure you get the latest Docker version.</span></p><pre><code class="language-plaintext">sudo amazon-linux-extras install Docker -y</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Getting the suitable packages for running Docker on Amazon Linux 2 is made more accessible by Amazon Linux Extras.</span></p><h3><span style="font-family:Arial,sans-serif;">Start Docker Service</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">After setting up Docker, you need to start the service and ensure it runs every time the instance starts.</span></p><pre><code class="language-plaintext">sudo systemctl start docker sudo systemctl enable Docker</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">These commands start Docker and are configured to run automatically at boot.</span></p><h3><span style="font-family:Arial,sans-serif;">Verify Docker Installation</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Run the following command to make sure Docker was installed correctly:</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><i>docker --version</i></span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">If Docker is set up properly, it will give you the version number. For example, you might see something like Docker version <i>20.10.7.</i></span></p><h2 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Step 4: Manage Docker as a Non-root User</span></h2><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Docker commands need root access to run by default. You can add your user to the <i>docker</i> group so that you don't have to type <i>sudo</i> before every Docker command:</span></p><pre><code class="language-plaintext">sudo usermod -aG docker $USER</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">To see the changes, log out and log in again after adding yourself to the Docker group:</span></p><pre><code class="language-plaintext">exit</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Then reconnect to your EC2 instance via SSH and test Docker without <i>sudo.</i></span></p><h2 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Step 5: Test Docker Installation</span></h2><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Now that it's been set up and loaded, let's test Docker by running it as a simple container.</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><i>docker run hello-world</i></span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">It gets a test picture from the Internet and runs it in a container. If Docker is working correctly, you should see a message that says, "Hello from Docker!"</span></p><h2 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Step 6: Deploying Containers on EC2</span></h2><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Now that Docker is running on your EC2 server, you can start using your containers. Docker Hub has container files that you can use or make your own. Say you want to pull and start a Nginx container. Here's the command:</span></p><pre><code class="language-plaintext">docker run -d -p 80:80 nginx</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">This command will run the nginx container in detached mode <i>(-d)</i>. Port 80 on the EC2 server will be mapped to port 80 on the container. You can now use a browser to connect to the Nginx web server by going to the public IP address of your EC2 instance.</span></p><h2 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Step 7: Managing Docker Containers</span></h2><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">You can manage your running containers using various Docker commands. For example:</span></p><ul><li><span style="font-family:Arial,sans-serif;"><strong>List running containers</strong>:</span></li></ul><pre><code class="language-plaintext">docker ps</code></pre><ul><li><span style="font-family:Arial,sans-serif;"><strong>Stop a container</strong>:</span></li></ul><pre><code class="language-plaintext">docker stop [container_name]</code></pre><ul><li><span style="font-family:Arial,sans-serif;"><strong>Remove a container</strong>:</span></li></ul><pre><code class="language-plaintext">docker rm [container_name]</code></pre><h2 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Step 8: Configuring Docker for Production</span></h2><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">If you want to use Docker for production tasks on EC2, here are some extra things you can do to ensure stability, security, and performance:</span></p><h3><span style="font-family:Arial,sans-serif;">Enable Docker Swarm or Kubernetes</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">If you need to manage many containers across many EC2 instances, you might want to use Docker Swarm or Kubernetes for orchestration. These tools make deployment, scaling, and control of containerized apps easy and automatic.</span></p><h3><span style="font-family:Arial,sans-serif;">Configure Security Groups</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Ensure that your security groups are set up correctly so that they only let traffic on the necessary ports, like 80/443 for web services, block access to private ports.</span></p><h3><span style="font-family:Arial,sans-serif;">Set Up Auto-scaling</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Set up AWS Auto Scaling groups to change the number of EC2 servers based on how much traffic or load there is. This makes sure that your products can handle more demand without any help from a person.</span></p><h2 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Setting Sail with Docker on EC2</span></h2><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Setting up Docker on an AWS EC2 server makes it possible to run containerized apps on a large scale. Whether you're building microservices, launching web apps, or trying out cloud-native architectures, Docker and EC2 work great together to make things more flexible, consistent, and efficient.</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">You have now successfully installed Docker on EC2. You can now look into more advanced Docker features, such as Kubernetes for container orchestration, AWS CodeBuild and CodePipeline for CI/CD pipelines, and IAM roles and rules for security. You can play in the cloud, so start building!</span></p><p style="text-align:justify;"><span style="font-family:Arial, sans-serif;">Read More</span></p><p style="text-align:justify;"><a href="https://devopsden.io/article/cloudflare-web-performance-and-Security">https://devopsden.io/article/cloudflare-web-performance-and-Security</a></p><p style="text-align:justify;"><span style="font-family:Arial, sans-serif;">Follow us on</span></p><p style="text-align:justify;"><a href="https://www.linkedin.com/company/devopsden/">https://www.linkedin.com/company/devopsden/</a></p>