Introduction
Docker is like a virtual computer that can run inside your actual computer. This virtual computer is called a container, and it can run a specific program or application.
Think of it like a lunchbox that contains everything you need for your lunch. The lunchbox is the container, and the food inside it is the program or application. The lunchbox keeps the food separate from the other things in your bag, so you can easily find and eat your lunch when you’re ready.
In the same way, a Docker container keeps the program or application separate from the rest of your computer, so it can run without interfering with other programs or applications. And just like you can easily carry your lunchbox with you wherever you go, Docker containers can be easily moved and run on different computers or servers.
it helps developers package their applications into containers.
Containers are like small, standalone, and portable environments that include everything needed to run an application, such as the application code, dependencies, and runtime. Think of it like a “package” for an application.
When we talk about an application, there are usually three main components that make it work: the application code, dependencies, and runtime.
Application Code: This is the actual program or software that performs a specific task. It includes all the code that was written by the developer to create the application.
Dependencies: These are other software components that the application needs to function properly. Dependencies can include libraries, frameworks, and other software packages that the application relies on. e.g. JDK
Runtime: This is the environment in which the application runs. It includes the software and hardware components that the application needs to operate, such as the operating system, system libraries, and any other services or components required to run the application. e.g. JRE
Benifits
The benefits of using Docker are that it makes it easier to move software between different computers and servers, and it helps ensure that the software runs the same way every time, no matter where it is running. It can also help reduce conflicts between different software packages on the same computer, since each package runs in its own container.
Docker key concepts
Images
A Docker image is a read-only template
that defines the application
, its dependencies, and the runtime environment. Images are used to create Docker containers.
When we talk about an application, there are usually three main components that make it work: the application code, dependencies, and runtime.
Application Code: This is the actual program or software that performs a specific task. It includes all the code that was written by the developer to create the application.
Dependencies: These are other software components that the application needs to function properly. Dependencies can include libraries, frameworks, and other software packages that the application relies on. e.g. JDK
Runtime: This is the environment in which the application runs. It includes the software and hardware components that the application needs to operate, such as the operating system, system libraries, and any other services or components required to run the application. e.g. JRE
1
2
3
4
5
# Pull an image from Docker Hub
docker pull nginx
# List all images on the local machine
docker images
Containers
A Docker container is a lightweight, standalone, and executable package that includes everything needed to run an application, including the application code, libraries, dependencies, and runtime.
1
2
3
4
5
6
7
8
9
10
# Create a new container from an image
docker run -it --name mycontainer nginx
# List all running containers
docker ps
# Start, Stop and remove a container
docker start mycontainer
docker stop mycontainer
docker rm mycontainer
Dockerfile
A Dockerfile is a text file that contains instructions for building a Docker image. It specifies the base image, any additional software dependencies, and how to configure the container.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Use an official Python runtime as a parent image FROM python:3.8-slim-buster # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install the required packages RUN pip install --no-cache-dir -r requirements.txt # Make port 80 available to the world outside this container EXPOSE 80 # Define the command to run the application CMD ["python", "app.py"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# Specify the base image FROM node:14-alpine # Set the working directory WORKDIR /app # Copy the package.json and package-lock.json files to the container COPY package*.json ./ # Install the dependencies RUN npm install # Copy the rest of the application files to the container COPY . . # Expose port 3000 for the application EXPOSE 3000 # Start the application CMD [ "npm", "start" ]
1 2 3 4 5
# Build a Docker image from the current directory and tag it as "myimage" docker build -t myimage . # Add a new tag to the existing "myimage" Docker image with the name "dockerhub4manohar/myimage" docker tag myimage dockerhub4manohar/myimage
Registry
A Docker registry is a repository for storing and distributing Docker images. Docker Hub is a popular public registry, but you can also set up your own private registry.
1
2
3
4
5
6
7
8
9
# Log in to a Docker registry
docker login dockerhub4manohar
# Push an image to the registry
docker tag myimage dockerhub4manohar/myimage
docker push dockerhub4manohar/myimage
# Pull an image from the registry
docker pull dockerhub4manohar/myimage
Compose
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define the services, networks, and volumes for your application in a single YAML file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Define a multi-container application using Docker Compose
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
depends_on:
- db
db:
image: postgres
environment:
POSTGRES_PASSWORD: example