Show List
Docker Coding Questions
- Create a Dockerfile for a Python web application that runs on port 8080 and uses Flask as the web framework. Answer:
sqlCopy code
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8080
CMD ["python", "app.py"]
- Create a Docker Compose file that sets up a WordPress site with a MySQL database. Answer:
yamlCopy code
version: "3"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: example_password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: example_password
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: example_password
WORDPRESS_DB_NAME: wordpress
volumes:
db_data:
- Write a Dockerfile for a Java application that uses Gradle to build the application. Answer:
bashCopy code
FROM gradle:6.9.0-jdk11 AS builder
WORKDIR /app
COPY build.gradle settings.gradle gradlew ./
COPY gradle ./gradle
RUN ./gradlew build -x test --no-daemon
COPY src ./src
RUN ./gradlew build -x test --no-daemon
FROM openjdk:11.0.11-jre-slim-buster
WORKDIR /app
COPY --from=builder /app/build/libs/*.jar ./app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
- Create a Dockerfile for a Node.js application that uses Yarn to install dependencies. Answer:
sqlCopy code
FROM node:14.17.4-alpine3.14
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
COPY . .
EXPOSE 3000
CMD ["yarn", "start"]
- Create a Docker Compose file for a multi-container application that includes a web server and a database. Answer:
yamlCopy code
version: "3"
services:
web:
build: .
ports:
- "8000:8000"
depends_on:
- db
environment:
DATABASE_URL: postgresql://user:password@db:5432/dbname
db:
image: postgres:12
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: dbname
volumes:
db_data:
- Write a Dockerfile for a Go application that compiles the binary during the build process. Answer:
sqlCopy code
FROM golang:1.17.2-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o app .
FROM alpine:3.14
WORKDIR /app
COPY --from=builder /app/app .
EXPOSE 8080
CMD ["./app"]
- Create a Dockerfile for a Ruby on Rails application that uses PostgreSQL as the database. Answer:
sqlCopy code
FROM ruby:3.0.2-alpine
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN bundle install
COPY . .
RUN apk add --no-cache postgresql-dev && \
bundle exec rake assets:precompile
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
- Write a Dockerfile for a .NET Core application that uses the SDK image to build the application. Answer:
sqlCopy code
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS builder
WORKDIR /app
COPY . .
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/aspnet:5.0-alpine3.14
WORKDIR /app
COPY --from=builder /app/out .
EXPOSE 80
CMD ["dotnet", "app.dll"]
- Write a Dockerfile for a Python Flask application that uses the Alpine Linux base image. Answer:
sqlCopy code
FROM python:3.9.7-alpine
WORKDIR /app
COPY requirements.txt .
RUN apk add --no-cache gcc musl-dev libffi-dev openssl-dev && \
pip install -r requirements.txt
COPY . .
ENV FLASK_APP=app.py
EXPOSE 5000
CMD ["flask", "run", "--host", "0.0.0.0"]
- Create a Docker Compose file for a multi-container application that includes a web server, a database, and a worker. Answer:
yamlCopy code
version: "3"
services:
web:
build: .
ports:
- "8000:8000"
depends_on:
- db
- worker
environment:
DATABASE_URL: postgresql://user:password@db:5432/dbname
REDIS_URL: redis://cache:6379
db:
image: postgres:12
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: dbname
cache:
image: redis:6
worker:
build: worker
depends_on:
- db
- cache
environment:
DATABASE_URL: postgresql://user:password@db:5432/dbname
REDIS_URL: redis://cache:6379
volumes:
db_data:
- Write a Dockerfile for a Node.js application that uses the official Node.js base image. Answer:
sqlCopy code
FROM node:16.13.0-alpine3.14
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
- Create a Docker Compose file for a multi-container application that includes a web server, a database, and a message broker. Answer:
yamlCopy code
version: "3"
services:
web:
build: .
ports:
- "8000:8000"
depends_on:
- db
- broker
environment:
DATABASE_URL: postgresql://user:password@db:5432/dbname
BROKER_URL: amqp://user:password@broker:5672/
db:
image: postgres:12
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: dbname
broker:
image: rabbitmq:3.9-alpine
environment:
RABBITMQ_DEFAULT_USER: user
RABBITMQ_DEFAULT_PASS: password
volumes:
db_data:
- Write a Dockerfile for a Ruby on Rails application that uses the Alpine Linux base image. Answer:
sqlCopy code
FROM ruby:3.1.0-alpine3.14
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN apk add --no-cache build-base postgresql-dev tzdata && \
bundle install --jobs 4
COPY . .
ENV RAILS_ENV=production
ENV RAILS_SERVE_STATIC_FILES=true
ENV RAILS_LOG_TO_STDOUT=true
ENV RAILS_MAX_THREADS=5
EXPOSE 3000
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
- Create a Docker Compose file for a multi-container application that includes a web server, a database, and a Redis cache. Answer:
yamlCopy code
version: "3"
services:
web:
build: .
ports:
- "8000:8000"
depends_on:
- db
- cache
environment:
DATABASE_URL: postgresql://user:password@db:5432/dbname
REDIS_URL: redis://cache:6379
db:
image: postgres:12
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: dbname
cache:
image: redis:6
volumes:
db_data:
- Write a Dockerfile for a PHP application that uses the Apache web server. Answer:
lessCopy code
FROM php:8.1.0-apache
WORKDIR /var/www/html
COPY . .
RUN docker-php-ext-install pdo_mysql
EXPOSE 80
CMD ["apache2-foreground"]
- Create a Docker Compose file for a multi-container application that includes a web server, a database, and a background worker. Answer:
yamlCopy code
version: "3"
services:
web:
build: .
ports:
- "8000:8000"
depends_on:
- db
- worker
environment:
DATABASE_URL: postgresql://user:password@db:5432/dbname
WORKER_URL: amqp://user:password@worker:5672/
db:
image: postgres:12
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: dbname
worker:
build: worker
depends_on:
- db
- broker
environment:
DATABASE_URL: postgresql://user:password@db:5432/dbname
BROKER_URL: amqp://user:password@broker:5672/
volumes:
db_data:
Leave a Comment