Strategio
Published in

Strategio

Provisioning EC2 for a Basic Docker Image

onica.com

Amazon EC2, or Elastic Compute Cloud, allows you to “borrow” Amazon’s servers to test, run and manage your own applications. We can enrich AWS’s toolset by running a Docker image on our EC2 instance. Docker’s PaaS (Platform as a Service) enables you to neatly package your applications within a container, and quickly launch this container across platforms.

In this tutorial, I’ll walk you through provisioning an EC2 instance via AWS Console. Once our instance is built, we’ll transition to the AWS CLI for installing and launching our Docker image. To wrap up, we’ll visit our public DNS to verify the launch was successful.

Prerequisites

  • Amazon Web Services account accessed through a non-root user

We’re going to stick to the free tier services — costs should not be incurred if you properly stop your EC2 instance once we’re done.

  • AWS CLI
  • A folder accessible on your local machine to store SSH credentials

Step 1

In your browser, visit the AWS Console and log in using administer credentials. If you haven’t designated a non-root user, hop over to this tutorial before moving forward.

In the search bar, enter EC2.

On the left-hand side, select Instances.

Click on the blue Launch Instance icon.

Your EC2 wizard might display an invitation to try the beta version. It improves the user interface and offers a simplified view of the wizard. I recommend selecting Try it now! to experience the improved wizard.

Step 2

Let’s begin provisioning our instance by specifying a name. Next, choose Amazon Linux as your OS, since it’s part of the free tier.

Our instance type is pre-selected at t2.micro — keep it as such.

You’ll see key pair (login) options next. The key pair is security credentials that will allow you to access your instance via AWS CLI. Create a new RSA key pair with a .pem format. Download the keypair to a file you can easily access.

For network settings, ensure SSH traffic is selected, with your IP address specified. Also, select HTTP traffic so we can verify in a browser that our docker image launched correctly.

Finally, hit the orange Launch instance and wait while AWS works some magic.

Step 3

While still viewing your EC2 instance, hit Connect overhead, and copy the SSH address. Your key pair file should be included in this address, denoted by quotations.

Note the public DNS for your instance is also displayed here. We’ll grab this for our final step — viewing our image in a browser.

Launch AWS CLI/terminal, and cd into the folder housing your key pair. Paste the copied SSH address, and enter yes when the terminal prompts you to connect.

A successful connection displays [ec2-user@<IPADDRESS>~]$.

Step 4

Now that we’ve constructed our virtual machine, we can begin on our Docker tasks. Let’s run a series of commands to update packages and install Docker Engine.

Update packages:

sudo yum update -y

Install Docker Engine:

sudo amazon-linux-extras install docker

Start Docker and instruct the daemon to start after subsequent reboots:

sudo service docker startsudo usermod -a -G docker ec2-user

Close your terminal, and SSH back into your instance. This should solidify the permissions we added beforehand.

Step 5

Finally, we can construct our Docker image. Create a new file, which will house our Docker image instructions:

touch Dockerfile

Enter your file to modify it, and hit “i” to insert text.

vim Dockerfile

Add the following code:

Key the following to save and close the vim editor:

:wq

Begin building image from our new file:

docker build -t devops-fun .

Run our new image:

docker run -t -i -p 80:80 devops-fun

In your browser, paste the public DNS for your EC2 instance.

Success! We’ve successfully launched our Docker image, hosted on an EC2 instance.

Back in the terminal, stop Docker by keying “ctrl + c”, and stop your EC2 instance to keep from accruing charges.

Comment with your experiences, and follow me for more content while I learn the ropes as a junior technologist.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store