IaC - Docker: Difference between revisions
No edit summary |
No edit summary |
||
| Line 3: | Line 3: | ||
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. | ||
== Key Concepts == | == Key Concepts == | ||
| Line 10: | 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. | ||
== Installing Docker == | == Installing Docker == | ||
| Line 16: | Line 14: | ||
<syntaxhighlight lang="bash">sudo apt-get install docker.io</syntaxhighlight> | <syntaxhighlight lang="bash">sudo apt-get install docker.io</syntaxhighlight> | ||
= Docker Compose = | = Docker Compose = | ||
== 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. | ||
== Key Concepts == | == Key Concepts == | ||
| Line 30: | 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 & Usage with php-apache Container = | = Configuring & Usage with php-apache Container = | ||
== 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="dockerfiles">Dockerfile | ||
Copy code | Copy code | ||
FROM php:7.4-apache | FROM php:7.4-apache | ||
| Line 63: | Line 56: | ||
# 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>version: '3' | <syntaxhighlight lang="yml">version: '3' | ||
services: | services: | ||
| Line 85: | Line 77: | ||
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. | ||
| Line 93: | Line 85: | ||
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> | ||
[[Category:Infrastructure as Code]] | [[Category:Infrastructure as Code]] | ||
Revision as of 12:02, 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:
Dockerfile
Copy code
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/html
Running 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