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 infoAnswer:
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 psAnswer:
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 nginxAnswer:
A Docker Container is a running instance of a Docker image. Containers are isolated, lightweight, and portable.
Code Example:
docker run -d nginx
docker psAnswer:
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/myimageAnswer:
The docker pull command downloads an image from Docker Hub or another container registry.
Code Example:
docker pull mysql
docker pull redis
docker imagesAnswer:
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 nginxAnswer:
Use docker ps to list running containers. Use docker ps -a to view all containers including stopped ones.
Code Example:
docker ps
docker ps -aAnswer:
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_idAnswer:
The docker images command displays all locally available Docker images, including repository name, tag, image ID, and size.
Code Example:
docker images
docker image lsAnswer:
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 psAnswer:
Use the docker build command with a Dockerfile to create a custom Docker image.
Code Example:
docker build -t myapp:1.0 .
docker imagesAnswer:
Use the docker rmi command to delete one or more Docker images.
Code Example:
docker rmi nginx
docker rmi image_idAnswer:
Tags identify different versions of an image, making it easier to manage releases.
Code Example:
docker tag myapp:latest myrepo/myapp:v1
docker imagesAnswer:
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:v1Answer:
A Dockerfile is a text file containing instructions to build a Docker image automatically.
Code Example:
FROM nginx
COPY . /usr/share/nginx/html
EXPOSE 80Answer:
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.0Answer:
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 ./Answer:
WORKDIR sets the working directory for all subsequent Dockerfile instructions.
Code Example:
FROM node:20
WORKDIR /app
COPY . .
RUN npm installAnswer:
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;"]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:8Answer:
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 downAnswer:
The docker-compose.yml file defines services, networks, volumes, environment variables, and dependencies for multi-container applications.
Code Example:
services:
api:
build: .
redis:
image: redisAnswer:
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-networkAnswer:
Docker provides Bridge, Host, None, Overlay, and Macvlan network drivers for different networking scenarios.
Code Example:
docker network create --driver bridge my-networkAnswer:
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-volumeAnswer:
Volumes can be mounted using the -v or --mount option when creating a container.
Code Example:
docker run -d -v my-volume:/app/data nginxAnswer:
Environment variables are used to pass configuration values into containers at runtime.
Code Example:
docker run -e ASPNETCORE_ENVIRONMENT=Production myappAnswer:
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_idAnswer:
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 .