top of page

Docker-Day-2

### Project Scenario: Pushing Docker Image to AWS ECR from an EC2 Instance


In this project, you will create a repository in AWS Elastic Container Registry (ECR), configure an EC2 instance for AWS access, and push a Docker image to the ECR repository. The steps include creating a security key, configuring AWS CLI, tagging the Docker image, and pushing it to the ECR.



### Step 1: Create a Repository in AWS ECR

- Explanation : AWS ECR provides secure, scalable Docker container registry hosting. You first need to create a repository in AWS ECR.

- Steps :

1. Go to the AWS Management Console.

2. Navigate to `ECR` under the `Services` section.

3. Click `Create repository`.

4. Enter a name for the repository (e.g., `my-app-repo`).

5. Leave other settings as default and click `Create`.

- Validation : The repository should appear in the ECR console after creation.


### Step 2: Create Security Key (Access Key and Secret Key)

- Explanation : Create access and secret keys in AWS IAM to authenticate your EC2 instance with AWS.

- Steps :

1. Navigate to `IAM` in the AWS Management Console.

2. Select `Users` > Select your username.

3. Go to the `Security credentials` tab and create a new access key.

4. Download and note down the Access Key ID and Secret Access Key .

- Validation : You should have both keys saved for configuring AWS CLI on your EC2 instance.



### Step 3: Configure AWS CLI on the EC2 Instance

- Explanation : AWS CLI must be configured with your access credentials so the EC2 instance can communicate with AWS services.


- Commands :

sudo yum install -y aws-cli

aws configure

- Enter your Access Key ID .

- Enter your Secret Access Key .

- Enter the default region name (e.g., `us-east-1`).

- Enter the default output format (`json` or `text`).


- Validation :

Run `aws sts get-caller-identity`

to confirm that the AWS CLI is correctly configured. The command should return your IAM user or role details.



### Step 4: Authenticate the Docker CLI to AWS ECR

- Explanation : You need to authenticate your Docker CLI to AWS ECR so that Docker can push/pull images to/from the repository.


- Command :

aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <your-account-id>.dkr.ecr.<your-region>.amazonaws.com

Replace:

- `<your-region>` with your AWS region (e.g., `us-east-1`).

- `<your-account-id>` with your AWS account ID.

- Validation : If the login is successful, you will see a message like `Login Succeeded`.



### Step 5: Tag the Docker Image

- Explanation : Before pushing the Docker image to ECR, you need to tag it with the ECR repository URI.


- Commands :

1. List your Docker images to find the image ID:

docker images

2. Tag your image with the repository URI:

docker tag <image-id> <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/my-app-repo:latest

Replace:

- `<image-id>` with your Docker image ID (from the previous step).

- `<your-account-id>` with your AWS account ID.

- `<your-region>` with your AWS region.

- `my-app-repo` with the ECR repository name.

- Validation : Run `docker images` again, and you should see the image tagged with the ECR URI.


### Step 6: Push the Docker Image to AWS ECR

- Explanation : Push the Docker image to the newly created ECR repository.

- Command :

docker push <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/my-app-repo:latest

- Validation : You should see output showing the progress of image layers being pushed. Once complete, the image will be available in the ECR repository. You can verify by checking the AWS ECR console.



### Step 7: Validate the Image on AWS ECR

- Explanation : After the push is complete, validate the image in the AWS ECR console.

- Steps :

1. Go to the AWS Management Console.

2. Navigate to `ECR` and open the `my-app-repo` repository.

3. Check that the Docker image is listed under the `Images` tab.


 

### Project Scenario: Managing Docker Containers Interactively and in Detached Mode


This project involves running a Docker container in interactive terminal detach mode, attaching to it, exiting the container, stopping, starting, and removing it. Each task includes an explanation and the necessary commands for validation.


### Step 1: Run Docker Container in Interactive Terminal Detach Mode

- Explanation : Running a Docker container in interactive terminal mode (`-it`) with the detached flag (`-d`) allows you to start the container in the background and interact with it later.


- Command :

docker run -itd --name my_container ubuntu

- `-it` : Interactive terminal mode.

- `-d` : Detached mode, so the container runs in the background.

- `--name` : Names the container `my_container`.

- `ubuntu` : The image used for the container.


- Validation :

- Run `docker ps` to check if `my_container` is running in detached mode.

docker ps


### Step 2: Attach to the Running Docker Container

- Explanation : Attaching to a running container allows you to interact with it in real-time.


- Command :

docker attach my_container

- `my_container` : The name of the container.


- Validation :

- Once attached, you should be able to see the shell of the running container (for example, `root@<container-id>`).

- You are now inside the container and can run commands interactively.


### Step 3: Exit the Docker Container

- Explanation : Exiting the container closes the session and, depending on the signal, may stop or leave the container running.


- To keep the container running , detach without stopping by pressing `Ctrl + P` then `Ctrl + Q`.

- To exit and stop the container , type `exit`.


- Commands :

- To keep it running:

Ctrl + P, Ctrl + Q

- To stop it by exiting:

exit

- Validation :

- If you used `Ctrl + P` then `Ctrl + Q`, the container will still be running. Check with:

docker ps

- If you used `exit`, the container will stop. Check with:

docker ps -a


### Step 4: Start the Docker Container

- Explanation : After stopping the container, you can start it again with its previous state preserved.


- Command :

docker start my_container

- Validation :

- Run `docker ps` to confirm that the container is running again.

docker ps


### Step 5: Stop the Docker Container

- Explanation : Stopping a container gracefully ends the running process inside.


- Command :

docker stop my_container


- Validation :

- Run `docker ps` to ensure that `my_container` is no longer running.

docker ps


### Step 6: Remove the Docker Container

- Explanation : Removing a container deletes it from the system. The container must be stopped before it can be removed.


- Command :

docker rm my_container

- If the container is still running, stop it first:

docker stop my_container

docker rm my_container


- Validation :

- Run `docker ps -a` to verify that the container has been removed.

docker ps -a


This step-by-step guide will help you manage a Docker container by running it interactively and detached, attaching, exiting, stopping, starting, and finally removing it.


 

### Project Scenario: Running and Managing Docker Containers in Interactive Terminal Mode


In this project, we will cover running a Docker container in interactive terminal mode, checking the operating system inside the container, exiting without stopping, re-attaching, starting, and finally exiting the container. Each task will include explanations and the necessary commands for validation.



### Step 1: Run Docker Container in Interactive Terminal Mode

- Explanation : Running a container in interactive terminal mode (`-it`) allows you to interact with the container directly through a terminal.


- Command :

docker run -it --name my_container ubuntu /bin/bash

- `-it` : Interactive terminal mode.

- `--name my_container` : Assigns a custom name (`my_container`) to the container.

- `ubuntu /bin/bash` : Starts an Ubuntu container and opens a Bash shell for interaction.


- Validation :

- After running the command, you should be inside the container's Bash shell (e.g., `root@<container-id>:/#`).


### Step 2: Check the OS Inside the Container

- Explanation : Once inside the container, you can check the OS and kernel version running inside it.


- Command :

cat /etc/os-release

- This will display details about the operating system running inside the container.


- Validation :

- The output should show information about the Ubuntu distribution.


### Step 3: Exit the Container Without Stopping It

- Explanation : You can exit the container without stopping it by detaching using `Ctrl + P` followed by `Ctrl + Q`. This keeps the container running in the background.


- Command :

Ctrl + P, Ctrl + Q


- Validation :

- Run `docker ps` to confirm that the container is still running.

docker ps

### Step 4: Attach to the Running Container

- Explanation : Attaching to the running container allows you to resume interaction with the terminal inside the container.


- Command :

docker attach my_container

- Validation :

- You should see the same terminal session you left earlier, with the Bash shell prompt inside the container.



### Step 5: Exit the Container

- Explanation : When you are done with the container, you can simply exit the Bash shell, which will stop the container.


- Command :

exit

- Validation :

- Run `docker ps -a` to verify that the container is now stopped.

docker ps -a


This guide will walk you through the key Docker container operations, focusing on maintaining and managing an interactive terminal session.


 

### Project Scenario: Managing Docker Container by ID and Name


This project will guide you through running an Ubuntu container, managing it using both the container name and ID, stopping and starting containers, forcefully removing a running container, and cleaning up all stopped containers. Each task includes a one-line explanation and the necessary commands for validation.



### Step 1: Run an Ubuntu Container

- Explanation : Start an Ubuntu container in detached mode and assign a custom name to it.


- Command :

docker run -itd --name my_ubuntu_container ubuntu

- `-d` : Detached mode, runs the container in the background.

- `--name` : Assigns a custom name (`my_ubuntu_container`) to the container.

- `ubuntu` : The Ubuntu image used to create the container.


- Validation :

- Run `docker ps` to check that the container is running.

docker ps


### Step 2: Find the Container ID and Name

- Explanation : Retrieve the container’s ID and name by listing all running containers.


- Command :

docker ps

- The output will show both the container name (`my_ubuntu_container`) and container ID.


### Step 3: Stop the Container Using the Container Name

- Explanation : Stopping the container using its name gracefully halts its execution.


- Command :

docker stop my_ubuntu_container

- Validation :

- Run `docker ps` to ensure that the container has stopped.

docker ps


### Step 4: Start the Container Using the Container Name

- Explanation : Start the previously stopped container using its name.


- Command :

docker start my_ubuntu_container


- Validation :

- Run `docker ps` to confirm that the container is running again.

docker ps


### Step 5: Stop the Container Using the Container ID

- Explanation : You can also stop the container using its ID, which is a unique identifier assigned to each container.


- Command :

- First, retrieve the container ID (if not already known):

docker ps

- Then stop the container using the ID:

docker stop <container_id>


- Validation :

- Run `docker ps` again to verify that the container has stopped.

docker ps

### Step 6: Start the Container Using the Container ID

- Explanation : Start the container again using its ID.


- Command :

docker start <container_id>


- Validation :

- Run `docker ps` to check that the container is running again.

docker ps


### Step 7: Forcefully Remove a Running Container

- Explanation : You can forcefully remove a running container using the `docker rm -f` command.

- Command :

docker rm -f my_ubuntu_container


- Validation :

- Run `docker ps -a` to ensure the container has been removed.

docker ps -a


### Step 8: Remove All Stopped Containers

- Explanation : This command removes all stopped containers to free up space and maintain a clean environment.


- Command :

docker container prune

- Validation :

- You’ll be prompted to confirm the removal of all stopped containers.

- Run `docker ps -a` to verify that all stopped containers have been removed.



This project walks you through managing a container using both name and ID, stopping, starting, removing containers, and cleaning up the environment.


 

### Project Scenario: Running NGINX in Docker on EC2 and Accessing it Externally


#### 1. Run NGINX in Detached Mode

docker run -d --name my_nginx nginx

- Explanation : This starts an NGINX container in the background (detached mode).


#### 2. List the Running Containers

docker ps

- Explanation : This displays all running containers, showing the container ID, name, and exposed ports.


#### 3. Find the IP Address and Port of the NGINX Container

docker inspect my_nginx | grep "IPAddress"

docker inspect my_nginx | grep -A 5 "Ports"

- Explanation : Use `docker inspect` to get details of the container, including the IP address of the running container.


#### 4. Curl the IP Address of the Container

curl <container_ip>

- Explanation : This sends a request to the NGINX container's internal IP to verify if it responds correctly.


#### 5. Run NGINX with Port Forwarding to EC2 Default Port

docker run -d -P --name my_nginx_port nginx

- Explanation : This command automatically maps the container’s internal port 80 to a random port on the EC2 instance’s host.


#### 6. Inspect the NGINX Container to Verify the Port Mapping

docker inspect my_nginx_port

- Explanation : This shows details about the container, including the port mapping (e.g., `0.0.0.0:32769->80/tcp`).


#### 7. Open the NGINX Port on EC2 Instance

- Explanation :

- Go to your AWS Management Console, open Security Groups .

- Edit the Inbound Rules to allow traffic on the port mapped from Docker (as seen in `docker ps` or `docker inspect`).

- Add a new rule allowing `HTTP` traffic on port 80 (or the forwarded port).

- Apply the changes to the EC2 instance security group.


#### 8. Open the NGINX Page in Your Web Browser

- Explanation : Open a web browser on your laptop and enter the following:

http://<ec2-public-ip>:<mapped-port>

- This should show the default NGINX welcome page, verifying that NGINX is running and accessible via your EC2 instance.


 

### Project Scenario: Exposing NGINX on EC2 to Port 9090 Publicly and Accessing It


#### 1. Run NGINX and Expose Port 9090 Publicly

docker run -d -p 9090:80 --name my_nginx_1 nginx

- Explanation : This starts an NGINX container and forwards container's internal port 80 to port 9090 on the EC2 instance (host machine).


#### 2. Inspect the Container to Verify Port Mapping

docker inspect my_nginx

- Explanation : This command provides detailed information about the container, including the mapped ports (e.g., `9090:80`).


#### 3. Find the Mapped Port on the EC2 Instance

docker ps

- Explanation : This command lists running containers, showing the mapped ports between the EC2 host and the container (e.g., `0.0.0.0:9090->80/tcp`).


#### 4. Open Port 9090 in the EC2 Security Group

- Explanation :

- Go to AWS Management ConsoleEC2 DashboardSecurity Groups .

- Select the security group attached to your EC2 instance.

- Edit Inbound Rules and add a new rule:

- Type: Custom TCP

- Protocol: TCP

- Port Range: 9090

- Source: Anywhere (0.0.0.0/0) for public access, or specify your IP address.

- Apply the rule.


#### 5. Access NGINX in a Web Browser

- Explanation : Open a browser on your laptop and access NGINX by typing:

http://<ec2-public-ip>:9090

- This should display the default NGINX welcome page, verifying that the container is running and exposed on port 9090.


 

bottom of page