Docker Interview Questions & Notes
Now, let's go through some useful Docker commands:
docker build
: Build an image from a Dockerfiledocker run
: Run a container from an imagedocker ps
: List running containersdocker images
: List available imagesdocker stop
: Stop a running containerdocker rm
: Remove a containerdocker rmi
: Remove an imagedocker exec
: Execute a command in a running containerdocker logs
: View logs of a containerdocker-compose up
: Start services defined in a docker-compose.yml file
Auto-scaling with Docker Swarm:
Docker Swarm is Docker's native clustering and orchestration solution. Here's a basic example of how to use it:
- Initialize a Swarm:
docker swarm init
- Create a service with multiple replicas:
docker service create --name my-web-app --replicas 3 -p 80:3000 my-nodejs-app
- Scale the service:
docker service scale my-web-app=5
This will increase the number of replicas to 5.
Understanding Docker Compose:
Docker Compose is a tool for defining and running multi-container Docker applications. Here's a simple docker-compose.yml
file for our Node.js app:
yamlversion: '3' services: web: build: . ports: - "3000:3000" redis: image: "redis:alpine"
To run this composition:
docker-compose up
This will start both the Node.js app and a Redis container.
To stop and remove the containers:
docker-compose down