Getting Started With Docker
Heard of docker many times? Not getting what docker is? Don’t worry, we will walk you through what docker is and how it works.
What is Docker?
“Docker containers wrap up a piece of software in a complete filesystem that contains everything it needs to run: code, runtime, system tools, system libraries – anything you can install on a server. This guarantees that it will always run the same, regardless of the environment it is running in” – https://www.docker.com/what-docker
Cut The Crap
In general, docker is a tool designed to create, run and deploy your application with the help of containers.
Now the question comes, what the hell “containers” are?
Containers are running instance of docker images and docker images are the immutable/frozen file which is a snapshot of containers and are generated by the build command in docker.
Assume image as the class and container as the object/instance of that class.
What is Docker engine?
Docker engine is a part of Docker which creates and run docker containers.
How Docker is different from Vagrant?
Vagrant is a tool to create configure lightweight, reproducible and portable development environment.
Vagrant works on Virtualization whereas Docker works on Containerization
Virtualization – Each virtual machine runs its own operating system with the allocated resources (CPU, RAM) and environment provided by the program “Hypervisor”. The benefit of using it is you get almost a complete separation from Host. The drawback is that you need to allocate some fix amount of resources (CPU, RAM) and the Hypervisor itself eats up some resources.
Containerization – It allows us to run application in an isolated partition of a single linux kernel running directly on the physical hardware. Linux cgroups and namespaces are used to isolate, secure and manage the containers. The benefit is higher performance since there is no hypervisor involved and the container can use the amount of resources it needs.
Wait!!! Did I just say, Docker uses linux kernel??? then how can you use it on windows/mac?
Well, you can actually use docker on mac/windows also. When you install it on mac, Docker bundles a tiny linux VM (25MB on disk) which acts as a wrapper for the container. This means you can directly use the docker command without any problem. Hence docker can run on windows and mac too.
Which one should You use?
The simple answer is – If you want to manage machine, use vagrant (example – Cloud infrastructure) and if you want to manage and run application use docker (example – An ROR application).
Docker is available for MAC, Windows and Linux distributions:
Basic Docker Commands
- Docker version – docker –version
- Docker compose version – docker-compose –version
- Docker machine version – docker-machine –version
- Verify docker is running – docker run hello-world
- List running docker containers – docker ps
- List stopped docker containers – docker ps -a
- List images – docker images
Let’s try out some command:
To check information about the docker installed, run docker info. To check if docker is installed properly run docker run hello-world
Let’s spin up a dockerized webserver.
docker run -d -p 80:80 –name webserver nginx
If docker doesn’t find this image locally then it will pull it from Docker Hub. Go to http://localhost and check if nginx is running or not.
Run docker ps to see the details of webserver container
This nginx webserver will continue to run unless we stop it. To stop it run docker stop webserver and to start it back run docker start webserver
To stop and remove the container run docker rm -f webserver. This will simply remove the container but will keep the nginx image which can be seen via docker images. This is because you might want to keep some docker images so that you don’t need to pull them again if you want to use it again.
However, if you want to remove the image as well then run the command docker rmi nginx
Next Part – How to run Rails application with Docker.