Getting Started with Docker: A Comprehensive Guide
Docker is a powerful platform for developing, shipping, and running applications inside containers. Containers are lightweight, portable, and self-sufficient units that can run applications and their dependencies in an isolated environment. This guide will walk you through the basics of Docker, including images, containers, Dockerfile, and more.
What is Docker?
Docker simplifies the process of creating, deploying, and managing containerized applications. It provides a set of tools and a platform that allows developers to package applications and their dependencies into a standardized unit known as a container.
Key Points
Docker Images
Docker Container
Dockerfile
Building an Image
Understanding Volumes and Bind Mounts
Docker Images
What are Docker Images?
Docker images are the building blocks of containers. An image is a lightweight, standalone, and executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, and system tools.
How to Pull an Image from Docker Hub
Docker Hub is a cloud-based repository where Docker users can create, test, store, and distribute container images. To pull an image from Docker Hub, use the following command:
*docker pull nginx:1.24
- nginx
is the image name.
- 1.24
is the image tag/version.
Docker Containers
What are Docker Containers?
Containers are instances of Docker images. They run in an isolated environment on the host system and share the host OS kernel. Containers can be started, stopped, and deleted easily, providing a consistent and reproducible environment.
Running a Container
To run a container from an image, use the following commands:
docker run -d nginx:1.24
docker run -d -p 9000:80 nginx:1.24
- -d
detaches the terminal.
- -p 9000:80
maps port 80 of the Nginx container to port 9000 on localhost.
Whenever request comes on port 9000 of machine host , it will be routed to port 80 of Nginx container since we mapped port 80 of container to localhost:9000 of host machine
Managing Containers
To view running containers:
docker ps
To see logs of a container:
docker logs <containerID>
To stop a container:
docker stop <containerID>
What is a Dockerfile?
A Dockerfile is a text document that contains all the commands to assemble an image. Docker can use these commands to build an image.
Basic Dockerfile Instructions
Docker file is the set of instruction which specifies commands to build a standalone Docker Image of your local project
- FROM
specifies the base image.
- RUN
executes commands in the container.
- COPY
copies files from the local filesystem to the container.
Example Dockerfile:
Building an Image
To build an image from a Dockerfile:
docker build -t node-app:1.0 -f Dockerfile .
- -t
tags the image with a name and version.
- -f
` specifies the Dockerfile.
Docker Volumes
Volumes are used to persist data generated by and used by Docker containers. To create and manage volumes:
whenever a container get killed at that time all the data related to that container will be deleted if we don't use volume with it,
To avoid this we need to explicitly create volumes and attach it to containers to persist the data
Below is the commands for creating and attaching volume to the mongo container
docker volume create volume_database
docker volume ls
docker run -v volume_database:/data/db -p 27017:27017 mongo
Bind Mounts
Bind mounts are used for hot-reloading of your Docker container during development:
Docker containers automatically can't hot reload, and when developers develops some features or done some changes in the code then they need to see the changes in effect while developing without rebuilding the image,
In order to do this we need to use bind mounts
docker run -p 4500:4200 -v ./app:/angular/app docker_hot_reload
- -v ./app:/angular/app
mounts the app folder of the host to the /angular/app
folder in the container.
Conclusion
Docker is a versatile platform that simplifies application development and deployment through containerization. By understanding images, containers, Dockerfile, volumes and Bind Mounts you can leverage Docker to streamline your development workflow and ensure consistency across environments.