Docker - pranavkumarpk01/MD-DevOps GitHub Wiki
Docker is a containerization platform that allows developers to package applications and their dependencies into lightweight, portable containers. Containers ensure that applications run consistently across different environments.
- Portability: Run anywhere (local, cloud, servers).
- Scalability: Easily scale applications with container orchestration tools.
- Consistency: Avoid environment-specific issues.
- Resource Efficiency: Containers use fewer resources than virtual machines.
- Faster Deployment: Containers start quickly and reduce downtime.
- A Docker image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software (code, libraries, dependencies, and configuration files).
- Images are built from Dockerfiles and stored in Docker Hub.
Common Commands:
# List all downloaded images
docker images
# Pull an image from Docker Hub
docker pull ubuntu:latest
# Remove an image
docker rmi image_id
- A container is a running instance of a Docker image.
- Containers are isolated environments, ensuring that applications run consistently.
Common Commands:
# Run a container
docker run -d -p 8080:80 nginx
# List running containers
docker ps
# Stop a container
docker stop container_id
# Remove a container
docker rm container_id
- A Dockerfile is a script that contains a set of instructions for building a Docker image.
Example Dockerfile:
# Use an official Node.js runtime as a parent image
FROM node:14
# Set the working directory
WORKDIR /app
# Copy package.json and install dependencies
COPY package.json .
RUN npm install
# Copy the project files
COPY . .
# Expose a port and start the application
EXPOSE 3000
CMD ["node", "index.js"]
Build and Run the Image:
# Build an image
docker build -t myapp .
# Run the container
docker run -p 3000:3000 myapp
- Docker Compose allows you to define multi-container applications.
Example docker-compose.yml
:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
database:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
Commands:
# Start services
docker-compose up -d
# Stop services
docker-compose down
- A cloud-based registry to store and share Docker images.
- You can push your own images to Docker Hub for sharing.
# Log in to Docker Hub
docker login
# Push an image to Docker Hub
docker tag myapp username/myapp
docker push username/myapp
- A developer writes a web application in Node.js.
- They create a
Dockerfile
to containerize the app. - They build a Docker image using
docker build
. - The image is pushed to Docker Hub.
- A team member pulls the image and runs it locally.
- The image is deployed to a cloud server using
docker run
. - Updates are made, a new image is built, and the cycle repeats.
Answer: Docker is an open-source platform that enables developers to automate the deployment of applications inside lightweight, portable containers.
Answer: A Docker container is a lightweight, standalone, and executable package that includes everything needed to run an application, such as code, runtime, libraries, and dependencies.
Answer: A Docker image is a lightweight, stand-alone, and executable software package that contains all the dependencies and instructions needed to run an application in a container.
Answer: You can create a Docker container using the command:
docker run -d --name my_container my_image
Answer: Use the command:
docker ps
Answer: Use the command:
docker ps -a
Answer: Use the command:
docker stop <container_id>
Answer: Use the command:
docker rm <container_id>
Answer: Use the command:
docker rmi <image_id>
Answer: A Dockerfile is a script containing a series of instructions that are used to build a Docker image.
Answer: Use the command:
docker build -t my_image .
Answer: CMD sets default commands and arguments, while ENTRYPOINT is used to define a fixed command that cannot be overridden easily.
Answer: Use the command:
docker run -it my_image bash
Answer: Use the command:
docker logs <container_id>
Answer: Use the command:
docker start <container_id>
Answer: Use the command:
docker inspect <container_id>
Answer: A Docker Volume is a persistent storage mechanism that allows data to persist beyond the lifecycle of a container.
Answer: Use the command:
docker volume create my_volume
Answer: Use the command:
docker run -v my_volume:/data my_image
Answer: Docker Compose is a tool for defining and running multi-container Docker applications using a YAML file.
Answer: Use the command:
docker-compose up -d
Answer: Use the command:
docker-compose down
Answer: The default network driver in Docker is bridge.
Answer: Use the command:
docker container prune
Answer: Use the command:
docker image prune
For More Questions Pls refer -> https://www.geeksforgeeks.org/docker-interview-questions/