Containerization with Docker

Submitted on Tue, 01/21/2025 - 07:55

Tags

Dockerfile

 

Scenario 1

PHP

 

index.php

<?php

echo ‘<h1>Welcome to the world of DevOps</h1>’;

?>

 

Dockerfile

FROM php:8.2-apache

COPY index.php /var/www/html

WORKDIR /var/www/html

EXPOSE 8008

CMD ["apache2-foreground"]

 

Build docker image

docker build -t papache .

 

Run docker container

docker run -dit -p 8008:80 --name papa papache

 

Access the web app in the browser at: 

localhost:8008

!Important: Use netstat commands to check port availability in Host OS.

 

Note: By default, docker build looks for a file named Dockerfile. Any other file can be passed as follows:

docker build -f dockfile -t papache .

 

Note: Can also include 

RUN apt update && apt install -y inetutils-ping vim


Scenario 2

JAVA

 

sample.war

 

Dockerfile

FROM tomcat:9-jdk11

COPY sample.war /usr/local/tomcat/webapps/ROOT.war

EXPOSE 8080

CMD ["catalina.sh","run"]

 

docker build -t javawar .

docker run -dit -p 8080:8080 --name javasrv javawar

 

Access the web app in the browser at:

localhost:8080


Scenario 3 

Python

 

app.py

pyreq.txt

Dockerfile

 

app.py

from flask import Flask

app=Flask(__name__)

@app.route('/')

def webout():

return '<h1>DevOps is fun.</h1>'

app.run(host='0.0.0.0',port=7000)

 

pyreq.txt

Flask==2.0.3

Werkzeug==2.0.3

 

Dockerfile

FROM python:3.9-slim

COPY . /app

WORKDIR /app

RUN pip install --no-cache-dir -r pyreq.txt

EXPOSE 7000

CMD ["python","app.py"]

 

Build docker images

docker build -t apurwasingh/flask .

 

Run docker container

docker run -dit -p 8000:7000 --name flask apurwasingh/flask

 

Access the web app in the browser at:

localhost:8000


Pushing docker images to Docker Hub

  1. Log into docker hub

docker login -u apurwasingh

  1. Input password
  2. Check login info with

docker info | grep -i username

  1. Push local image to docker hub

docker push apurwasingh/flask

!Important: Username must be part of the docker image name.

 

Logout command

docker logout


Docker Compose

For managing multi-container applications at once by using a yaml configuration file.

 

Wordpress setup

Write a docker-compose.yaml as follows:

services:

 wordpress:

  image: wordpress

  container_name: wordweb

  ports:

  - "8070:80"

  volumes: 

   - wordwebvol:/var/www/html

  depends_on: 

   - db

  restart: unless-stopped

 

 db:

  image: mysql:8

  container_name: worddb

  environment:

   MYSQL_ROOT_PASSWORD: nepal

   MYSQL_DATABASE: devdb

   MYSQL_USER: devuser

   MYSQL_PASSWORD: devpwd

  volumes: 

   - worddbvol:/var/lib/mysql

  restart: unless-stopped

 

volumes: 

 wordwebvol: 

 worddbvol:

 

Run

docker-compose up -d

Note: By default docker-compose looks for a file named docker-compose.yaml. Any other file can be passed as follows:

docker-compose -f server.yaml up -d

 

Access the web app in the browser at: localhost:8070

Command to take down the containers: docker-compose down


Use Case: Local deployment of a cpanel based website

Environment variables

  • Web application: Drupal 8
  • Database server version: Mariadb/Mysql 5
  • Database name: itodb
  • Database username: ito
  • Database password: itopwd
  • Database host: localhost

 

Website Backup files:

  • public_html (folder)
  • itodb.sql (database backup)

 

Step 1. Go to the folder containing the backup files and write a docker-compose.yaml file as follows:
 

services:

 drupal:

  image: drupal:8

  container_name: drupalweb

  environment:

   MYSQL_ROOT_PASSWORD: rootpwd

   MYSQL_DATABASE: itodb

   MYSQL_USER: ito

   MYSQL_PASSWORD: itopwd

  ports:

   - "80:80"

  volumes:

   - ./public_html:/var/www/html

 db:

  image: mysql:5.7

  container_name: drupaldb

  environment:

   MYSQL_ROOT_PASSWORD: rootpwd

   MYSQL_DATABASE: itodb

   MYSQL_USER: ito

   MYSQL_PASSWORD: itopwd

  volumes:

   - ./mysql_data:/var/lib/mysql

 

Step 2. Edit the configuration file in host OS at public_html/sites/default/settings.php to update the database host. 

Finally it should look like this:

 

Step 3. Run the containers with:

docker-compose up -d

 

Step 4. Run these commands to import the sql file into the mysql container:

docker cp itodb.sql drupaldb:/itodb.sql

docker exec -it drupaldb bash -c 'mysql -u root -prootpwd itodb < itodb.sql'

 

Access the web app in the browser at:

localhost


Docker Compose approach for python flask app

In the python folder write a docker-compose file as follows:

services:
  flask:
    image: apurwasingh/flask
    container_name: flask
    ports:
      - "8000:7000"

Run the container with:

docker-compose up -d