IaC - Docker

From A Technological Wiki
Revision as of 12:07, 22 October 2025 by Root (talk | contribs) (→Create Dockerfile for PHP with Apache)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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.io

Docker 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 80

Setting 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/html

Running the Containers

Navigate to the directory containing the docker-compose.yml file.

docker-compose up

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

When you want to stop the services:

docker-compose down