IaC - Docker: Difference between revisions
No edit summary |
|||
| (4 intermediate revisions by the same user not shown) | |||
| Line 25: | Line 25: | ||
* \**Project: A set of services that are defined in a docker-compose.yml file. | * \**Project: A set of services that are defined in a docker-compose.yml file. | ||
= Configuring & | = Configuring & Usage with php-apache Container = | ||
== Create Dockerfile for PHP with Apache == | == Create Dockerfile for PHP with Apache == | ||
| Line 31: | Line 31: | ||
In your project directory, create a file named Dockerfile: | In your project directory, create a file named Dockerfile: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="TypeScript"> | ||
FROM php:7.4-apache | FROM php:7.4-apache | ||
# Install system dependencies and PHP extensions | # Install system dependencies and PHP extensions | ||
RUN apt-get update & | RUN apt-get update && apt-get install -y \ | ||
libpng-dev \ | libpng-dev \ | ||
libonig-dev \ | libonig-dev \ | ||
| Line 42: | Line 41: | ||
zip \ | zip \ | ||
curl \ | curl \ | ||
unzip & | unzip && \ | ||
docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd | docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd | ||
| Line 56: | Line 55: | ||
# Expose port 80 | # Expose port 80 | ||
EXPOSE 80</syntaxhighlight> | EXPOSE 80</syntaxhighlight> | ||
== Setting up docker-compose.yml == | == Setting up docker-compose.yml == | ||
Create a docker-compose.yml file in the project directory: | Create a docker-compose.yml file in the project directory: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="yaml">version: '3' | ||
services: | services: | ||
| Line 69: | Line 69: | ||
container_name: php-apache-container | container_name: php-apache-container | ||
ports: | ports: | ||
- | - "8080:80" | ||
volumes: | volumes: | ||
- .:/var/www/html</syntaxhighlight> | - .:/var/www/html</syntaxhighlight> | ||
== Running the Containers == | == Running the Containers == | ||
| Line 80: | Line 79: | ||
This will build the php-apache container and start it. Once started, you can access the PHP application on http://localhost:8080 in your browser. | This will build the php-apache container and start it. Once started, you can access the PHP application on http://localhost:8080 in your browser. | ||
== Stopping the Containers == | == Stopping the Containers == | ||
Latest revision as of 12:07, 22 October 2025
What is Docker?
Docker is a platform for developing, shipping, and running applications in containers. A container packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
Key Concepts
- Image: A lightweight, stand-alone, executable software package that includes everything needed to run a piece of software, including the code, a runtime, system tools, libraries, and settings.
- Container: A runtime instance of an image.
- Dockerfile: A text document that contains all the commands a user could call on the command line to assemble an image.
Installing Docker
For installing docker on Ubuntu use
sudo apt-get install docker.ioDocker Compose
What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a docker-compose.yml file to configure your application's services, and then, with a single command, you create and start all the services from your configuration.
Key Concepts
- \**Service: A container in production. A service only runs one image, but it codifies the way that image runs—what ports it should use, how many replicas of the container should run to have the necessary capacity, and so on.
- \**Project: A set of services that are defined in a docker-compose.yml file.
Configuring & Usage with php-apache Container
Create Dockerfile for PHP with Apache
In your project directory, create a file named Dockerfile:
FROM php:7.4-apache
# Install system dependencies and PHP extensions
RUN apt-get update && apt-get install -y \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
curl \
unzip && \
docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Set our application folder as an environment variable
ENV APP_HOME /var/www/html
# Change the working directory to our application root
WORKDIR $APP_HOME
# Copy the content of our application to the image
COPY . $APP_HOME
# Expose port 80
EXPOSE 80Setting up docker-compose.yml
Create a docker-compose.yml file in the project directory:
version: '3'
services:
php-apache:
build:
context: .
dockerfile: Dockerfile
container_name: php-apache-container
ports:
- "8080:80"
volumes:
- .:/var/www/htmlRunning the Containers
Navigate to the directory containing the docker-compose.yml file.
docker-compose upThis will build the php-apache container and start it. Once started, you can access the PHP application on http://localhost:8080 in your browser.
Stopping the Containers
When you want to stop the services:
docker-compose down