Docker Basics with some of its Commands and how to Install Docker by AWS
Tags: docker, aws, cloud, linux
Published: 2023-08-18 23:10:49

**Outline**
What do we mean by Docker?
How Docker works
Docker properties
Why use Docker
Some important terms in Docker
Install Docker on an Amazon Web Services (AWS) instance
Some basic commands for Docker



**What do we mean by Docker?** 

Docker is an open-source platform used to build, run and manage applications inside containers. Where containers represent an isolated and lightweight environment that contains all the components needed to run a specific application, including source code, settings, libraries, tools, and the file system, and this makes them very similar to virtual machines (Virtual Machines), but they work more efficiently and quickly. Docker technology enables developers to uniformly develop and port applications between development, test, and production environments, reducing compatibility and dependency issues on specific environments. Developers can define the architecture of their applications using configuration files that define the required environment and settings, and then use Docker to create containers based on these definitions.

And it downloads to your operating system, be it Windows, Linux, Mac, or any other operating system
So, Docker is a tool through which containers are controlled.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdo3jkcinawkknke58da.png)



**How Docker works**

Docker works by providing a standard way to run your code. Docker is an operating system for containers. Similar to how a virtual machine virtualizes (removes the need to directly manage) server hardware, containers virtualize the operating system of a server. Docker is installed on each server and provides simple commands you can use to build, start, or stop containers.
AWS services such as **AWS Fargate**, **Amazon ECS**, **Amazon EKS**, and **AWS Batch** make it easy to run and manage Docker containers at scale.


**Docker properties**

- Each application that runs in the container will work in isolation from other applications in the rest of the containers, as it will be as if each application is running on an operating system alone, but the truth is that it is one operating system and all applications in the containers share it, and the Docker engine is the one that performs the management currency.

- More than one container can run on a single operating system and is run by the Docker engine.

- Applications can be run on Docker through containers on a private laptop, on a server, or on the cloud.



**Why use Docker**

Using Docker lets you ship code faster, standardize application operations, seamlessly move code, and save money by improving resource utilization. With Docker, you get a single object that can reliably run anywhere. Docker's simple and straightforward syntax gives you full control. Wide adoption means there's a robust ecosystem of tools and off-the-shelf applications that are ready to use with Docker.

**- Ship more software faster**
Docker users on average ship software 7x more frequently than non-Docker users. Docker enables you to ship isolated services as often as needed.

**- Standardize Operations**
Small containerized applications make it easy to deploy, identify issues, and roll back for remediation.

**- Seamlessly move**
Docker-based applications can be seamlessly moved from local development machines to production deployments on AWS.

**- Save money**
Docker containers make it easier to run more code on each server, improving your utilization and saving you money.



**Some important terms in Docker**  

**Image:**
It is a template of code that cannot be modified. It is manufactured by other people or companies, such as Odoo, sonarqube, Ubuntu, Nginx, and others. It contains some instructions on how to create a Docker container through it. 
**The Docker image** is all the things you need to run the application and it is light and small in size and easy to use because it contains only what the application needs.

**Docker container:** 
It is a piece of software, such as icons, files, etc., that you need to run your application, and a container is an image that runs. 

**Registry:** 
It is a repository or registry that contains all Docker images The Docker Hub (https://hub.docker.com/) is a repository for all images created and supported by Docker and is available for anyone to use.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6bqia86lc7a3fki9rc0p.png)


**Client:**
It is the primary means by which a user can interact with Docker to run commands, for example, pull, build, run, and others.
Which is a graphical interface, CLI, or something else, Docker uses an API to execute instructions or commands.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rkvvtr7n8qgr5ss23hnc.png)


**Docker demand:**
It is a listener that waits for the command to be executed
If the image is on your local device, it creates a container from it, and more than one container can be run from the same image
If the image is not present, he will go to the registry ( Docker Hub ) and pull it from there.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6uzgbty3qezd8zmxx3cx.png)


**Namespace:**
Docker uses a technology called namespaces to provide the isolated workspace called the container, when you run a container, Docker creates a set of Namespace for that container. These Namespace provide a layer of isolation. Each aspect of a container runs in a separate namespace and its access is limited to that namespace.


**Docker tag:**
It is the address or reference to the docker image
More than one tag can refer to the same image. Docker images can contain multiple tags pointing to the layers of the same image, allowing you to easily
manage versions and references. Each image has a tag, so when the image is pulled without any tag, it pulls the latest. When specifying the tag, it removes the delimiter from it, for example, redis:alpain.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2a1re0ekdbt2wpkgfntw.png)


**Image layers**
Any change that happens or is made to the image is called a layer, which means that every command you specify (from, RUN, COPY, etc.) is a layer, and the sum of all layers is called a Dockerfile.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzyi0jfbfww5ty4c64wj.png)


**Dockerfile:**
Is a text document that contains all the commands a user could call on the command line to assemble an image.
Dockerfile can build images automatically by reading the instructions from a Dockerfile.


**Install Docker on an Amazon Web Services (AWS) instance** 

To install Docker on an Amazon Web Services (AWS) instance, you can follow these steps: Log in to the AWS Control Panel:
-Start logging in to your AWS account through the Control Panel (AWS Management Console).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lswbfmeczdf4716nqjaj.png)

**Create an EC2 instance:**
- Go to the EC2 section of the Control Panel.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bhujsy0zetuk483rh06.png)

- Click on "**Launch Instances**" to start the EC2 instance creation.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rke5bn3m6j0v3nli98lu.png)

**Name and Tags, AMI selections:**

-Give the instance a name.
-Choose Ubuntu (**free tier eligible**) or another operating system that supports Docker as the instance platform.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnvpt37iqadiccpfhq60.png)

**Installation configuration:**
- Choose the instance type (**t2.micro-free tier eligible**) or another type, and specify the required resources (such as processor type and memory size).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjiz5w8d60t4c3s7wvo2.png)

- Choose your SSH key (key pair) if this was done previously, or create a new key and save it somewhere safe.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lenawjij87shg9selfk1.png)

- Follow the required steps until you reach the "Configure Security Group" section. In this section, make sure to configure Allow SSH port (port 22) to access the instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hop98imztnov3jfcwyfb.png)

**Instance launch:** 
- Browse and confirm the settings, then click "Launch instance". Accessing the instance via SSH:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpjuo0uheew1hge8zrem.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dq1yzh7xa3swhn3hlxdn.png)
Successfully launch the instance is done, click on the instance ID.

**Connect to the instance:**
After you launch the instance, you can use your SSH key to connect to it.
First, choose the instance and the click on **connect** `


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w18fgtzy1uvlfewb7qi4.png)




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plx9lq4hhmtbejjbelxt.png)

-In the terminal window, Use the following command:
-`chmod 400 teste.pem` 
Then:
-`ssh -i "teste.pem" ubuntu@ec2-3-110-86-76.ap-south-1.compute.amazonaws.com`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/50hbkxicb21lkpkv5734.png)

Execute the command: 
`Sudo –i` 
"sudo": is short for "Super User Do", which is a command that allows regular users to execute certain commands with root user privileges.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhm7ml714nlypis9nnmu.png)


**Docker installation:** 
After connecting to the instance via SSH and obtaining the root privilege, use the following command automation to install Docker:

`curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" && sudo apt-get update && apt-cache policy docker-ce`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lzji161f956h9oc5oswc.png)

 **Docker experience:** Run a simple test command `docker -v` to check that Docker is working properly and to see the Docker version:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efeh7n91g4xca1bnhamq.png)

**Some basic commands for Docker** 

`docker pull [image name]:tag` 

The docker pull command is used to download Docker images from a specified repository, where: 
- Image name: The name of the Docker image you want to pull. 
- tag: Optional tag (version) of the image. If not specified, it defaults to (latest).       

For example: 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3rssx56wrjp5q04amjq.png)
This command will fetch the latest version of the Ubuntu image from the Docker Hub repository and store it on your local machine.

`docker images` 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9fpxg3shtkm8gpiuc7k5.png)

This command displays a list of all the images on your device with details such as the image's name, tag, and size.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6j67j7nbt4qpioym7nl.png)

 `docker rmi [image name:tag]` 
To remove one image. For example: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkjw43fi43nc5pn1d4vm.png)

And again to display the images after removing by the command `docker images`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyhniik0csmtbbpw1ijy.png)

`docker run [image name]`
The docker run command is used to create and start a new Docker container based on a specified image. It allows you to run a command or process inside the container, and you can customize various aspects of the container's behavior using options.
For example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7o8guurpl5ypy879wtyb.png)


To displays existing containers by the command:
`docker ps –all`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7tskx99zmkw3w06ats90.png)

All containers can be shown even if there is more than one container for the same image by command: `docker container ls –a`

`docker ps –a` 
To list all containers, including both running and stopped containers.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wigqz9vic4s8jloap1do.png)

`docker rm [container id]` 
To remove one or more container 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i3cvwk86gyti1j7fx0ds.png)

Again, run the `docker ps -a` command to show that the container is not in the list and that it has been deleted:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svrdhvbycj9egwr3szh5.png)

You can also specify a custom command to run in the container:

In this case, the `echo "Hello, Docker!"` the command will be executed inside a new container based on the image.
The execution is **Hello, Docker!**

 The `docker run` command can be customized using various options to control aspects such as networking, volumes, environment variables, ports, and more.

 `docker container –d [image]` 
The `-d` helps run the container in the background That is, when the screen is turned off by running the image and we open a new window, it remains present.

`docker inspect [image]` 
The docker inspect command is used to obtain detailed information about a Docker container or image, including metadata, configuration, networking details, and more. It provides a comprehensive JSON-formatted output that can be useful for debugging, troubleshooting, or extracting specific information from Docker objects.

 For example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ygjs7dya8t7rnz1yb1b3.png)

`docker stats [container id ]` 
The `docker stats` command is used to display a live stream of resource usage statistics for running containers. It provides real-time information about the CPU, memory, network I/O, and block I/O usage of each container. This command can be useful for monitoring and understanding the resource consumption of your Docker containers. 

For example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3d932nvtt1ffk5hm74xf.png)


`docker --help` 
Running `docker --help` in the command line will provide you with a summary of the available Docker commands and their descriptions. However, the output might be quite extensive. Here's a simplified version of the output you might see:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uslatr7mwthhe3rslbng.png)

 and other commands ….






References:

https://aws.amazon.com/docker/