Difference between revisions of "Dev:AI/Docker"

From C3LearningLabs
(docker-compose)
(docker-compose)
Line 263: Line 263:
 
:The started process will start automatic every time the computer starts.
 
:The started process will start automatic every time the computer starts.
 
:<code>sudo docker-compose -f docker-compose.yml up</code>
 
:<code>sudo docker-compose -f docker-compose.yml up</code>
 +
 +
=== Start ===
 +
 +
;Run Container
 +
:Instantiate container
 +
:Use start if you want to cotinue to work in a created instance.
 +
 +
:Anslut xserver: <code>xhost local:root</code>
 +
 +
:<code>sudo docker run -e QT_X11_NO_MITSHM=1 -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix:rw --runtime=nvidia -it risedl78/wasp_images:tf2_0_keras_tfhub_sherpa</code>
 +
 +
;Start Container
 +
:Reopen a started container instance<br>
 +
 +
:List container instances: <code>sudo docker ps -a</code>
 +
 +
:<code>sudo docker start -i <DOCKER CONTAINER ID></code><br>
 +
:Example: <code>sudo docker start -i 06b57a5a652f</code>
  
 
= LAMP - Try 1 =
 
= LAMP - Try 1 =

Revision as of 15:11, 29 November 2019

Docker

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

Install

Ubuntu

Mint see below

Update pip
sudo apt-get install python3-setuptools
sudo apt-get install -y python3-pip
sudo pip3 install --upgrade pip


Start
  • 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
    sudo 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
    User: regogranlund


Docker without root permissions
sudo groupadd docker
sudo usermod -aG docker $USER
Log out and log back in (or reboot) so that your group membership is re-evaluated. You can type groups command in a terminal window after that and check if docker group is in its output.


Install docker-compose, a tool for multi-container docker applications.
sudo apt-get install -y python3-pip
sudo pip3 install docker-compose

Mint 19.2

Update pip
sudo apt-get install python3-setuptools
sudo apt-get install -y python3-pip
sudo pip3 install --upgrade pip


  • 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
  • Set up the stable repository
    Manually uppdate the repository, the file can be empty.
    sudo nano /etc/apt/sources.list.d/additional-repositories.list
    Insert row
    deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable


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
    sudo 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 Take time.
  • Login to Docker
    sudo docker login
    User: regogranlund


Docker without root permissions
sudo groupadd docker
sudo usermod -aG docker $USER
Log out and log back in (or reboot) so that your group membership is re-evaluated. You can type groups command in a terminal window after that and check if docker group is in its output.


Install docker-compose, a tool for multi-container docker applications.
sudo pip3 install docker-compose
Or I did also try
sudo curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Start

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>
sudo docker stop [OPTIONS] CONTAINER [CONTAINER...]
sudo docker stop 003037ed7752


Images

View all images
All containers that have been pulled from repository, with the pull command.
sudo docker images
All dangling images:sudo docker images -f dangling=true


Delete Images
sudo docker rmi -f ImageID
Delete all Images, Danger
sudo docker rmi $(docker images -a -q)
Delete all dangling images that have no container process
sudo docker images purge
Delete all dangling images, containers etc that have no container process
sudo docker system prune
Delete all (dangling images) and all Images for all stopped process container
sudo docker system prune -a

Processes

View all processes (instances)
All containers that have been instantiated with the run command.
sudo docker ps -a
Stop process
sudo docker stop ContainerID
Delete process container
sudo docker rm ContainerID
Inspect container
sudo docker inspect cvat

Repositories

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
docker login --username regogranlund
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/

docker-compose

Build
sudo docker-compose -f docker-compose.yml up --no-start
Build and start
The started process will start automatic every time the computer starts.
sudo docker-compose -f docker-compose.yml up

Start

Run Container
Instantiate container
Use start if you want to cotinue to work in a created instance.
Anslut xserver: xhost local:root
sudo docker run -e QT_X11_NO_MITSHM=1 -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix:rw --runtime=nvidia -it risedl78/wasp_images:tf2_0_keras_tfhub_sherpa
Start Container
Reopen a started container instance
List container instances: sudo docker ps -a
sudo docker start -i <DOCKER CONTAINER ID>
Example: sudo docker start -i 06b57a5a652f

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


Test MariaDB

https://hub.docker.com/_/mariadb

Create work folder

sudo mkdir /opt/mariadb
sudo chmod 777 /opt/mariadb
cd /opt/mariadb


sudo docker pull mariadb


sudo docker run --name mariadb-test -e MYSQL_ROOT_PASSWORD=example -d mariadb:tag

YAML File

Create YAML file.

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

MariaDB default port: 3306

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

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

# MediaWiki with MariaDB
#
# Access via "http://localhost:8088"
#   (or "http://$(docker-machine ip):8088" if using docker-machine)
version: '3.1'
services:
  database:
    image: mariadb
    restart: always
    environment:
      # @see https://phabricator.wikimedia.org/source/mediawiki/browse/master/includes/DefaultSettings.php
      MYSQL_DATABASE: my_wiki
      MYSQL_USER: wikiuser
      MYSQL_PASSWORD: example
      MYSQL_ROOT_PASSWORD: example

    adminer:
      image: adminer
      restart: always
      ports:
        - 8080:8080

Build

??? Pröva att inte göra, inget händer
cd /opt/mariadb
sudo docker-compose -f /opt/mariadb/docker-compose.yml build

Start

cd /opt/mariadb
sudo docker-compose up

Test Connect

Test with MySql Workbench

Setup

surf to: http://localhost:8088/

  • Language: English
  • Database Host: localhost
  • Database Name: my_wiki
  • DataBase userName: wikiuser
  • DataBase password: example