IaC - Docker: Difference between revisions

From A Technological Wiki
Jump to navigation Jump to search
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<span id="what-is-docker"></span>
== What is Docker? ==
== 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.
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.


<span id="key-concepts"></span>
== Key Concepts ==
== Key Concepts ==


Line 11: Line 9:
* '''Dockerfile''': A text document that contains all the commands a user could call on the command line to assemble an image.
* '''Dockerfile''': A text document that contains all the commands a user could call on the command line to assemble an image.


<span id="installing-docker"></span>
== Installing Docker ==
== Installing Docker ==


For installing docker on Ubuntu use
For installing docker on Ubuntu use


<syntaxhighlight>sudo apt-get install docker.io</syntaxhighlight>
<syntaxhighlight lang="bash">sudo apt-get install docker.io</syntaxhighlight>
<span id="docker-compose"></span>
= Docker Compose =
= Docker Compose =


<span id="what-is-docker-compose"></span>
== What is 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.
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.


<span id="key-concepts-1"></span>
== Key Concepts ==
== Key Concepts ==


Line 31: 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.


<span id="configuring-usage-with-php-apache-container"></span>
= Configuring & Usage with php-apache Container =
= Configuring &amp; Usage with php-apache Container =


<span id="create-dockerfile-for-php-with-apache"></span>
== Create Dockerfile for PHP with Apache ==
== Create Dockerfile for PHP with Apache ==


In your project directory, create a file named Dockerfile:
In your project directory, create a file named Dockerfile:


<syntaxhighlight>Dockerfile
<syntaxhighlight lang="TypeScript">
Copy code
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 &amp;&amp; apt-get install -y \
RUN apt-get update && apt-get install -y \
     libpng-dev \
     libpng-dev \
     libonig-dev \
     libonig-dev \
Line 50: Line 41:
     zip \
     zip \
     curl \
     curl \
     unzip &amp;&amp; \
     unzip && \
     docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
     docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd


Line 64: Line 55:
# Expose port 80
# Expose port 80
EXPOSE 80</syntaxhighlight>
EXPOSE 80</syntaxhighlight>
<span id="setting-up-docker-compose.yml"></span>
 
== 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>version: '3'
<syntaxhighlight lang="yaml">version: '3'


services:
services:
Line 78: Line 69:
     container_name: php-apache-container
     container_name: php-apache-container
     ports:
     ports:
       - &quot;8080:80&quot;
       - "8080:80"
     volumes:
     volumes:
       - .:/var/www/html</syntaxhighlight>
       - .:/var/www/html</syntaxhighlight>
<span id="running-the-containers"></span>
== Running the Containers ==
== Running the Containers ==


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


<syntaxhighlight>docker-compose up</syntaxhighlight>
<syntaxhighlight lang="bash">docker-compose up</syntaxhighlight>
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.


<span id="stopping-the-containers"></span>
== Stopping the Containers ==
== Stopping the Containers ==


When you want to stop the services:
When you want to stop the services:


<syntaxhighlight>docker-compose down</syntaxhighlight>
<syntaxhighlight lang="bash">docker-compose down</syntaxhighlight>
<!-- From bodyContent 1543232.mraw -->
[[Category:Infrastructure as Code]]
 

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