Now since we have understood the basic architecture of Docker in my previous tutorial titled “Docker: Understanding Docker Architecture and Components”, lets now learn how to install Docker and run some basic commands.
- For our demonstration, we will be using centos-07.
- We will be using 1 machine for our lab with the below IP details:
3. The memory should be at least 2 GB and there should be at least 2 core CPU.
Please perform the below steps:
- Set the hostname and update /etc/hosts file.
2. Run below Docker installation script. This script will do the installation for you.
3. Once the docker has been installed, add the “vagrant” user to be part of the “docker” group.
4. Validate the docker version:
5. Now “enable” and “start” the docker service.
That’s it. Our docker has been installed, configured, and now ready for use !!
Understanding Basic docker commands:
The First thing we are going to do is to run the “docker run hello-world” command.
This command tries to find the “hello-world” image locally and if not found, it then downloads an image from the docker hub and runs the container out of this image.
You can explore docker hub by visiting the following URL “https://hub.docker.com/”
“docker images” command shows that the image has been downloaded.
“docker ps”command shows that there is no currently running container:
Use “docker ps -a” to show all the containers including stopped containers.
Now, what if you want to download an image, run it, and log in to the container? The below command just does the same.
It will try to find the image “centos:latest” locally, if not found will download the image from docker hub, run it, and then goes inside the container:
“docker images” command shows that the centos image has been downloaded.
Now, what if you want to create the container, and execute some command inside it without actually going inside it? The below command does the same.
“docker images” command shows that the “ubuntu” image has been downloaded:
“docker ps -a” command shows that the “hostname” command was executed for this container.
Understanding Docker pull and Docker exec command:
In the earlier section, we have used the “docker run” command to download the image from the docker hub and running the container out of it.
But, what if, you just want to download the image and don’t want to run it as a container as of now. Use the “docker pull” command instead.
Run the “docker ps” command. You will see no container is currently running.
Run “docker run -it -d centos” command. This will run the container out of the centos image and run it in the background. docker ps command shows that the container is running.
Now, what if you want to go inside this running container. For this, use the docker exec command instead. Type exit command to exit from container:
Stopping a Running Container
Use the “docker stop” command to stop a running container. let’s see how it works:
Run the “docker ps” command to get the running containers. Now use docker stop <container_id> command to stop the container.
“docker ps” command now shows that no container is currently running. Running the “docker ps -a” command shows that the container has been stopped 10 seconds ago.
Note: Sometimes you might have to deal with hung containers. These containers might not get stopped by using the “docker stop” command. In those scenarios, use the “docker kill <container_id>” command instead.
Use “docker rm <container_id” to remove any container. What we need to note is that we can’t remove the running container. The container needs to be stopped before we remove it.
Below “docker rm” command throws an error when we try to remove a running container:
You can use the “docker rmi <image_name>” command to Delete an Image.
Note: In order to remove an image, you first need to remove the associated containers which were created from this image.
The below example demonstrates the same.
That concludes our tutorial for Docker installation and understanding basic docker commands.
Here’s How TOP 1% Of Marketers Make Profits
Did you know… that Online Learning is the quickest growing market in the education industry with a whopping 900% growth rate since 2000?
You’ve already left a lot of money on the table (and are leaving even more with each passing day) if you haven’t tapped into this market.
Click here to see how the TOP MARKETERS are doing it. https://bit.ly/Generate_Income