Practical Use Cases and Examples
Now that we've covered the basics of Docker and Podman, let's explore some practical use cases and examples to help you understand how these tools can be applied in real-world scenarios.
Building and Deploying a Simple Web Application
Let's start with a simple example of building and deploying a web application using Docker and Podman.
- Create a new directory for your project and navigate to it:
mkdir my-web-app
cd my-web-app
- Create a new file called
Dockerfile
with the following content:
FROM nginx:latest
COPY index.html /usr/share/nginx/html/
- Create an
index.html
file with some basic HTML content:
<h1>Hello from my web app!</h1>
- Build the Docker image:
docker build -t my-web-app .
- Run the Docker container:
docker run -d -p 8080:80 my-web-app
- Now, you can access your web application at
http://localhost:8080
.
To achieve the same result using Podman, you can follow a similar process:
- Create the
Dockerfile
and index.html
files as before.
- Build the Podman image:
podman build -t my-web-app .
- Run the Podman container:
podman run -d -p 8080:80 my-web-app
- Access your web application at
http://localhost:8080
.
Deploying a Multi-Container Application with Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. Let's see how you can use it to deploy a simple web application with a database.
- Create a new directory for your project and navigate to it:
mkdir my-app
cd my-app
- Create a new file called
docker-compose.yml
with the following content:
version: "3"
services:
web:
build: .
ports:
- "8080:80"
depends_on:
- db
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: mydb
MYSQL_ROOT_PASSWORD: mypassword
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
- Create a
Dockerfile
in the same directory with the following content:
FROM nginx:latest
COPY index.html /usr/share/nginx/html/
- Create an
index.html
file with some basic HTML content:
<h1>Hello from my web app!</h1>
- Run the application using Docker Compose:
docker-compose up -d
- Access your web application at
http://localhost:8080
.
To achieve the same result using Podman Compose (a Podman-specific tool for managing multi-container applications), you can follow a similar process:
- Create the
docker-compose.yml
, Dockerfile
, and index.html
files as before.
- Run the application using Podman Compose:
podman-compose up -d
- Access your web application at
http://localhost:8080
.
These examples should give you a good starting point for understanding how to use Docker and Podman in practical scenarios. As you continue to work with these tools, you'll discover more advanced use cases and techniques to suit your specific needs.