What is Docker?
A platform for building, launching, and delivering applications in a consistent manner.
Configure once and run it on any machine where the configuration part will be handled by Docker, and you don’t have to configure it again and again for different machines.
Containers
After installing the dependencies, it runs in an isolated environment called containers. This isolated environment allows multiple applications to use different versions of any software side by side.
For example, if there are two applications that both use Node, but with different versions. They can run side by side in an isolated environment without interfering with each other.
Virtual machines
A virtual machine is an emulation of a machine/physical hardware. Using a hypervisor, we can run multiple virtual machines on a real physical machine.
The hypervisor is the tool we used to create and manage virtual machines. There are many hypervisors available, such as VirtualBox and VMware, which are cross-platform. It helps us to run applications in isolation in a VM and on the same physical machine we can have another VM running a completely different application.
So why don’t we use virtual machines instead of containers?
Virtual machines vs containers
Because virtual machines come with certain problems. Let’s discuss them. Each virtual machine needs a full copy of the operating system. It is slow because it mimics our computer. Each virtual machine uses real physical hardware resources.
Let’s talk about containers. Containers also provide us with the same kind of insulation. They are lightweight and do not need a full operating system. It uses the host’s operating system.
Since the operating system has already started on the host, the container will start quickly. It also does not use any hardware resources like CPU, memory, etc. Due to this, we can run many containers side by side based on requirements.
Docker architecture
Docker uses a client-server architecture. Where the client component communicates with the server component using a quiet API, over UNIX sockets or a network interface.
All containers on a host share the host kernel with respect to the operating system. For example, Linux containers run on a Linux operating system, Windows containers run on a Windows operating system, and so on.
Installing Docker
visit https://docs.docker.com/get-docker/ and follow the guide to install Docker on the appropriate platform i.e. Windows, Linux, or MAC. However, check the system requirements before installing.
Commands to install docker on Ubuntu
Install docker
- sudo apt-get update - sudo apt install docker.io - sudo snap install docker - docker -v
To run/check docker images
- sudo docker run docker-image-name - sudo docker images
List docker images
- sudo docker ps -a— List all images - sudo docker ps — List running images
Once Docker is installed. We need to verify this and run the following command. This command will list all the options available for the dockable panel that you can use.
$ docker
In case, you see the following error or something similar.
'docker' is not recognized as an internal or external command, operable program, or batch file.
make sure you followed the installation steps correctly and then continue. Then run the following command to check which version you have installed on your system. This command will give you all the detailed information about the installed version of Docker.
$ docker version
Development Workflow
We take an application, regardless of the technologies it uses, and dockerize it.
How?
By simply adding a Dockerfile that contains the instructions that Docker then uses to package the application into an image. This image meets all the requirements the application needs to run.
We can push the image to Docker Hub just like Github and then we can put it on any machine from Docker Hub and run it virtually because it contains all the specific dependencies required by the application to run.
Storage: Push and pull container images.
Working with Docker
Open a terminal and enter the commands below
First, make sure you have installed vscode and node
$ mkdir docker-app $ cd docker-app $ code .
Create an index.js file and paste the code below
console.log(“My First Docker App”)
In the terminal, run the command below to run the index.js file.
$ node index
Create a Dockerfile at the same level where you created the index.js file and add the Docker extension to vscode as shown in the image below
Filename: Dockerfile [without any extension]
Copy the code below into Dockerfile and save it
FROM node:alpine COPY . /index WORKDIR /index CMD node index.js
Let’s create our docker application
sudo docker build -t docker-app .
How we created the image but it is not visible in our working directory. To check this, run the following command
$ sudo docker image ls
Run the docker-app image on our development machine.
$ sudo docker run docker-app