- Dockerfile
FROM nginx:alpine
COPY default.conf /etc/nginx/conf.d/
COPY index.html /usr/share/nginx/html/
- 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;
}
- 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>
Building and Exporting a Docker Image to Deploy a Static Website with Apache Web Server
- Set Up the Base Apache Docker Image
sudo docker image pull httpd:latest
- Run the Container in Detached Mode
sudo docker run -d --name web-mania -p 8080:80 httpd
sudo docker ps
- 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
- 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>
- Test the Created Apache Static Website
http://127.0.0.1:8080/index.html
- 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
- Pull image
docker pull mysql/mysql-server:8.0.28
- Start MySQL container
docker run --name='my_sql_container' -d -p 3306:3306 mysql/mysql-server
- check container
docker ps
- Check Logs
docker logs my_sql_container
- Exec into container
docker exec -it my_sql_container bash
- Install psql
sudo apt install mysql-client
- Connect MySQL
cd /var/lib/mysql
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
use mysql;
select user from user;
- Create a user for connection
CREATE USER 'dbeaver'@'%' IDENTIFIED BY 'dbeaver';
GRANT ALL PRIVILEGES ON *.* TO 'dbeaver'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
- Start postgres
docker pull postgres
docker run --name my_postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
- Install client
sudo apt install postgresql-client
- Access PostgreSQL
psql -h localhost -p 5432 -U postgres
- Pull mongodb
docker pull mongo
- Run mongodb
docker run --name my_mongodb -d -p 27017:27017 mongo
# 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
- Create Docker Networks
docker network create postgres_network
docker network create mysql_network
docker network create mongo_network
- 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
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
Dockerize following php application docker-php-sample Public
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 .