Docker Fundamentals : Hands On - I

Docker Fundamentals : Hands On - I


Setting Up Docker on AWS EC2 (Ubuntu)


Step 1: Update the Package List

sudo apt-get update        

  • Purpose: Updates the package list on your system to ensure you have the latest information about available software and updates.

sudo apt-get upgrade        

  • Purpose: Upgrades all installed packages on your system to their latest versions based on the updated package list from the repositories. This ensures your software is up-to-date without adding or removing packages.


Step 2: Install Docker

sudo apt-get install docker.io        

  • Downloads and installs the docker.io package, which contains the Docker Engine.
  • Configures Docker to run as a service that starts automatically.


Step 3: Add User to Docker Group

sudo usermod -aG docker ubuntu && newgrp docker        

  • Grants non-root users access to Docker commands by adding them to the docker group and activates the changes immediately.
  • 𝐬𝐮𝐝𝐨 𝐮𝐬𝐞𝐫𝐦𝐨𝐝 -𝐚𝐆 𝐝𝐨𝐜𝐤𝐞𝐫 𝐮𝐛𝐮𝐧𝐭𝐮: Adds the user ubuntu to the docker group, allowing Docker commands to run without sudo.
  • 𝐧𝐞𝐰𝐠𝐫𝐩 𝐝𝐨𝐜𝐤𝐞𝐫: Refreshes the user session to apply group changes without requiring a logout.


Step 4: Verifying the Installation

  • Check the Docker version:

docker --version        

  • Run a test container:

docker run hello-world        


Article content



Common Docker Commands


1. docker pull

  • Purpose: Downloads a Docker image from a registry (e.g., Docker Hub).
  • Syntax:

docker pull <image_name>        


2. docker images

  • Purpose: Lists all images present locally.
  • Syntax:

docker images        


3. docker ps

  • Purpose: Lists all running containers.
  • Syntax:

docker ps        


4. docker ps -a

  • Purpose: Lists all containers, including stopped ones.
  • Syntax:

docker ps -a        


5. docker run

  • Purpose: Creates and starts a new container from an image.
  • Syntax:

docker run <image_name>        


6. docker run -d

  • Purpose: Runs a container in detached mode (in the background).
  • Syntax:

docker run -d <image_name>        



Hands-On: Setting Up a MySQL Container with Docker


Step 1: Pull the MySQL Docker Image

docker pull mysql:latest        

  • Purpose: Downloads the latest version of the official MySQL image from Docker Hub.
  • What Happens: The command fetches the MySQL image and stores it locally on your system.


Verify the Download

docker images        

  • Purpose: Lists all images downloaded to your system.
  • What Happens: Displays the image name, tag, ID, and size.


Article content


Step 2: Run the MySQL Container

docker run -d -e MYSQL_ROOT_PASSWORD=password mysql:latest        

  • Purpose: Creates and starts a MySQL container in detached mode.

Explanation of Options:

  • -𝐝: Runs the container in detached mode (background).
  • -𝐞 𝐌𝐘𝐒𝐐𝐋_𝐑𝐎𝐎𝐓_𝐏𝐀𝐒𝐒𝐖𝐎𝐑𝐃=𝐩𝐚𝐬𝐬𝐰𝐨𝐫𝐝: Sets the root password for MySQL inside the container.
  • 𝐦𝐲𝐬𝐪𝐥:𝐥𝐚𝐭𝐞𝐬𝐭: Specifies the image and tag to use.


Verify the Container Is Running

docker ps        

  • Purpose: Lists all currently running containers.
  • What Happens: Displays container IDs, names, statuses, and ports.


Article content


Step 3: Access the Running Container

docker exec -it <container-id> bash        

  • Purpose: Opens a terminal session inside the running container.

Explanation of Options:

  • 𝐞𝐱𝐞𝐜: Executes a command inside a running container.
  • -𝐢𝐭: Opens an interactive terminal session.
  • <𝐜𝐨𝐧𝐭𝐚𝐢𝐧𝐞𝐫-𝐢𝐝>: Replace with the ID of your running container (from docker ps).


Access MySQL CLI Inside the Container

  • After running the docker exec command, log into MySQL:

mysql -u root -p        

  • Enter the root password (password).
  • Test the connection by running:

show databases;        


Article content



Hands-On Guide: Setting Up NGINX with Docker


Step 1: Pull the NGINX Docker Image

  • Run the following command to download the latest NGINX image:

docker pull nginx:latest        

  • Verify that the image has been downloaded:

docker images        


Article content


Step 2: Run the NGINX Container

docker run -d -p 80:80 --name my-nginx nginx:latest        

Explanation:

  • -𝐝: Runs the container in detached mode (in the background).
  • -𝐩 80:80: Maps port 80 on the host to port 80 in the container.
  • --𝐧𝐚𝐦𝐞 𝐦𝐲-𝐧𝐠𝐢𝐧𝐱: Assigns the name my-nginx to the container.
  • 𝐧𝐠𝐢𝐧𝐱:𝐥𝐚𝐭𝐞𝐬𝐭: Specifies the image to use.


Confirm that the container is running:

docker ps        


Article content

Step 3: Access NGINX Using the EC2 Public IPv4 Address

  • Open a browser and navigate to http://<Public_IPv4_Address>


Article content













To view or add a comment, sign in

More articles by Rishabh Sharma

  • Docker Volumes

    Docker volumes let you save data used by your containers, so it doesn’t disappear when the container stops or is…

  • Docker Fundamentals : Hands On - II

    Understanding Dockerfile A Dockerfile is a simple text file containing a set of instructions used to build a Docker…

  • Docker Fundamentals : Part III

    Understanding Docker Engine Docker Engine is the core software that enables the creation, management, and operation of…

  • Docker Fundamentals : Part II

    Understanding Virtualization Virtualization is a technology that allows multiple operating systems (OS) to run on a…

  • Docker Fundamentals : Part I

    Understanding Docker in Layman's Terms Imagine you bake a cake. To do this, you need specific ingredients, tools, and…

    1 Comment
  • Exploring GitHub

    If you are new to Git and Github , check out this blog for understanding git. What Is GitHub? GitHub is a web-based…

  • Version Control System: Git

    1. What is a Version Control System (VCS)? A Version Control System (VCS) is a crucial tool that enables developers to…

  • Understanding Shell Scripting

    Shell scripting is a powerful way to automate repetitive tasks, manage system processes, and handle system…

  • Basics of Shell Scripting

    What is a Shell Script? A shell script is simply a text file that contains a series of commands to be executed by the…

  • Linux System Management Tools: systemctl and find

    Linux system management can be both powerful and efficient when you know how to use the right command-line tools. Two…

Insights from the community

Others also viewed

Explore topics