Difference between revisions of "Dev:AI/Docker"

From C3LearningLabs
(LAMP - Try 1)
(build environment)
 
(29 intermediate revisions by the same user not shown)
Line 310: Line 310:
  
 
= LAMP - Try 2 =
 
= LAMP - Try 2 =
 +
 +
Did work
 +
 +
Based on: https://linuxconfig.org/how-to-create-a-docker-based-lamp-stack-using-docker-compose-on-ubuntu-18-04-bionic-beaver-linux <br>
 +
 +
== Install docker-compose ==
 +
<code>sudo apt-get install docker-compose</code>
 +
 +
== Create Folders ==
 +
=== Create docker lamp folder ===
 +
<code>sudo mkdir -p /opt/docker/lamp</code><br>
 +
<code>sudo chmod 777 -R /opt/docker</code><br>
 +
<code>cd /opt/lamp</code><br>
 +
 +
=== Create document root folder ===
 +
<code>sudo mkdir -p /opt/docker/lamp/www</code><br>
 +
<code>sudo chmod 777 /opt/docker/lamp/www</code><br>
 +
<code>cd /opt/docker/lamp/www</code><br>
 +
 +
 +
== Create yaml file ==
 +
 +
 +
Create YAML file.<br>
 +
 +
File Name: '''docker-compose.yml'''<br>
 +
Path: /opt/docker/lamp/<br>
 +
 +
<code>sudo pico /opt/docker/lamp/docker-compose.yml</code>
 +
 +
<code>sudo chmod 777 /opt/docker/lamp/docker-compose.yml</code>
 +
 +
<Source>
 +
version: '3'
 +
services:
 +
    php-apache:
 +
        build:
 +
            context: ./php-apache
 +
        ports:
 +
            - 80:80
 +
            - 443:443
 +
        volumes:
 +
            - /opt/docker/lamp/www:/var/www
 +
        links:
 +
            - 'mariadb'
 +
 +
    mariadb:
 +
        image: mariadb:10.1
 +
        ports:
 +
          - "3306:3306"
 +
        volumes:
 +
          - mariadb:/var/lib/mysql
 +
          - ./db:/var/lib/mysql
 +
        environment:
 +
            TZ: "Europe/Rome"
 +
            MYSQL_ALLOW_EMPTY_PASSWORD: "no"
 +
            MYSQL_ROOT_PASSWORD: "rootpwd"
 +
            MYSQL_USER: 'testuser'
 +
            MYSQL_PASSWORD: 'testpassword'
 +
            MYSQL_DATABASE: 'testdb'
 +
 +
volumes:
 +
    mariadb:
 +
</Source>
 +
 +
== php Extensions ==
 +
 +
<code>sudo mkdir /opt/docker/lamp/php-apache</code><br>
 +
<code>sudo chmod 777 /opt/docker/lamp/php-apache</code><br>
 +
<code>cd /opt/docker/lamp/php-apache</code><br>
 +
 +
Create docker file.<br>
 +
 +
File Name: '''Dockerfile'''<br>
 +
Path: /opt/docker/lamp/php-apache/<br>
 +
 +
<code>sudo pico /opt/docker/lamp/php-apache/Dockerfile</code>
 +
 +
<code>sudo chmod 777 /opt/docker/lamp/php-apache/Dockerfile</code>
 +
 +
<Source>
 +
FROM php:7.2.1-apache
 +
MAINTAINER egidio docile
 +
RUN docker-php-ext-install pdo pdo_mysql mysqli
 +
</Source>
 +
 +
 +
== build environment ==
 +
 +
<code>cd /opt/docker/lamp</code><br>
 +
<code>sudo docker-compose up BUILD???</code><br>
 +
 +
 +
=== Run ===
 +
 +
=== Make php info file ===
 +
 +
<code>echo "<?php phpinfo(); ?>" > /opt/docker/lamp/www/html/index.php</code>
 +
 +
== Start container ==
 +
 +
<code>sudo docker ps -a</code>
 +
 +
<code>sudo docker start -i <DOCKER CONTAINER ID></code><br>
 +
<code>sudo docker start -i 0074d6444c11</code>
 +
 +
== Commit and Push ==
 +
 +
;Commit
 +
:Commit and tag
 +
 +
:<code>sudo docker commit 0074d6444c11 regogranlund/rise:lamp_mw-lamp_running</code>
 +
 +
;Push
 +
 +
:<code>sudo docker push regogranlund/rise:lamp_mw-lamp_running</code>
 +
 +
= LAMP - Try 3 =
 +
 +
Based on: https://www.cloudreach.com/en/insights/blog/containerize-this-how-to-use-php-apache-mysql-within-docker-containers/ <br>
 +
https://qiita.com/osk_kamui/items/7587da36548bf4533824
 +
http://www.inanzzz.com/index.php/post/su76/creating-apache-mysql-and-php-fpm-containers-for-a-web-application-with-docker-compose

Latest revision as of 15:36, 12 September 2019

Docker

https://www.docker.com/get-started

Install

Ubuntu

  • Update Software Repositories
    sudo apt-get update
    sudo dpkg --configure -a
  • Uninstall Old Versions of Docker
    sudo apt-get remove docker docker-engine docker.io containerd runc


Set up the repository
  • Install packages to allow apt to use a repository over HTTPS:
    sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common


Install Docker CE
  • Update Software Repositories
    sudo apt-get update
  • Install the latest version of Docker CE and containerd
    sudo apt-get install docker-ce docker-ce-cli containerd.io


  • Check Docker Version
    docker --version
  • Start and Automate Docker
    You do not need to do this. The Docker service needs to be setup to run at startup.
    sudo systemctl start docker
    sudo systemctl enable docker
  • Login to Docker
    sudo docker login
    regogranlund

Windows

Mac

Docker Hub

https://hub.docker.com/


Commands

Run Container
Instantiate container
Use start if you want to cotinue to work in a created instance.
sudo nvidia-docker run --rm --shm-size=1g --ulimit memlock=-1 --ulimit stack=67108864 -p8000:8000 -p8001:8001 -p8002:8002 -v/home/rego/inference_server_models/models:/models nvcr.io/nvidia/tensorrtserver:19.04-py3 trtserver --model-store=/models


--rm : The instance are deleted after session ended.


Start Container
Reopen a initialized container instance
Restart a container that have been instantiated with the run command.


sudo docker start -i <DOCKER CONTAINER ID>


View all images
All containers that have been pulled from repository, with the pull command.
sudo docker images


View all processes (instances)
All containers that have been instantiated with the run command.


sudo docker ps -a


Pull
sudo docker pull [user]/[repo name]:[Tag name you want to give the image]
Example: sudo docker pull regogranlund/rise:inference_client_v1


Push
Login
docker login --username username --password password


Commit
Commit and tag
sudo docker commit [CONTAINER-ID] [user]/[repo name]:[Tag name you want to give the image]
[CONTAINER-ID] : processes (instances) : sudo docker ps -a
Example: sudo docker commit 74681ea6d8a0 regogranlund/rise:inference_client_v1


Tag
Set tag name
sudo docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST]/[USERNAME]/[Repo name]:[TAG]


Push
sudo docker push [user]/[repo name]:[TAG]
Example: sudo docker push regogranlund/rise:inference_client_v1


Copy files to Main Linux
sudo docker cp 74681ea6d8a0:/code/inference_client/inference_client_threaded_no_tf.py /home/rego/Daniel/inference_client_live_v9
sudo docker cp /home/rego/Daniel/inference_client_live_v9/inference_client_threaded_no_tf.py 74681ea6d8a0:/code/inference_client/

LAMP - Try 1

Did not work

Based on: https://medium.com/code-kings/docker-building-a-lamp-stack-9503c62d9214

Other examples: https://linuxconfig.org/how-to-create-a-docker-based-lamp-stack-using-docker-compose-on-ubuntu-18-04-bionic-beaver-linux

Install docker-compose

If Needed: sudo apt-get install docker

sudo apt-get install docker-compose

Create work folder

sudo mkdir /opt/lamp
sudo chmod 777 /opt/lamp
cd /opt/lamp

Apache, PHP, Docker File

Create main docker file.

File Name: MainLamp.DockerFile
Path: /opt/lamp/

sudo pico /opt/lamp/MainLamp.DockerFile

sudo chmod 777 /opt/lamp/MainLamp.DockerFile

FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y
RUN apt-get upgrade -y
RUN apt-get install -y apache2 
RUN apt-get install -y php 
RUN apt-get install -y php-dev 
RUN apt-get install -y php-mysql 
RUN apt-get install -y libapache2-mod-php 
RUN apt-get install -y php-curl 
RUN apt-get install -y php-json 
RUN apt-get install -y php-common 
RUN apt-get install -y php-mbstring 
RUN apt-get install -y composer
RUN curl -s "https://packagecloud.io/install/repositories/phalcon/stable/script.deb.sh" | /bin/bash
RUN apt-get install -y software-properties-common
RUN apt-get install -y php 7.2-phalcon
COPY ./php.ini /etc/php/7.2/apache2/php.ini
COPY ./slc.conf /etc/apache2/sites-available/slc.conf
COPY ./apache2.conf /etc/apache2/apache2.conf
RUN mkdir /opt/lamp
RUN rm -rfv /etc/apache2/sites-enabled/*.conf
RUN ln -s /etc/apache2/sites-available/slc.conf /etc/apache2/sites-enabled/slc.conf

CMD ["apachectl","-D","FOREGROUND"]
RUN a2enmod rewrite
EXPOSE 80
EXPOSE 443

MYSQL, Docker File

Create DB docker file.

File Name: MYSQL.DockerFile
Path: /opt/lamp/

sudo pico /opt/lamp/MYSQL.DockerFile

sudo chmod 777 /opt/lamp/MYSQL.DockerFile

FROM mysql:5.7.25
ENV MYSQL_ALLOW_EMPTY_PASSWORD='yes'
EXPOSE 3306

YAML File

Create YAML file.

File Name: docker-compose.yml
Path: /opt/lamp/

sudo pico /opt/lamp/docker-compose.yml

sudo chmod 777 /opt/lamp/docker-compose.yml

version: '3'
services:
  lamp:
    build:
      context: ./
      dockerfile: MainLamp.DockerFile
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /opt/lamp:/opt/lamp
  devmysql:
    build:
      context: ./
      dockerfile: MySQL.DockerFile
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: 'drone'
      MYSQL_ALLOW_EMPTY_PASSWORD : 'yes'
    restart: always
    volumes:
            - /opt/lamp/:/opt/lamp

YAML file explanations.

//The Docker Compose Version
version: '3'

//This is a list of the services (containers) that you will be using to run whatever application you have written.
//Let's say you are writing a simple php web app with a database. Then these are the containers that you would use
services:

//This is the name of one of your first container. You can name it whatever you want. 
//You will want to name this something memorable as you will use this name to access the box for SSH or other network activity
  lampmain:
  
//This is the build section of the Docker Compose file. This is where the docker-compose program will look if you use it's build
//command.
//Example: docker-compose build
    build:
    
//Context is the location of where your DockerFile resides on your local machine you are building the image from.
//In our example of this blog post, it would be referring to one of the DockerFile examples that was posted earlier.
      context: ./
      
//dockerfile is used if you are not using the default name of a docker file (which is just DockerFile).
//This comes in handy when you have multiple DockerFiles that you need to use in your application.
//In this example, we are using the filename MainLamp.DockerFile. You can name the file whatever you want.
      dockerfile: MainLamp.DockerFile
      
//Ports are used when you want to publish ports and allow them to be accessed by computers or services outside
//of the docker network. Since we are working on a LAMP stack that we want to access from a browser, we opened ports
//80 & 443 for http and https access.
    ports:
      - "80:80"
      - "443:443"

//Volumes are how you mount your local filesystem to the filesystem inside your container. Works the same way as SMB works
//Except a lot easier. The syntax is shown in the example below
//    - /opt/lamp/var/www/html:/var/www/html:z
    volumes:
      - /path/of/my/local/filesystem:/path/to/mount/into/containers/filesystem
      
//This is the start of the second container we have. The first was for our Apache/PHP container.
//This one below is for the MySQL container that we need for running our database. 
//Notice how some of the properties of this entry is the same as the other one, like the build statement and the ports.
  lampmysql:
    build:
      context: ./
      dockerfile: MySQL.DockerFile
    ports:
      - "3306:3306"
      
//This environment section allows you to set environment variables within the container      
    environment:
      MYSQL_ROOT_PASSWORD: 'drone'
      MYSQL_ALLOW_EMPTY_PASSWORD : 'yes'
      
//Restart can be set so if your container fails for some reason and exits, that the container will try and reboot itself.      
    restart: always
    volumes:
            - /database/path/of/my/local/filesystem:/database/path/to/mount/into/containers/filesystem

Build

cd /opt/lamp
sudo docker-compose -f /opt/lamp/docker-compose.yml build

Start

cd /opt/lamp
sudo docker-compose up

LAMP - Try 2

Did work

Based on: https://linuxconfig.org/how-to-create-a-docker-based-lamp-stack-using-docker-compose-on-ubuntu-18-04-bionic-beaver-linux

Install docker-compose

sudo apt-get install docker-compose

Create Folders

Create docker lamp folder

sudo mkdir -p /opt/docker/lamp
sudo chmod 777 -R /opt/docker
cd /opt/lamp

Create document root folder

sudo mkdir -p /opt/docker/lamp/www
sudo chmod 777 /opt/docker/lamp/www
cd /opt/docker/lamp/www


Create yaml file

Create YAML file.

File Name: docker-compose.yml
Path: /opt/docker/lamp/

sudo pico /opt/docker/lamp/docker-compose.yml

sudo chmod 777 /opt/docker/lamp/docker-compose.yml

version: '3'
services:
    php-apache:
        build:
            context: ./php-apache
        ports:
            - 80:80
            - 443:443
        volumes:
            - /opt/docker/lamp/www:/var/www
        links:
            - 'mariadb'

    mariadb:
        image: mariadb:10.1
        ports:
           - "3306:3306"
        volumes:
           - mariadb:/var/lib/mysql
           - ./db:/var/lib/mysql
        environment:
            TZ: "Europe/Rome"
            MYSQL_ALLOW_EMPTY_PASSWORD: "no"
            MYSQL_ROOT_PASSWORD: "rootpwd"
            MYSQL_USER: 'testuser'
            MYSQL_PASSWORD: 'testpassword'
            MYSQL_DATABASE: 'testdb'

volumes:
    mariadb:

php Extensions

sudo mkdir /opt/docker/lamp/php-apache
sudo chmod 777 /opt/docker/lamp/php-apache
cd /opt/docker/lamp/php-apache

Create docker file.

File Name: Dockerfile
Path: /opt/docker/lamp/php-apache/

sudo pico /opt/docker/lamp/php-apache/Dockerfile

sudo chmod 777 /opt/docker/lamp/php-apache/Dockerfile

FROM php:7.2.1-apache
MAINTAINER egidio docile
RUN docker-php-ext-install pdo pdo_mysql mysqli


build environment

cd /opt/docker/lamp
sudo docker-compose up BUILD???


Run

Make php info file

echo "<?php phpinfo(); ?>" > /opt/docker/lamp/www/html/index.php

Start container

sudo docker ps -a

sudo docker start -i <DOCKER CONTAINER ID>
sudo docker start -i 0074d6444c11

Commit and Push

Commit
Commit and tag
sudo docker commit 0074d6444c11 regogranlund/rise:lamp_mw-lamp_running
Push
sudo docker push regogranlund/rise:lamp_mw-lamp_running

LAMP - Try 3

Based on: https://www.cloudreach.com/en/insights/blog/containerize-this-how-to-use-php-apache-mysql-within-docker-containers/
https://qiita.com/osk_kamui/items/7587da36548bf4533824 http://www.inanzzz.com/index.php/post/su76/creating-apache-mysql-and-php-fpm-containers-for-a-web-application-with-docker-compose