Senior
Tell me about Docker: main principles of operation, multistage builds, CLI commands
sobes.tech AI
Answer from AI
Docker is a platform for containerizing applications, allowing you to package an application with its dependencies into an isolated container that can run in any environment.
Main principles of operation:
- Containers use images that contain everything needed to run the application.
- Images are built in layers, which allows efficient use of disk space.
- Containers are isolated from each other and from the host, but are lightweight compared to virtual machines.
Multistage builds: Allow creating multiple build stages in a single Dockerfile, helping to reduce the size of the final image. For example, the first stage builds the application with development tools, and the second copies only the necessary artifacts into a minimal image.
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
FROM alpine:latest
COPY --from=builder /app/myapp /myapp
CMD ["/myapp"]
Main CLI commands:
docker build— builds an image from a Dockerfile.docker run— runs a container from an image.docker ps— views running containers.docker images— views local images.docker stopanddocker rm— stop and remove containers.docker pullanddocker push— download and upload images to/from a registry.
Docker significantly simplifies deployment and scaling of applications, ensuring environment consistency.