Skip to content

Latest commit

 

History

History
308 lines (256 loc) · 6.62 KB

hol-all-docker-basics.md

File metadata and controls

308 lines (256 loc) · 6.62 KB

Example 1

  1. Dockerfile
FROM nginx:alpine

COPY default.conf /etc/nginx/conf.d/
COPY index.html /usr/share/nginx/html/
  1. default.conf
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    ssi on;
}
  1. index.html
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>My wonderful website</title>
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
        <h1>Welcome to my wonderful website</h1>
        <p>You will never find another place as cool as this</p>
        <p><img src="img/bridge.jpg" alt="A nice picture of The Bridge"/></p>
    </body>
</html>

Example 2

Building and Exporting a Docker Image to Deploy a Static Website with Apache Web Server

  1. Set Up the Base Apache Docker Image
sudo docker image pull httpd:latest
  1. Run the Container in Detached Mode
sudo docker run -d --name web-mania -p 8080:80 httpd
sudo docker ps
  1. Start an Interactive Terminal to Access the Created Container
sudo docker container exec -it web-mania /bin/bash

Install vi or nano

apt-get update;apt-get install nano
  1. Create the index.html File
nano /usr/local/apache2/htdocs/index.html

Content

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Web-Mania!!!</title>
</head>
<body>
    <h1>This is a test landing page for the created docker image</h1>
</body>
</html>
  1. Test the Created Apache Static Website
 http://127.0.0.1:8080/index.html
  1. Create a Custom Docker Image from the Created Container
sudo docker commit web-mania
sudo docker images
sudo docker image tag c9917b2064a6 docker-mania-apache:1.0

This created image can then be exported and distributed as needed by using the docker save command

sudo docker save docker-mania-apache:1.0 | gzip > docker-mania-apache.tar.gz

Example 3

Connect various databases

MySQL

  1. Pull image
docker pull mysql/mysql-server:8.0.28
  1. Start MySQL container
docker run --name='my_sql_container' -d -p 3306:3306 mysql/mysql-server
  1. check container
docker ps
  1. Check Logs
docker logs my_sql_container
  1. Exec into container
docker exec -it my_sql_container bash
  1. Install psql
sudo apt install mysql-client
  1. Connect MySQL
cd /var/lib/mysql
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
use mysql;
select user from user;
  1. Create a user for connection
CREATE USER 'dbeaver'@'%' IDENTIFIED BY 'dbeaver';
GRANT ALL PRIVILEGES ON *.* TO 'dbeaver'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Postgres

  1. Start postgres
docker pull postgres
docker run --name my_postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
  1. Install client
sudo apt install postgresql-client
  1. Access PostgreSQL
psql -h localhost -p 5432 -U postgres

Mongodb

  1. Pull mongodb
docker pull mongo
  1. Run mongodb
docker run --name my_mongodb -d -p 27017:27017 mongo

Running database with volume

# PostgreSQL with Volume
docker run --name postgres_container -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 -v /path/to/postgres_data:/var/lib/postgresql/data postgres

# MySQL with Volume
docker run --name mysql_container -e MYSQL_ROOT_PASSWORD=mysecretpassword -d -p 3306:3306 -v /path/to/mysql_data:/var/lib/mysql mysql

# MongoDB with Volume
docker run --name mongo_container -d -p 27017:27017 -v /path/to/mongo_data:/data/db mongo

Running database in separate network

  1. Create Docker Networks
docker network create postgres_network
docker network create mysql_network
docker network create mongo_network
  1. Start databses
# Start PostgreSQL in the `postgres_network`:
docker run --name postgres_container --network postgres_network -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres

# Start MySQL in the `mysql_network`
docker run --name mysql_container --network mysql_network -e MYSQL_ROOT_PASSWORD=mysecretpassword -d -p 3306:3306 mysql

# Start MongoDB in the `mongo_network`:
docker run --name mongo_container --network mongo_network -d -p 27017:27017 mongo

Example 4

Postgress DB Initialization scripts

init.sql

create table sometable(id int);
docker run -v ./init.sql:/docker-entrypoint-initdb.d/init.sql p 5432:5432 postgres

More..

CREATE USER docker;
CREATE DATABASE docker;
GRANT ALL PRIVILEGES ON DATABASE docker TO docker;

Dockerfile

FROM library/postgres
COPY init.sql /docker-entrypoint-initdb.d/

Run

docker run -e POSTGRES_USER=docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=docker library/postgres

or with Dockerfile

FROM library/postgres
ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker

Challange HOL

Dockerize following php application docker-php-sample Public

Example 5

my-database.sql

CREATE TABLE student (
    id int,
    name VARCHAR(255)
);

INSERT INTO student(id, name) VALUES
(1,'A'),
(2,'B'),
(3,'C');

Mysql

FROM mysql:latest

ENV MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
ENV MYSQL_DATABASE=${MYSQL_DATABASE}
ENV MYSQL_USER=${MYSQL_USER}
ENV MYSQL_PASSWORD=${MYSQL_PASSWORD}

COPY my-database.sql /docker-entrypoint-initdb.d/

Build

docker build --build-arg MYSQL_ROOT_PASSWORD=password --build-arg MYSQL_DATABASE=my_database --build-arg MYSQL_USER=my_user --build-arg MYSQL_PASSWORD=my_password -t my-database-env .

References

  1. docker-apache-php
  2. docker commit make image
  3. docker samples
  4. Setting up and Running a MySQL Container
  5. PostgreSQL database samples with Docker
  6. docker postgresql
  7. Apache Docker Container
  8. Mysql dockerfile