0% found this document useful (0 votes)
0 views24 pages

docker

The document provides an extensive overview of Docker concepts including entrypoints, CMD vs ENTRYPOINT, file operations (ADD vs COPY), and best practices for Dockerfile creation. It covers various commands and scenarios related to Docker image management, container runtime, security measures, and troubleshooting techniques. Additionally, it discusses Docker Swarm, multistage builds, and the differences between Docker images and containers.

Uploaded by

kkumarmy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views24 pages

docker

The document provides an extensive overview of Docker concepts including entrypoints, CMD vs ENTRYPOINT, file operations (ADD vs COPY), and best practices for Dockerfile creation. It covers various commands and scenarios related to Docker image management, container runtime, security measures, and troubleshooting techniques. Additionally, it discusses Docker Swarm, multistage builds, and the differences between Docker images and containers.

Uploaded by

kkumarmy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DOCKER

1 * Entrypoint in Docker?
2 * CMD vs Entrypoint ?
3 * ADD and Copy ?
4 * At the time of docker build –t cmd execution if you press CTRL+C, what happens ?
5 * Docker file size is 2GB and need to reduce the size ? what will you do ?
6 * Diff bw layers and image ?
7 * If you give multiple entrypoints in Docker file, which will be executed and what happened to
previous ones ?
8 * By default in which user the container starts to execute cmds ?
9 * Docker socket ?
10 * what is container Runtime in Docker ?
11 * How do you mount a file system in docker container ?
12 * Write a Dockerfile and push to dockerhub.
13 * docker file structure
14 * multistage dockerfile script
15 * dockerfile to install java
16 * From command
17 * Publish & Expose
18 * command to publish & expose
19 * docker container
20 * diff b/w cmd & entrypoint
21 * difference between cmd and run
22 * If previous night the container had stopped working and terminated, how will you collect logs
when you even dont have volume attach to it
23 * What are security measures you took to secure the docker image
24 * Which scanning mechanism you are using,to scan the images regularly
25 * Do you use docker swam
26 * Dockerfile
27 * Entrypoint , cmd
28 * Dockerfile
29 * How u create docker image from Dockerfile?
30 * Difference between docker image and container?
31 * Namespace cgroup
32 * I have created a custom container , how can I deliver it to the other users?
33 * Where u store docker images in project?
34 * What is DTR, docker trust registry
35 * write the docker file with all the commands and explain
36 * docker file versioning
37 * docker file structure
38 * multistage dockerfile script
39 * dockerfile to install java
40 * From command
41 * Publish & Expose
42 * command to publish & expose
43 * docker container
44 * Docker multiple stage build
45 * docker cmd for trouble container
46 * docker swam
47 * docker compose
48 * how you maintain docker and k8s in your production
49 * difference between docker and k8s
50 * Best practices for writing Dockerfile.
51 * how will u scan ur docker images for security vulnerabilities?
52 * Docker layers ;top layer is read & write
53 * Have you used any kind of docker concept.
54 * where you fiting this docker concept.. which network you will be used ?
55 * difference between CMD & ENTRYPOINT
56 * COPY & ADD
57 * common kind of issues you have faced when you creating a docker image?
58 * created container using cmd docker -p & my container crashed how you are going to
troubleshoot
59 * container having a java based application when i am restarting everything working fine but
when i was working to pass same command in cmd its not restarting what could be the issues
60 * am using plane vanila docker machine not using any networking concept still container is not
up.
61 * one of my client having a requirement they donot want to host their image on a docker registry.
they want to containerization custom registry.(client want secured image)
62 * To copy files/docker image from one machine to another machine. one machine is build vm
another is plain vm bcoz my container is running out of memeory. is it possible? whats the command
63 * VM vs Docker
64 * Why cant we use AWS AMI instead of Docker Image
65 * Dockerfile directives and some sceneries based on entry point
66 * Docker file size is 2GB and need to reduce the size ? what will you do ?
67 * Diff bw layers and image ?
68 * If you give multiple entrypoints in Docker file, which will be executed and what happened to
previous ones ?
69 * By default in which user the container starts to execute cmds ?
70 * dockerfile structure
71 * docker networks
72 * command to attach docker volume to container
73 * create basic container cmd
74 * whats the use of docker ps
75 * difference between k8s & dockerswam
76 * what are the services in docker swam
77 * with dockers what kind of application you used java based/ python based
78 * What's docker file
79 * CMD vs entrypoint
80 * What docker port mapping
81 * What's -p
82 * Dockerfile
83 * Docker many questions on network, docker commands , docker troubleshooting, multistage
build
84 * State full and stateless containers
85 * Difference between them
86 * Tell usecase
87 * In dockerfile run the application which command u use - entrypoint, CMd
88 * What is containerization
89 * How you give root permission inside docker file
90 * Dockerfile - explain each command what you have written
91 * Docker commands
92 * How will you make your application in docker access externally
93 * Write multistage docker file
94 * Diff between CMD and entry point
95 * How to find background process in docker containers
96 * what is docker swarm & Compose
97 * Which applications are running in your docker containers
98 * what is entry poind and command in in docker
99 * write docker file
1. ENTRYPOINT in Docker

ENTRYPOINT defines the main command to run inside the container. It is not overridden by
command-line arguments passed during container run (unless using --entrypoint flag).

Example:
Dockerfile
CopyEdit
FROM ubuntu
ENTRYPOINT ["echo", "Hello from"]
bash
CopyEdit
docker build -t entrypoint-demo .
docker run entrypoint-demo Docker
# Output: Hello from Docker

2. CMD vs ENTRYPOINT
CMD ENTRYPOINT
Default arguments Default executable
Can be overridden with docker run args Not overridden unless --entrypoint is used
One per Dockerfile One per Dockerfile
Used for providing defaults Used to set the main command

Example:
Dockerfile
CopyEdit
# Using CMD
FROM ubuntu
CMD ["echo", "Hello from CMD"]

# Using ENTRYPOINT
FROM ubuntu
ENTRYPOINT ["echo"]
CMD ["Hello from CMD"]

3. ADD vs COPY
COPY ADD
Only copies local files/directories Also handles URL and tar extraction
Safer and more predictable Has more features but is less explicit

Example:
Dockerfile
CopyEdit
COPY [Link] /app/
ADD [Link] /app/ # Extracts the archive

4. What happens if you press CTRL+C during docker build -t?

 The build process is interrupted.


 Partially built image layers are cached and remain on disk.
 You can resume from the last successful layer.

5. Docker image is 2GB – how to reduce size?

 Use multistage builds


 Choose minimal base images (alpine, scratch)
 Clean up cache/files (apt-get clean, remove temp)
 Use .dockerignore
 Combine RUN steps to reduce layers

Example:
Dockerfile
CopyEdit
RUN apt-get update && apt-get install -y package && rm -rf /var/lib/apt/lists/*

6. Difference between layers and image

 Layer: Each instruction (RUN, COPY, etc.) creates a new layer.


 Image: Union of all layers.
 Layers are cached and shared between images to optimize builds.

7. Multiple ENTRYPOINTs in Dockerfile – what happens?

Only the last ENTRYPOINT will be used. Earlier ones are overridden.

Example:
Dockerfile
CopyEdit
ENTRYPOINT ["echo", "First"]
ENTRYPOINT ["echo", "Second"]
# Only "Second" is effective

8. Default user in Docker container

 By default, Docker containers run as root.


 Can be changed using USER in Dockerfile.

Example:
Dockerfile
CopyEdit
USER nobody

9. Docker socket

 Docker daemon listens on /var/run/[Link].


 Used by Docker CLI or 3rd-party tools to communicate with the daemon.
 Security risk: Giving access to the socket is equivalent to giving root access on the host.
10. What is container runtime in Docker?

 Responsible for running containers (e.g., creating namespaces, cgroups).


 Docker uses containerd and runc as runtimes.
 OCI-compliant runtimes (e.g., crun, runc, Kata)

11. Mount filesystem in Docker container

Use -v or --mount with docker run to mount volumes.

Example:
bash
CopyEdit
docker run -v /host/path:/container/path ubuntu

12. Write Dockerfile and push to DockerHub

Dockerfile:
Dockerfile
CopyEdit
FROM alpine
RUN echo "Hello Dockerhub!" > /[Link]
CMD ["cat", "/[Link]"]

Commands:
bash
CopyEdit
docker build -t yourusername/hello-docker .
docker login
docker push yourusername/hello-docker

13. Dockerfile structure

1. FROM – base image


2. LABEL/MAINTAINER
3. RUN – install packages
4. COPY/ADD – copy files
5. CMD/ENTRYPOINT – default command
6. EXPOSE – expose ports
7. ENV – set environment vars
8. USER, WORKDIR, VOLUME, etc.

14. Multistage Dockerfile

Used to optimize build size.

Example:
Dockerfile
CopyEdit
# Stage 1
FROM golang:alpine as builder
WORKDIR /app
COPY . .
RUN go build -o app

# Stage 2
FROM alpine
COPY --from=builder /app/app /app
CMD ["/app"]

15. Dockerfile to install Java


Dockerfile
CopyEdit
FROM openjdk:11-jre-slim
COPY [Link] /[Link]
CMD ["java", "-jar", "/[Link]"]

16. FROM command

Specifies base image.

Example:
Dockerfile
CopyEdit
FROM ubuntu:20.04

17. Publish & Expose

 EXPOSE: Documented port (doesn't publish)


 -p: Maps host port to container port

18. Command to publish & expose


bash
CopyEdit
docker run -p 8080:80 nginx

Exposes container port 80 to host port 8080.

19. Docker container

A container is a lightweight, isolated environment to run applications using image layers.

Create & run:


bash
CopyEdit
docker run -it ubuntu bash
20. CMD vs ENTRYPOINT (Revisited for clarity)

 Use CMD when you want users to override the default behavior.
 Use ENTRYPOINT when the container is expected to always run a specific binary (like a
webserver).

Combined usage:
Dockerfile
CopyEdit
ENTRYPOINT ["python3"]
CMD ["[Link]"]

Can override CMD with:


bash
CopyEdit
docker run myimage [Link]

21. Difference between CMD and RUN


CMD RUN
Executes when container starts Executes during image build
Defines default runtime command Used to install/setup software
Doesn’t create image layer Creates a new image layer

Example:
Dockerfile
CopyEdit
RUN apt-get install -y nginx # builds the image
CMD ["nginx", "-g", "daemon off;"] # runs when container starts

22. If container terminated last night without volumes, how to get logs?

Use:
bash
CopyEdit
docker ps -a # find container ID
docker logs <container_id> # fetch logs of terminated container

🔴 Note: Logs are lost if:

 Container was run with --rm


 Docker daemon restarted and logging driver wasn’t json-file

23. Security measures for Docker image

 Use minimal base images (alpine, scratch)


 Don’t store secrets in images
 Use .dockerignore to exclude sensitive files
 Set non-root USER
 Use multi-stage builds
 Keep images updated
 Use signed images (Docker Content Trust)

24. Image scanning mechanisms

 Trivy (popular open-source tool)


 Docker Scout (previously Docker Scan, powered by Snyk)
 Anchore, Clair, Twistlock, Aqua
 Integrate scanning in CI/CD pipeline

25. Do you use Docker Swarm?

You can answer:

Yes, for managing containers across clusters. It provides native orchestration, supports services,
scaling, load balancing, and rolling updates. However, we prefer Kubernetes for larger workloads.

26. Dockerfile

It’s a script to automate image creation using a base image and layers.

Basic Example:
Dockerfile
CopyEdit
FROM alpine
RUN apk add --no-cache curl
CMD ["curl", "[Link]

27. ENTRYPOINT vs CMD

You can combine both:


Dockerfile
CopyEdit
ENTRYPOINT ["python3"]
CMD ["[Link]"]

This runs python3 [Link], but you can override CMD during runtime:
bash
CopyEdit
docker run myimg [Link] # becomes python3 [Link]

28. Dockerfile (repetition of Q26, included above)

29. How to create Docker image from Dockerfile?

1. Create a file named Dockerfile


2. Build the image:
bash
CopyEdit
docker build -t myimage:1.0 .

30. Difference between Docker image and container


Docker Image Docker Container
Blueprint for container Running instance of image
Static Dynamic
Built once Runs many times

31. Namespace & Cgroup

Namespaces isolate:

 Process IDs (pid)


 File systems (mnt)
 Networks (net)
 Users (user)

Cgroups:

 Control resource limits (CPU, memory)

Together, they provide container isolation and control.

32. I’ve created a custom container. How can I deliver it?

 Push to Docker Hub:


bash
CopyEdit
docker tag myapp user/myapp
docker push user/myapp

 Export as tar:
bash
CopyEdit
docker save myapp > [Link]
# share and load with:
docker load < [Link]

33. Where do you store Docker images in your project?

 Private Docker registry (Harbor, ECR, Nexus)


 Docker Hub (for public or personal use)
 Use GitHub Actions + DockerHub for CI/CD delivery

34. What is DTR (Docker Trusted Registry)?


 Enterprise-grade private Docker registry
 Offers:
o Image signing & verification
o Access control
o Vulnerability scanning
o Integration with LDAP/AD

35. Dockerfile with all commands & explanation


Dockerfile
CopyEdit
FROM ubuntu:20.04 # base image
LABEL maintainer="dev@[Link]"
RUN apt-get update && apt-get install -y curl # install curl
COPY [Link] /[Link] # copy script
ADD [Link] /app/ # extract archive
ENV ENV_VAR=value # set environment variable
EXPOSE 8080 # expose port
VOLUME /data # mount point
WORKDIR /app # working directory
USER nobody # run as non-root
CMD ["bash", "/[Link]"] # default command

36. Dockerfile versioning

 Docker itself doesn’t version Dockerfiles.


 Use Git to version control your Dockerfile.
 Tag image versions during build:
bash
CopyEdit
docker build -t myapp:v1.2 .

37. Dockerfile structure

Same as Q13, re-summarized:

 FROM
 LABEL
 RUN
 COPY/ADD
 ENV
 EXPOSE
 VOLUME
 USER
 WORKDIR
 CMD/ENTRYPOINT

38. Multistage Dockerfile script (already explained in Q14, here’s another example)
Dockerfile
CopyEdit
# Build Stage
FROM node:18 as builder
WORKDIR /app
COPY . .
RUN npm install && npm run build

# Production Stage
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html

39. Dockerfile to install Java


Dockerfile
CopyEdit
FROM openjdk:17-jdk-slim
COPY [Link] /[Link]
CMD ["java", "-jar", "/[Link]"]

40. FROM command

Used to define the base image.


Dockerfile
CopyEdit
FROM python:3.10-slim

41. Publish & Expose

 EXPOSE: Dockerfile instruction that documents which port the app listens on.
 -p (publish): Command-line option that maps host port to container port.

EXPOSE in Dockerfile:
Dockerfile
CopyEdit
EXPOSE 8080

Publish during run:


bash
CopyEdit
docker run -p 8080:80 nginx

42. Command to publish & expose


bash
CopyEdit
docker run -d -p 8080:80 --name myweb nginx

This maps:

 Host port 8080 → Container port 80


Even if you used EXPOSE 80 in Dockerfile, you still must publish using -p.
43. Docker container

 A Docker container is a running instance of a Docker image.


 It's lightweight, isolated, and shares the OS kernel.
 Starts using:
bash
CopyEdit
docker run -it ubuntu /bin/bash

44. Docker multi-stage build

Used to create smaller, optimized images.

Example:
Dockerfile
CopyEdit
# Builder stage
FROM golang:1.21 as builder
WORKDIR /app
COPY . .
RUN go build -o app

# Final image
FROM alpine
COPY --from=builder /app/app /app
CMD ["/app"]

45. Docker CMDs to troubleshoot container

 docker logs <id> – View logs


 docker inspect <id> – Details and config
 docker exec -it <id> bash – Get inside running container
 docker events – Monitor Docker events
 docker stats – Monitor CPU/memory

46. Docker Swarm

 Docker's native clustering/orchestration tool.


 Allows:
o Scaling services
o Rolling updates
o Load balancing

Commands:
bash
CopyEdit
docker swarm init
docker service create --replicas 3 nginx
47. Docker Compose

Used to define multi-container apps using [Link].

Example:
yaml
CopyEdit
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
app:
build: .
depends_on:
- web

Run:
bash
CopyEdit
docker-compose up -d

48. Maintaining Docker & K8s in Production

You can answer:

We use Docker for containerization and Kubernetes for orchestration. CI/CD pipelines build Docker
images and push them to a registry (like ECR). Kubernetes pulls those images and deploys them via
Helm charts, ensuring scaling, auto-healing, and service discovery.

49. Docker vs Kubernetes


Feature Docker Kubernetes
What Container engine Orchestration system
Scope Container only Multi-container apps
Scaling Manual Auto-scaling
Networking Basic bridge Advanced (Services, Ingress)
Scheduling None Powerful scheduler

50. Best practices for Dockerfile

 Use multi-stage builds


 Keep images small (use alpine)
 Avoid latest tag
 Use .dockerignore
 Combine RUN commands:
Dockerfile
CopyEdit
RUN apt update && apt install -y curl && rm -rf /var/lib/apt/lists/*
 Specify USER and not run as root

51. How to scan Docker images

 Tools:
o Trivy
o Docker Scout (Snyk)
o Anchore
o Clair
 Example using Trivy:
bash
CopyEdit
trivy image myapp:latest

52. Docker Layers – Top is read/write

 Every Docker image has read-only layers


 When a container runs, a read/write layer is added on top
 Any changes happen only in that top writable layer

53. Have you used any Docker concept?

You can answer:

Yes, I've used Docker to containerize applications, created optimized Dockerfiles, used Compose for
local orchestration, and integrated image builds and scanning into CI/CD pipelines.

54. Where you’re fitting Docker? Which network used?

 Docker used in CI/CD, microservices, local development, staging, and production.


 Networks:
o bridge (default)
o host (uses host network stack)
o overlay (used in Swarm/K8s)

55. CMD vs ENTRYPOINT

🔁 Already explained in Q2 and Q20. Refer back there for table + examples.

56. COPY vs ADD

🔁 Already explained in Q3 and Q56. Summary:

 Use COPY for simple file copy


 Use ADD only for extracting archives or fetching from URL
57. Common issues while building Docker images

 Large image size


 Permission issues (root vs non-root)
 Missing files in .dockerignore
 Incorrect base image
 Caching causing outdated layers
 Dependency version mismatch

58. Container crashed after using docker -p, how do you troubleshoot?

 Check logs:
bash
CopyEdit
docker logs <container_id>

 Check port conflicts:


bash
CopyEdit
docker ps -a
netstat -tuln | grep 8080

 Try running with interactive mode:


bash
CopyEdit
docker run -it -p 8080:80 image bash

 Check if entrypoint/cmd is correct in Dockerfile

59. Java app works on restart but not on CMD – what's the issue?

Possible causes:

 Wrong CMD or ENTRYPOINT override


 CMD is interpreted as shell form instead of exec form
 App may require sh -c or full command string

✅ Fix:
Dockerfile
CopyEdit
CMD ["java", "-jar", "[Link]"]
# OR
CMD java -jar [Link] # if shell form needed

60. Vanilla Docker machine, no networking, but container won’t start

Likely issues:

 No CMD or ENTRYPOINT defined → container exits


 App crashes → check logs
 Port exposed but not published
 Try:
bash
CopyEdit
docker run -it image bash

Then run app manually to see error.

61. Client doesn’t want to use Docker Hub – wants secure custom registry

✅ Use private Docker registry:

 Docker Registry (open-source)


 Harbor (popular in enterprises)
 AWS ECR, GitLab Container Registry

Set up basic registry:


bash
CopyEdit
docker run -d -p 5000:5000 --restart=always --name registry registry:2

Tag and push image:


bash
CopyEdit
docker tag myimage localhost:5000/myimage
docker push localhost:5000/myimage

🔐 For security, use:

 TLS certificates
 Basic Auth
 Harbor (with RBAC, LDAP, scan tools)

62. Copy Docker image or files to another machine (build VM → plain VM)

Yes, it’s possible.

Export image from build machine:


bash
CopyEdit
docker save myimage > [Link]
scp [Link] user@target:/tmp/

On target VM:
bash
CopyEdit
docker load < /tmp/[Link]

📝 Also applicable to copying files using scp or rsync.

63. VM vs Docker
Feature VM Docker
Boot Time Minutes Seconds
Size GBs MBs
OS Overhead Full OS Shares host OS
Isolation Full Process-level
Performance Slower Faster

64. Why not use AWS AMI instead of Docker image?

 AMI is entire OS-level image → heavy & slow to spin up.


 Docker is lightweight → quick, reproducible, easy to scale.
 Docker works well for microservices, CI/CD, k8s, etc.
 AMIs are better for full VM-level setup, not app containers.

65. Dockerfile directives + ENTRYPOINT scenarios

Common directives:

 FROM, RUN, COPY, CMD, ENTRYPOINT, WORKDIR, USER, ENV, EXPOSE,


VOLUME

ENTRYPOINT scenario:
Dockerfile
CopyEdit
ENTRYPOINT ["python3"]
CMD ["[Link]"]

 Default run: python3 [Link]


 Override CMD: docker run myimg [Link]
 Override ENTRYPOINT: docker run --entrypoint bash myimg

66. Docker image size 2GB – how to reduce?

✅ Use:

 Alpine base image


 Multistage build
 Remove apt cache:
Dockerfile
CopyEdit
RUN apt-get update && apt-get install -y package \
&& rm -rf /var/lib/apt/lists/*

 Use .dockerignore

67. Diff between layers and image

 Layer: Each instruction creates a new cached, read-only layer.


 Image: A stack of layers + config metadata.
 Layers are shared across images → optimize builds.

68. Multiple ENTRYPOINTs – which executes?

Only the last ENTRYPOINT in Dockerfile is effective. Previous ones are overridden.
Dockerfile
CopyEdit
ENTRYPOINT ["echo", "First"]
ENTRYPOINT ["echo", "Second"]
# Output: Second

69. Default user in Docker container

 Default: root
 Can be changed with:
Dockerfile
CopyEdit
USER appuser

⚠️Always avoid running as root in production.

70. Dockerfile structure

Order matters:
Dockerfile
CopyEdit
FROM ubuntu
LABEL maintainer="you@[Link]"
RUN apt-get update
COPY . /app
WORKDIR /app
EXPOSE 8080
USER nobody
CMD ["python3", "[Link]"]

71. Docker networks

 bridge (default)
 host (shares host network)
 none
 overlay (used with Swarm)

Create network:
bash
CopyEdit
docker network create mynet

Run with network:


bash
CopyEdit
docker run --network=mynet nginx

72. Attach Docker volume to container


bash
CopyEdit
docker run -v /host/data:/container/data ubuntu

Or using named volume:


bash
CopyEdit
docker volume create myvol
docker run -v myvol:/data ubuntu

73. Create basic container command


bash
CopyEdit
docker run -it ubuntu /bin/bash

74. Use of docker ps

Lists running containers:


bash
CopyEdit
docker ps
docker ps -a # includes stopped containers

75. Difference: Kubernetes vs Docker Swarm


Feature Docker Swarm Kubernetes
Complexity Simple Complex, but powerful
Scalability Limited High
Ecosystem Native Docker Rich ecosystem
Rolling Update Yes Yes
Networking Basic overlay Advanced (Ingress, Services)

76. Services in Docker Swarm

 Manager nodes
 Worker nodes
 Services (equivalent to container deployments)
 Overlay network
 Commands:
bash
CopyEdit
docker service create --replicas 3 nginx
docker service scale myapp=5
77. What kind of apps you containerized?

You can answer:

We containerized both Java (Spring Boot) and Python (Flask) apps. Java apps are heavier, so we
optimized them using JRE base images and multi-stage builds. Python apps used alpine+pip with
minimal layers.

78. What’s Dockerfile?

A Dockerfile is a script of instructions to build Docker images.

Example:
Dockerfile
CopyEdit
FROM python:3.11
COPY . /app
WORKDIR /app
RUN pip install -r [Link]
CMD ["python", "[Link]"]

79. CMD vs ENTRYPOINT

🔁 Already explained in Q2, Q20, Q55 — refer there for full comparison.

80. Docker port mapping

Used to expose container port to host machine.


bash
CopyEdit
docker run -p 8080:80 nginx

 Host: 8080
 Container: 80

You can map multiple:


bash
CopyEdit
docker run -p 3000:3000 -p 5000:5000 myapp

81. What's -p in Docker?

-p stands for port mapping:


Maps a port on the host to a port on the container.

Example:
bash
CopyEdit
docker run -p 8080:80 nginx
 Host port 8080 → Container port 80

82. Dockerfile

A Dockerfile is a set of instructions used to build a Docker image.

Example:
Dockerfile
CopyEdit
FROM node:18
WORKDIR /app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["npm", "start"]

83. Docker topics: network, commands, troubleshooting, multistage

 Network types: bridge, host, overlay, none


 Basic commands:
o docker build, docker run, docker ps, docker exec, docker logs, docker inspect
 Troubleshooting:
o docker logs, docker stats, docker inspect, docker events, docker exec -it
 Multistage builds: Reduce image size by compiling/building in one stage and copying only
artifacts to final stage.

84. Stateful vs Stateless containers

 Stateless: No persistent data between runs (e.g., web servers)


 Stateful: Maintain data or state (e.g., databases)

85. Difference: Stateful vs Stateless


Aspect Stateless Stateful
Data saved Not retained Data is retained
Restart Doesn’t affect state May lose critical state
Use case Web servers, APIs DBs, caching systems

86. Use cases

 Stateless: Nginx, frontend apps, microservices


 Stateful: PostgreSQL, Redis, MongoDB

87. In Dockerfile, which to use to run app – CMD or ENTRYPOINT?

 Use ENTRYPOINT when you want fixed behavior (e.g., always run a specific binary)
 Use CMD to pass default arguments
Best practice:
Dockerfile
CopyEdit
ENTRYPOINT ["java", "-jar"]
CMD ["[Link]"]

88. What is containerization?

Containerization is the process of packaging an application and its dependencies into a container
image, which can run reliably in any environment.

Benefits:

 Portability
 Isolation
 Lightweight

89. Give root permission inside Dockerfile

By default, containers run as root. But if changed, you can switch back to root using:
Dockerfile
CopyEdit
USER root

⚠️Not recommended for production due to security risks.

90. Dockerfile explained with all commands


Dockerfile
CopyEdit
FROM python:3.11 # base image
WORKDIR /app # set working dir inside container
COPY . . # copy files into container
RUN pip install -r [Link] # install dependencies
EXPOSE 5000 # expose port
CMD ["python", "[Link]"] # default command to run

91. Docker commands

 docker build -t myapp . – Build image


 docker run -d -p 8080:80 myapp – Run container
 docker ps – List containers
 docker logs <id> – View logs
 docker exec -it <id> bash – Shell into container
 docker stop <id> – Stop container

92. Make Docker container accessible externally

Use -p to publish port:


bash
CopyEdit
docker run -d -p 8080:80 myapp

Ensure:

 App listens on [Link]


 Security group/firewall allows the port

93. Multistage Dockerfile


Dockerfile
CopyEdit
# Builder stage
FROM golang:1.20 as builder
WORKDIR /app
COPY . .
RUN go build -o myapp

# Final stage
FROM alpine
COPY --from=builder /app/myapp /myapp
CMD ["/myapp"]

94. CMD vs ENTRYPOINT


CMD ENTRYPOINT
Sets default command Sets default executable
Can be overridden Not easily overridden
Used for flexibility Used for fixed command behavior

95. Find background processes in Docker container


bash
CopyEdit
docker exec -it <container_id> ps aux

To check from host:


bash
CopyEdit
docker top <container_id>

96. Docker Swarm & Compose

 Docker Swarm: Native clustering & orchestration tool


 Docker Compose: Tool for defining and running multi-container apps with docker-
[Link]

97. Which applications are running in your containers?

Sample answer:
Our containers run various apps like:

 Java Spring Boot APIs


 Python Flask services
 Nginx reverse proxies
 Redis and MongoDB for stateful services

98. ENTRYPOINT vs CMD (revisit)

 ENTRYPOINT: Defines what to run


 CMD: Supplies default args

Combined:
Dockerfile
CopyEdit
ENTRYPOINT ["python3"]
CMD ["[Link]"]

99. Write Dockerfile


Dockerfile
CopyEdit
FROM node:18
WORKDIR /app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["npm", "start"]

You might also like