Getting Started with Docker
According to the Docker's website, "Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications."
In simple words, it's one of the methods used to run and deploy your software application. Docker allows you to create lightweight "virtual machines". Here lightweight virtual machines are nothing but Docker containers.
Docker comes in two pieces:
- Docker engine: You need to install this in your system. Docker engine runs your application.
- Docker hub: It is a website and cloud service that allows everyone to share their Docker images.
- Easily portable and hence good for distributed environment.
- Very less overhead compared to traditional VM.
- Doesn't require extensive resources like VMs.
Docker engine provides a virtual environment required by your application. It does not require computing resources to duplicate virtual hardware in guest OS unlike VMs that use computing resources of host OS.
Containers are where your application runs. You can create a container from the images (either created from scratch or pulled from the Docker hub) and then modify the image. It is an equivalent of creating VMs from a snapshot but way lighter than traditional VMs.
For example, you can pull an Ubuntu image from the Docker hub, modify it by installing Jenkins and your app with all of its dependencies. Then you can create a container from that image, which runs your app whenever it starts.
Docker allows you to expose specific ports of a container from where it can expose its services. Containers have one big difference that separate them from VMs, they are designed to run a single process and don't simulate the complete environment.
docker run [OPTIONS] IMAGE [COMMAND] [ARGUMENT]
Let's pull an image from the Docker hub and create a container using that image.
Pull an image:
docker pull IMAGE:tag
Run a container
List out all the containers
docker ps //list all active containers.
docker ps -a //list out all the containers.
Images are essentially the way you save the state of your container. Images on Docker are like snapshots of VMs and yet again they are much more lightweight!
To create an image, you can take one image and modify it to create a child image. This can be done either through a file (Dockerfile) that specifies a base image and required modifications, or live by "running" an image, modifying it and committing it.
To list out all the images, use command,
Docker images have intermediate layers that allow each layer to be cached, hence allowing reusability and speeding up the Docker build process.
ssh into Docker
To access the container, you can use the docker exec command
docker exec [option] $container_ID/$container_name [command]
The above command will create a new bash session in the running container. You can now install new stuff into this container and take a snapshot of this container by creating an image.
Modifying Existing Image
After accessing the container with the Docker exec command, you can install packages or set environment variables inside the container
Here inside our container we are installing the vim.
When you are done modifying your container you must exit by running the exit command. Once you exit the container, you need to find the container ID by running:
docker ps -a
Commit the changes
docker commit [option] $container_ID new_name:tag
Now doing Docker images will show your new image test vim
You cannot remove a running container. Therefore, you first need to stop the container and then remove it using the following command:
docker stop $container_ID
docker rm $container_ID
docker rmi $image_ID