Showing 30 question(s)

Answer:

Docker is an open-source platform that enables developers to package applications and their dependencies into lightweight, portable containers that run consistently across different environments.

Code Example:

docker --version

docker info

Tags:

Answer:

A Docker container shares the host operating system kernel and is lightweight, whereas a Virtual Machine includes a complete guest operating system, making it larger and slower to start.

Code Example:

# Run a container
docker run nginx

# List running containers
docker ps

Tags:

Answer:

A Docker Image is a read-only template containing application code, runtime, libraries, dependencies, and configuration required to create Docker containers.

Code Example:

# List images
docker images

# Pull an image
docker pull nginx

Tags:

Answer:

A Docker Container is a running instance of a Docker image. Containers are isolated, lightweight, and portable.

Code Example:

docker run -d nginx

docker ps

Tags:

Answer:

Docker Hub is a cloud-based registry that stores and distributes Docker images. Developers can pull official images or publish their own.

Code Example:

docker login

docker pull ubuntu

docker push myrepo/myimage

Tags:

Answer:

The docker pull command downloads an image from Docker Hub or another container registry.

Code Example:

docker pull mysql

docker pull redis

docker images

Tags:

Answer:

The docker run command creates and starts a container from a Docker image.

Code Example:

docker run nginx

docker run -d nginx

docker run -p 8080:80 nginx

Tags:

Answer:

Use docker ps to list running containers. Use docker ps -a to view all containers including stopped ones.

Code Example:

docker ps

docker ps -a

Tags:

Answer:

Use docker stop to stop a running container and docker rm to remove it.

Code Example:

docker stop container_id

docker rm container_id

docker rm -f container_id

Tags:

Answer:

The docker images command displays all locally available Docker images, including repository name, tag, image ID, and size.

Code Example:

docker images

docker image ls

Tags:

Answer:

A Docker Image is a read-only template used to create containers, while a Docker Container is a running instance of that image.

Code Example:

# Pull an image
docker pull nginx

# Create a container from the image
docker run -d nginx

# View images and containers
docker images
docker ps

Tags:

Answer:

Use the docker build command with a Dockerfile to create a custom Docker image.

Code Example:

docker build -t myapp:1.0 .

docker images

Tags:

Answer:

Use the docker rmi command to delete one or more Docker images.

Code Example:

docker rmi nginx

docker rmi image_id

Tags:

Answer:

Tags identify different versions of an image, making it easier to manage releases.

Code Example:

docker tag myapp:latest myrepo/myapp:v1

docker images

Tags:

Answer:

After logging into Docker Hub, use docker push to upload an image to your repository.

Code Example:

docker login

docker tag myapp myusername/myapp:v1

docker push myusername/myapp:v1

Tags:

Answer:

A Dockerfile is a text file containing instructions to build a Docker image automatically.

Code Example:

FROM nginx

COPY . /usr/share/nginx/html

EXPOSE 80

Tags:

Answer:

The FROM instruction specifies the base image from which a new Docker image is created.

Code Example:

FROM ubuntu:22.04

FROM node:20

FROM mcr.microsoft.com/dotnet/sdk:8.0

Tags:

Answer:

COPY simply copies files from the host to the container, whereas ADD can also extract local archives and download remote URLs.

Code Example:

COPY app/ /app/

ADD app.tar.gz /app/

COPY package.json ./

Tags:

Answer:

WORKDIR sets the working directory for all subsequent Dockerfile instructions.

Code Example:

FROM node:20

WORKDIR /app

COPY . .

RUN npm install

Tags:

Answer:

CMD specifies the default command that runs when a container starts. Only the last CMD instruction is executed.

Code Example:

FROM nginx

CMD ["nginx", "-g", "daemon off;"]

Tags:

Answer:

Docker Compose is a tool used to define and run multi-container Docker applications using a YAML configuration file.

Code Example:

version: '3.9'

services:
  web:
    image: nginx

  db:
    image: mysql:8

Tags:

Answer:

Use the docker compose up command to build and start all services defined in the compose file.

Code Example:

docker compose up

docker compose up -d

docker compose down

Tags:

Answer:

The docker-compose.yml file defines services, networks, volumes, environment variables, and dependencies for multi-container applications.

Code Example:

services:
  api:
    build: .

  redis:
    image: redis

Tags:

Answer:

Docker Networks enable communication between containers while isolating them from other applications.

Code Example:

docker network ls

docker network create my-network

docker network inspect my-network

Tags:

Answer:

Docker provides Bridge, Host, None, Overlay, and Macvlan network drivers for different networking scenarios.

Code Example:

docker network create --driver bridge my-network

Tags:

Answer:

A Docker Volume is a persistent storage mechanism that allows data to survive even after a container is removed.

Code Example:

docker volume create my-volume

docker volume ls

docker volume inspect my-volume

Tags:

Answer:

Volumes can be mounted using the -v or --mount option when creating a container.

Code Example:

docker run -d -v my-volume:/app/data nginx

Tags:

Answer:

Environment variables are used to pass configuration values into containers at runtime.

Code Example:

docker run -e ASPNETCORE_ENVIRONMENT=Production myapp

Tags:

Answer:

Use the docker logs command to view the standard output and error logs of a container.

Code Example:

docker logs container_id

docker logs -f container_id

Tags:

Answer:

Use official base images, minimize image size, use multi-stage builds, avoid running containers as root, use .dockerignore, pin image versions, and regularly scan images for vulnerabilities.

Code Example:

# .dockerignore
node_modules
.git
bin
obj

# Build optimized image
docker build -t myapp .

Tags: