This experiment focuses on understanding two fundamental approaches to container deployment in Docker: the imperative approach using Docker Run commands and the declarative approach using Docker Compose. The practical demonstrates how both methods can be used to deploy single-container and multi-container applications, highlighting their differences, advantages, and real-world usage.
The primary objective of this experiment is to:
Docker Run is an imperative way of managing containers where each configuration is specified directly in the command line. It requires users to define ports, environment variables, volumes, and container names explicitly.
Example: docker run -d -p 8081:80 nginx
Docker Compose uses a declarative YAML-based configuration file to define services. Instead of specifying everything in a command, the desired state is written in a file and executed with a single command.
Example: docker compose up -d
Docker Run:
Docker Compose:
docker run -d –name nginx-container -p 8090:80 nginx:alpine
docker ps
http://localhost:8090
services: nginx: image: nginx:alpine container_name: nginx-compose ports: - “8091:80”
docker compose up -d
http://localhost:8091
services: db: image: mysql:5.7 container_name: mysql-db environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: wordpress
wordpress: image: wordpress:latest container_name: wordpress-app ports: - “8092:80” environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: root WORDPRESS_DB_PASSWORD: root depends_on: - db
docker compose up -d
http://localhost:8092
docker volume ls
docker compose down
docker stop
docker rm
FROM node:18-alpine WORKDIR /app CMD [“node”, “-e”, “console.log(‘Hello from container’)”]
Docker Compose provides a structured and scalable approach compared to Docker Run and simplifies multi-container deployment significantly.