Before starting, I assume that you know the following topics:
What is Docker? (Its a Containerization Technology.)
What is Containerization? (It is a virtualization method that uses single OS to power multiple distributed application.)
Why to use Docker? (So that we can manage dependencies and configuration needed for different application.)
If you understand above points, then you are ready to go with Docker commands; else, please check the following link: What is Docker?
Now, let's run through the docker commands using question-answer paradigm for easy understanding.
1 What should be our first command?
A: We should Check if the docker service is up and running. For starting docker service in Linux, use:
$ service docker start
2 Now, the service has started, so can we create a container now?
A: Yes, indeed! For creating container, you will need an image. you can search the required image at dockerhub. In this tutorial, we are using "busybox" image. So, in search box type 'busybox'. The first result will be official image. Click on that image, you will see tags section there. For this we will be using '1.31' as a tag. So, the structure of our command would be like:
$ docker run [Image Name]:[Tag]
For our example, it would be:
$ docker run busybox:1.31
3 Is our container ready? If yes, how can we validate that?
A: Yes, our container is ready and we can validate that using following command:
$ docker ps -a
What it will do is, it will print all the container that we created till now. In your case it will be only one and that is created from busybox image. Now in output you will get dome info about your newly created container like container ID, Image used, is container up and running or not? etc.
4 So, whats next we created a container, but can we know more about our container like, what is IP and other stuff?
A: Yes, we can see that using following command. It will be of prime importance when we will be using environmental variables, networking and Volumes:
$ docker inspect [container ID]
What are you asking, where can we get this [container ID]? Yes, I here you, You can get it from the output of command from 3rd question. see, its the first field. Just copy and paste that ID in this command.
5 Enough about containers,how can I check if the image is locally present or not?
A: Yes, you can check that with following command:
$ docker images
6 OK, that's good. But what if I want to assign a port to the container or give name of my choice?
A: You can do that too. This can be done by passing the so called arguments to the "docker run" command. Let's have some light on that too. The first argument we want to know about is detach mode of container. With this you don't have to stay on that particular container and still your container will be up and running in background. The argument you pass to the 'docker run' command is "-d". Let's have an example:
$ docker run -d busybox:1.31
You can also assign a destination port to your container like this:
$ docker run -p [dest_port]:[source_port] [image]:[tag]
You can also give specific name to your container with --name argument, like:
$ docker run --name bbapp busybox:1.31 //now your container name is bbapp.
You can also start your container in an interactive mode with -it tag. Have a try:
$ docker run -it busybox:1.31
7 Last but not least, when there will be much more containers to handle, we will need to remove some containers. we can do that with following command:
dokcer rm [container1_id/name]....[containern_id/name]
$ docker rm bbapp
Conclusion: Okay, that's it for this article. In next article we will start with how to create our own images by using images from docker hub: With and without dockerfile. Thanks for a read.
Claim your page on DEV before someone else does
Level up every day