What is Docker?
Docker is the company driving the container movement and the only container platform provider to address every application across the hybrid cloud. Today’s businesses are under pressure to digitally transform but are constrained by existing applications and infrastructure while rationalizing an increasingly diverse portfolio of clouds, datacenters and application architectures. Docker enables true independence between applications and infrastructure and developers and IT ops to unlock their potential and creates a model for better collaboration and innovation. Learn more about Docker at docker.com
Here we are going to work on 2 different scenario:
Scenario 1: Run a web application in the Docker Container on a standalone Linux Box. Connect to your docker hub, create repository and much more.
Scenario 2: Run the same web appliction using ECS.
You must be able to meet the following pre-requisites:
- Be able to setup & Login to a EC2 linux server with a public ip address. Follow the instructions hereif you are new to AWS
- Create a docker hub account atcloud.docker.com
Docker in a standalone Linux Box:
- Setup the amazon custom Linux server and install docker: https://docs.docker.com/engine/installation/
- Create a dockerfile with the necessary contents. Learn more about working/creating with Dockerfile here: https://docs.docker.com/engine/reference/builder/
- Preferably save the dockerfile in a known path (/home for UNIX; Documents for Windows)
RUN apt update
RUN apt install – y apache2
RUN echo “Welcome to port 81 site on Docker Container” > /var/www/html/index.html
CMD /usr/sbin/apache2ctl -D FOREGROUND
- In the CMD (Shell for UNIX & PowerShell for Windows) navigate to the dockerfile location.
- Build an image from the docker file using the command
docker build -t Imagename . ( don’t miss the . at the end. Used to look out for the dockerfile )
- After successful image creation run the docker image using the command
docker run -d -p containerportnumber:hostportnumber Imagename
- Check if the image is running by using the command
- Once the image is ready, store the image by pushing it to the dockerhub. Tag the image appropriately before pushing to dockerhub.
First get the image id using the following command:
Second add a tag to the docker image using the dockerhub account id as below.
docker tag f77aa3cb762f gopinathr/testserver
(Here f77aa3cb762f is my image id and gopinathr is my docker id & testserver is my imagename)
- Login to your docker hub account using the command. Enter your username and password when prompted
- Now that tag is created and account login completed, we can push the image to the docker repo using the below comman
docker push gopinathr/testserver
- Once the image is pushed we can then visit our docker hub page to verify
- To pull the image to any docker container simply use the command:
docker pull gopinathr/testserver
- By default the repo will be public as you can see from the above picture, login to your docker hub account to make it private(if needed). Only one private repo is free.
Docker + AWS = ECS
Amazon EC2 Container Service (ECS) is a highly scalable, high performance container management service that supports Docker containers and allows you to easily run applications on a managed cluster of Amazon EC2 instances.
1. To setup docker through ECS, make sure the latest version of aws cli is installed on the linux host using the command
pip install awscli –upgrade –user
2. Post update, configure the awscli using aws configure command. Enter the Access Key and Secret Key, Default region name and Default output format.
3. Run the command aws ecr get-login –region <regionname>. This will provide the login information for docker
4. Copy & run the command from the linux shell. This will get the docker login setup. If you get any error remove -e none in the docker login information as per the screenshot below and run the login command again.
5. After successful login, create an ECS repository from the AWS ECS console and make a note of the URI.
6. Similar to the earlier setup, tag the image using the repo name using the command below
docker tag image id ecs-repo-name]
7. Push the image to ecs repository using the below command
docker push ecs-repo-name
- the repository URI can then be used as pull the images to the ECS containers hosted in AWS
docker pull 502433561161.dkr.ecr.us-west-2.amazonaws.com/497718-repo
Now that ECS repository is created, we can now create a ECS cluster to pull the docker image.
9. ECS has three components
- Create a ECS cluster with an active running EC2 instance
- Create a Task definition and add a container
- Create a service.
Step 1 is straight forward and its similar to creating an ec2 instance. Lets create a task definition and add container
Add a container as per the following parameters. Host Port and container port numbers are as per the requirement. Also based on the port number mapping the SG need to be updated accordingly.
- Task definition only contains what need to be done. Service makes the task run. Create a service by navigating to the ECS cluster. Leave the rest of the settings at defaults
In this service creation step, ELB & AutoScaling can be setup as required. Click create
- Access the public ip of the EC2 linux Host Viola!!!! Our Website in EC2 docker container is ready for action.