💖Docker Basics Command💖

What are we going to learn in this Blog?

📌Start a container

📌Stop a container

📌Remove a container

📌Remove images

📌Execute a command inside a container

📌Attach and Detach a container

❤Start a Container

Let’s suppose we have to use a service of a web server let’s say NGINX .So the easiest way to use it is via using container .So how to start the container the first question?

RUN — docker run nginx

fig 1

what will it do ?

This command will search an image of nginx in your docker host, if there will be no image found locally in the host then it will redirect to the docker hub and pull the image locally and run the container .

Just look at the fig 1 first line unable to find locally only then it has gone for the docker hub and successfully pulled the image.

After that just look at the latest tag at the very end. you know what does it mean ?

wait😂for the next blog to learn about the latest tag.

How will you know that what containers are running and what containers have stopped. No idea 😜let me give an idea about this.

Using docker ps command you can see those containers which are running and can also see some of their details.

I know screenshot is quite small, you have to zoom a little bit to see the contents. you will see a container id associated with it, this container id is random. Every container has an different id and we can access container via its id numbers or names. The second is the image name that you have pulled down i.e. nginx. Ignore that command for now . Created shows the time when it was created. Status shows it is in which mode still running or has been excited. Name is also randomly given to a container.

you can access the container via the container name & id

How to see those containers which have already stopped?

RUN docker ps -a to view those exited container along with the running containers .

Stop a Container

Run docker stop <container I.D. or name>

To stop a running container use this command. this command will stop the actions of the running container and exit out of it.

Note if you will stop a container before completing the task then you will observe a different exited code of that.

Normally after exiting it will give a code of zero but when you stop it, it give give you a random code.

Remove a container

Run docker rm <container I.D. or name>

This command will help you remove the container from that active containers or stopped containers. It will be removed completely.

Listing the images

To list down number of images present in your system 💖Run this command

Here you can see every image has a different I.D associated with that. You can see the size too and the tag.

How to remove images

Sometimes you have pulled too many images which is of no use now and it is taking so much of space as of now. So you are planning to remove it from your system then run this command.

Before removing, Delete all dependent containers to remove image.

Run docker rmi <image name or id >

Pulling an image

Let’s suppose you want to download a image but not use it right now to run a container.

Basically as of now you know only how to download an image along with the running using the command docker run <name> .

so what will u do?

RUN docker pull <image name>

it will store the image for the future use

NOW Lets suppose you have to run a container of ubuntu

then we will run the following command

what do you think what will happen ?

yes ,exactly 😜nothing. you know why?

Yep you just got it right. We are using a container not a virtual machine which will host your operating system. Containers are meant to perform a given task. If container will not given any task then it will be get stopped.

So in order to run a container we must give a task to the container otherwise it will get stopped.

Example — Now let’s suppose we have check the etc hosts file of the ubuntu image that we have just installed so what will we do ?

Run this

Here we have given a task to the container so it will only exit after the completion of the task and in this time we can play inside the ubuntu.

Execute a command

Now since we have given a task and now we have to look inside the hosts file configuration of the ubuntu. 😂What will we do?

Run This Command — docker exec <image name> <task name>

Finally we have achieved out task. Now lets move forward🙌👍

Attach and Detach

Normally when you run any docker command it will execute first and until its execution is not completed you cant work further with any other command. Sometimes because of you internet issues command takes time. Let’s say building an image will take so much of time leading to frustration in foreground. So what can we can to avoid this situation?

Solution — Run that container in background not in foreground

use detach mode

We have used -d to make this container work in background and if you want again to come in the foreground. Run this command

This will get back you in normal mode where you have to wait for execution's of any command.

Hands-on experience exercises

There are some sets of questions based on the Docker basics command those you have learned above, now its time to test yourself . so make sure to test yourself before moving to the next blog .Comment on the blog and let me know how much marks you have achieved. In case of any doubts/questions/suggestions feel free to Dm me on Twitter.

  1. Pull an image of Cent OS.
  2. Start the container using Cent OS image.
  3. Stop the container .
  4. Run the container using redis image.
  5. Delete all the images.
  6. Start a container in the background.
  7. See the logs of an ubuntu container.
  8. Create your own image 😂watch the next blog to know how to do it.

Try this all questions to test yourself and let me know

END OF BLOG

Happy learning 😃

Follow us on Twitter To join us in the initiative of learning Devops in 60 days.

see you in next blog, stay happy 😄

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store