Table of contents
- What is Docker, Docker container and Docker Image?
- Docker vs. Virtual Machine
- Docker Hub
- Docker Basic Commands
- Pull Docker Image
- Check All Images
- Run Image
- Check All Running Containers
- Run Docker Images on Detach Mode
- Stop Docker Container
- Check All Docker Container
- Start Previous Running Container
- Binding a container port to the host
- Set Custom Name For Container
- Check Docker log
- Get The Interactive Terminal For Container
- Create Docker Network
- Check Network List
What is Docker, Docker container and Docker Image?
Docker is a containerization platform that allows developers to package applications and their dependencies into isolated units called containers.
A Docker container is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
A Docker image is a read-only template that defines the contents of a Docker container. It includes the application code, runtime, system tools, system libraries, and settings. It is mainly used to create a docker container.
Docker vs. Virtual Machine
Docker and virtual machines (VMs) are both technologies that can be used to run applications in isolated environments. However, they have different approaches and use cases.
Docker is a containerization platform that allows developers to package applications and their dependencies into isolated units called containers. Containers share the underlying operating system of the host machine, but they are isolated from each other and the host operating system.
Virtual machines are hypervisor-based virtualization platforms that allow multiple operating systems to run on the same physical machine. Each virtual machine has its own operating system, CPU, memory, and storage.
Docker Hub
In this section, we will try to Understand Docker Hub. But first, we need to install docker on our local machine. Here is the installation guide.
https://docs.docker.com/engine/install/
After installing docker we can now check the version on our docker engine.
docker -v
Now we will pull a docker image from the docker hub later we will create our docker images. Docker Hub is a cloud-based platform provided by Docker that serves as a registry for Docker images. It allows developers to store, share, and manage Docker container images, which are the packaged applications and their dependencies that can be used to create Docker containers.
We need to create an account in the docker hub.
We will use Redis images from the docker hub. https://hub.docker.com/_/redis. but you can choose whatever images you want to play with.
Docker Basic Commands
We have installed the docker and have basic knowledge about the docker hub. We will use Redis from Docker Hub but you are free to choose any docker image you want.
Pull Docker Image
Now we will pull the image from the docker hub.
docker pull redis
Check All Images
Now we have the docker image for Redis from the docker hub. We can check all available docker images
docker images
Run Image
Let's create a docker container for our Redis image by running the image.
docker run redis
Check All Running Containers
In a new terminal, We can also check all running containers.
docker ps
We can see the container ID and port along with other information. We can stop our running container by control
+ c
Run Docker Images on Detach Mode
We can also run docker images in detach mode.
docker run -d redis
It will only give the container ID in return but our docker image is running.
Stop Docker Container
We need the container_id to stop a Docker which is running on detach mode.
docker stop container_id
Check All Docker Container
After stopping a container, If we need to start it again we need the container id. To check all container id regardless it is running or not.
docker ps -a
Now we have our container ID.
Start Previous Running Container
We will be able to start the container by id or name
docker start container_id/container_name
Binding a container port to the host
By running an image, We are creating a container. Which has a port number but that is not our local machine port. That is the container port. So we need to bind the container port with our local port.
docker run -d -p local_port:container_port redis
in example
docker run -d -p 3000:8080 redis
Here, we are binding our local port 3000 with the container port 8080. So our container port will be 8080 which will be accessible by. our local port 3000.
By using port binding we will be able to run as much as containers from the same Redis image just by defining different port numbers.
Set Custom Name For Container
If you check, you will find every docker container has a random name. If you want set a name for your container
docker run -d -p 3000:8080 --name redis-demo redis
Now our container name is redis-demo
. We can use it instead of the docker container ID.
We can stop the container by using the container name
docker stop redis-demo
Also, start the container
docker start redis-demo
Check Docker log
To check everything about our container we can check the log of that container
docker logs container_id/container_name
Get The Interactive Terminal For Container
To check a running container or interact with it
docker exec -it container_id/container_name /bin/bash
Create Docker Network
Docker networks allow containers to communicate with each other and with the outside world. By default, containers are connected to a bridge network, which provides them with access to the host machine's network. However, you can also create custom networks to control how containers communicate.
docker network create mongo-network
Check Network List
docker network ls