Niharika Goulikar
Posted on August 12, 2024
Docker is a containerization platform. It helps in packaging our application and its dependencies into a container that can be used across multiple environments.
Why do we need docker?
The way software applications and their dependencies are installed on one machine can differ from how they're installed on other machines. To ensure consistent behavior across different platforms, containerization tools (docker being one such) are used.
Installing docker on your machine-
Windows/Mac: Download Docker Desktop from the Official website and follow the installation instructions.
Linux: Use the package manager for your distribution (e.g., apt, yum, dnf) to install Docker.
For Ubuntu:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
What is a docker image?
Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how.
Building your docker image:
docker build -t apricot:1.0 .
The general command syntax is as follows:
docker build -t < imagename >:< tag-version > .
. refers to the current directory.If the dockerfile is in other directory, then use the below command:
docker build -t nginx /path/to/folder
We can get the list of local images present on our machine using this command:
docker images
In Docker, if you don't specify a tag name when pulling or building an image, it defaults to the latest tag. For example, if you run docker pull ubuntu, it will pull the image tagged as latest from the Docker registry.
What is docker registry?
The Docker registry, such as Docker Hub, is where images for various types of applications are stored. In simple terms, a Docker registry is analogous to GitHub, but for Docker images. Just like code repositories on GitHub, Docker Hub maintains a version history of your application's images, allowing you to track and manage different versions of your images.
To work with images, we need them locally on our machine.
Pulling image from docker hub:
docker pull mongo
This command will pull the MongoDB image with the latest tag from Docker Hub. If you want to pull a specific version of MongoDB, you can specify the tag like this:
docker pull mongo:<tag>
What is a container?
A container is a running instance of an image. A Docker container is a lightweight, standalone virtual environment that packages application code along with all the dependencies needed to run the application. This ensures that the application runs consistently and reliably across different computing environments.
docker run -d -p 9090:80 --name plum apricot:1.0
Here,
- -d flag corresponds to image running in detached mode.
- -p flag for the port number, the format is local-port: container-port
- --name for the container name, webserver in our case
To get all the containers that are currently running. It shows information such as container ID, image name, command, creation time, status, ports, and container names.
docker ps
To see all the container including the ones that are stopped use this command.
docker ps -a
Starting a container:
docker start <container-name>
Replace <container-name>
with the name or ID of the container you want to start.
Stopping a container:
docker stop <container-name>
Replace <container-name>
with the name or ID of the container you want to stop.
Docker Volumes
Volumes are used for data persistence in Docker. Without volumes, any data stored within a container is lost when the container is stopped or removed. Unlike a virtual machine, which has its own file system and memory, a Docker container's file system is ephemeral. To ensure that data is retained across container restarts or re-creations, Docker uses volumes. These volumes are stored on the host machine and can be mounted into one or more containers, allowing data to persist independently of the container's lifecycle.
Use this command to create a volume:
docker volume create <volume-name>
Replace <volume-name>
with your desired volume name
Use this command in order to explore the file system of the container:
docker exec -it <container-name> /bin/bash
This command lets you access your container and see what's happening inside a container to some extent.
Use this command in order to mount your volume on container
docker run -d -v <volume-name>:<container-path> --name <container-name> <image-name>
Here,
-
<volume-name>
corresponds to the name of the volume. -
<container-path>
corresponds to the file path inside the container where the data will be stored. -
<container-name>
corresponds to the name of the container. -
<image-name>
corresponds to the name of image that is locally present on our machine.
Docker Network
Docker networks are used when we want containers to communicate with each other. In simple terms, containers under the same network can communicate with each other.
Use this command to create a network:
docker network create <network-name>
Use this command to run a container and assign that container to a network
docker run -d --name <container-name> --network <network-name> <image-name>
Here,
-
<network-name>
corresponds to the name of the network. -
<container-name>
corresponds to the name of the container. -
<image-name>
corresponds to the name of image that is locally present on our machine.
Posted on August 12, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 20, 2024
November 19, 2024