In our previous tutorial, we have explained **[how to install Docker in Ubuntu][2]** , and how to [**install Docker in CentOS**][3]. Today, we will see the basic usage of Docker. This guide covers the Docker basics, such as how to create a new container, how to run the container, remove a container, how to build your own Docker image from the Container and so on. Let us get started! All steps given below are tested in Ubuntu 18.04 LTS server edition.
### Getting Started With Docker
Before exploring the Docker basics, don’t confuse with Docker images and Docker Containers. As I already explained in the previous tutorial, Docker Image is the file that decides how a Container should behave, and Docker Container is the running or stopped stage of a Docker image.
##### 1\. Search Docker images
We can get the images from either from the registry, for example [**Docker hub**][4], or create our own, For those wondering, Docker hub is cloud hosted place where all Docker users build, test, and save their Docker images.
Docker hub has tens of thousands of Docker images. You can search for the any Docker images with **“docker search”** command.
For instance, to search for docker images based on Ubuntu, run:
```
$ sudo docker search ubuntu
```
**Sample output:**
![][5]
To search images based on CentOS, run:
```
$ sudo docker search ubuntu
```
To search images for AWS, run:
```
$ sudo docker search aws
```
For wordpress:
```
$ sudo docker search wordpress
```
Docker hub has almost all kind of images. Be it an operating system, application, or anything, you will find pre-built Docker images in Docker hub. If something you’re looking for is not available, you can build it and make it available for public or keep it private for your own use.
##### 2\. Download Docker image
To download Docker image for Ubuntu OS, run the following command from the Terminal:
```
$ sudo docker pull ubuntu
```
The above command will download the latest Ubuntu image from the **Docker hub**.
You can also download a specific version of Ubuntu image using command:
```
$ docker pull ubuntu:18.04
```
Docker allows us to download any images and start the container regardless of the host OS.
For example, to download CentOS image, run:
```
$ sudo docker pull centos
```
All downloaded Docker images will be saved in **/var/lib/docker/** directory.
To view the list of downloaded Docker images, run:
```
$ sudo docker images
```
**Sample output:**
```
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 7698f282e524 14 hours ago 69.9MB
centos latest 9f38484d220f 2 months ago 202MB
hello-world latest fce289e99eb9 4 months ago 1.84kB
```
As you see above, I have downloaded three Docker images –**Ubuntu** , **CentOS** and **hello-world**.
Now, let us go ahead and see how to start or run the containers based on the downloaded images.
##### 3\. Run Docker Containers
We can start the containers in two methods. We can start a container either using its **TAG** or **IMAGE ID**. **TAG** refers to a particular snapshot of the image and the **IMAGE ID** is the corresponding unique identifier for that image.
As you in the above results **“latest”** is the TAG for all containers, and **7698f282e524** is the IMAGE ID of **Ubuntu** Docker image, **9f38484d220f** is the image id of CentOS Docker image and **fce289e99eb9** is the image id of **hello_world** Docker image.
Once you downloaded the Docker images of your choice, run the following command to start a Docker container by using its TAG.
```
$ sudo docker run -t -i ubuntu:latest /bin/bash
```
Here,
* **-t** : Assigns a new Terminal inside the Ubuntu container.
* **-i** : Allows us to make an interactive connection by grabbing the standard in (STDIN) of the container.
* **ubuntu:latest** : Ubuntu container with TAG “latest”.
* **/bin/bash** : BASH shell for the new container.
Or, you can start the container using IMAGE ID as shown below:
```
sudo docker run -t -i 7698f282e524 /bin/bash
```
Here,
* **7698f282e524** – Image id
After starting the container, you’ll be landed automatically into the Container’s shell (Command prompt):
![][7]
Docker container’s shell
To return back to the host system’s Terminal (In my case, it is Ubuntu 18.04 LTS) without terminating the Container (guest os), press **CTRL+P** followed by **CTRL+Q**. Now, you’ll be safely return back to your original host computer’s terminal window. Please note that the container is still running in the background and we didn’t terminate it yet.
To view the list running of containers, run the following command:
```
$ sudo docker ps
```
**Sample output:**
```
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
32fc32ad0d54 ubuntu:latest "/bin/bash" 7 minutes ago Up 7 minutes modest_jones
```
![][8]
List running containers
Here,
* **32fc32ad0d54** – Container ID
* **ubuntu:latest** – Docker image
Please note that **Container ID and Docker image ID are different**.
To list all available ( either running or stopped) containers:
```
$ sudo docker ps -a
```
To stop (power off the container) from the host’s shell, run the following command:
```
$ sudo docker stop <container-id>
```
**Example:**
```
$ sudo docker stop 32fc32ad0d54
```
To login back to or attach to the running container, just run:
```
$ sudo docker attach 32fc32ad0d54
```
As you already know, **32fc32ad0d54** is the container’s ID.
To power off a Container from inside it’s shell by typing the following command:
```
# exit
```
You can verify the list of running containers with command:
```
$ sudo docker ps
```
##### 4\. Build your custom Docker images
Docker is not just for downloading and using the existing containers. You can create your own custom docker image as well.
To do so, start any one the downloaded container:
```
$ sudo docker run -t -i ubuntu:latest /bin/bash
```
Now, you will be in the container’s shell.
Then, install any software or do what ever you want to do in the container.
For example, let us install **Apache web server** in the container.
Once you did all tweaks, installed all necessary software, run the following command to build your custom Docker image:
```
# apt update
# apt install apache2
```
Similarly, install and test any software of your choice in the Container.
Once you all set, return back to the host system’s shell. Do not stop or poweroff the Container. To switch to the host system’s shell without stopping Container, press CTRL+P followed by CTRL+Q.
From your host computer’s shell, run the following command to find the container ID:
```
$ sudo docker ps
```
Finally, create a Docker image of the running Container using command: