Mastering Docker Compose: Top Real-Time Interview Questions with Code
- What is Docker Compose, and how does it differ from Docker?
Docker Compose is a tool used for defining and running multi-container Docker applications. It allows you to define all the containers that make up your application in a single YAML file, and then start and stop them with a single command. Docker Compose makes it easy to configure the services your application needs and manage their interactions.
Docker, on the other hand, is a platform for developing, shipping, and running applications using containers. It provides an API and command-line interface for managing containers, images, networks, and volumes.
- How do you define services in a Docker Compose file?
Services are defined in a Docker Compose file using the services
keyword. Each service is given a name, which can be used to refer to it later on in the file. The configuration for each service includes options such as the container image to use, ports to expose, volumes to mount, and environment variables to set. Here’s an example of a simple Docker Compose file with two services:
version: '3'
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: postgres:latest
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
This file defines two services: web
and db
. The web
service uses the nginx
container image and exposes port 80 on the host machine. The db
service uses the postgres
container image and sets the POSTGRES_USER
and POSTGRES_PASSWORD
environment variables.
- What is the syntax for defining volumes in a Docker Compose file?
Volumes can be defined in a Docker Compose file using the volumes
keyword. You can specify the name of the volume, and any additional options you want to set. Here’s an example:
version: '3'
services:
web:
image: nginx:latest
volumes:
- data:/usr/share/nginx/html
volumes:
data:
This file defines a single service, web
, which uses the nginx
container image. The volumes
section defines a named volume called data
. The web
service mounts this volume at /usr/share/nginx/html
in the container.
- How do you configure environment variables for services in a Docker Compose file?
Environment variables can be configured for services using the environment
keyword. You can specify one or more key-value pairs to set as environment variables. Here’s an example:
version: '3'
services:
web:
image: nginx:latest
environment:
- ENV_VAR_1=value1
- ENV_VAR_2=value2
This file defines a single service, web
, which uses the nginx
container image. The environment
section sets two environment variables: ENV_VAR_1
with a value of value1
, and ENV_VAR_2
with a value of value2
.
- What is the difference between the
docker-compose up
anddocker-compose start
commands?
The docker-compose up
command creates and starts all the services defined in a Docker Compose file. It also attaches to the container logs, so you can see the output from all the containers. If any of the containers fail, docker-compose up
will stop all the containers and display an error message.
The docker-compose start
command starts all the containers defined in a Docker Compose file, but it does not create new containers. If a container is not running, docker-compose start
will start it.
Docker compose scenerio based questions
- Scenario: You have a microservices architecture-based application that has three containers: a web server, a Redis database, and a worker. How would you define this application in a Docker Compose file?
version: "3"
services:
web:
build: ./web
ports:
- "8080:80"
depends_on:
- redis
- worker
redis:
image: redis
worker:
build: ./worker
- Scenario: You have two containers, one for a web server and one for a database server. You want to launch the web server container with an environment variable that points to the IP address of the database server container. How would you define this in a Docker Compose file?
version: "3"
services:
web:
build: ./web
environment:
- DATABASE_URL=postgres://db:5432/mydb
ports:
- "8080:80"
db:
image: postgres
- Scenario: You have a WordPress application that requires a database server. You want to define both containers in a Docker Compose file and link them together. How would you do this?
version: "3"
services:
wordpress:
image: wordpress
ports:
- "8080:80"
depends_on:
- db
environment:
- WORDPRESS_DB_HOST=db:3306
- WORDPRESS_DB_NAME=mydb
- WORDPRESS_DB_USER=myuser
- WORDPRESS_DB_PASSWORD=mypassword
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=myrootpassword
- MYSQL_DATABASE=mydb
- MYSQL_USER=myuser
- MYSQL_PASSWORD=mypassword
- Scenario: You have a Python application that requires multiple containers, including a web server and a PostgreSQL database. You want to define the application in a Docker Compose file and use a custom network. How would you do this?
version: "3"
services:
web:
build: .
ports:
- "8000:8000"
networks:
- my-network
depends_on:
- db
db:
image: postgres
networks:
- my-network
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
networks:
my-network:
driver: bridge
- Scenario: You have a microservices-based application that has four containers: a web server, a Redis database, a PostgreSQL database, and a worker. You want to define the application in a Docker Compose file and use environment variables to define the connection strings for the databases. How would you do this?
version: "3"
services:
web:
build: .
ports:
- "8000:8000"
environment:
REDIS_HOST: redis
POSTGRES_HOST: db
depends_on:
- db
- redis
- worker
db:
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
redis:
image: redis
worker:
build: ./worker
These are just a few examples of scenario-based questions that may be asked in a Docker Compose interview. The key is to understand the purpose of Docker Compose and how it can be used to define and orchestrate multi-container applications.