Visualizzazione post con etichetta container. Mostra tutti i post
Visualizzazione post con etichetta container. Mostra tutti i post

mercoledì 11 aprile 2018

My Django Docker image


A description, step by step of how I builded my docker django image, how i loaded it on docker hub and how can be use it and customized.

STEP 1 — Create Dockerfile and push it on Docker Hub

In a folder I create a file named Dockerfile and an another named requirements.txt
Dockerfile
FROM python:2.7
LABEL maintainer "Pierangelo Orizio <pierangelo1982@gmail.com>"
RUN apt-get update -qq && apt-get install build-essential g++ flex bison gperf ruby perl \
  mysql-client \ 
  libsqlite3-dev libmysqlclient-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev \
  postgresql-client \
  libpng-dev libjpeg-dev python libx11-dev libxext-dev -y
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
COPY . /code
RUN pip install -r requirements.txt
#ADD . /code/
RUN django-admin.py startproject djangoproject
RUN mkdir /code/djangoproject/media && mkdir /code/djangoproject/static
VOLUME /code
WORKDIR /code/djangoproject
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
requirements.txt
Django==1.9.8
django-filer==1.2.5
django-filter==1.0.0
django-image-cropping==1.0.4
django-mptt==0.8.6
from terminal inside the folder, I build the local image:
docker build -t django .
tag the local image:
docker tag django pierangelo1982/django
sign in to your docker account:
docker login
push the image on your Docker Hub account:
docker push pierangelo1982/django
P.S: pierangelo1982 is my docker account username, change it with yours.

STEP 2 — create and implement container

pull the image:
docker pull pierangelo1982/django
For work and implement our project, we have need to share the project folders of the container on our local host. For do that we have need to build a volume and copy the file.
create a volume:
docker volume create --name django-volume
connect the volume to the container for can copy the project folder
docker run --name django-test \
 -v django-volume:/code \
 -p 8000:8000 \
 -d pierangelo1982/django
copy project folders in your host
docker cp django-test:/code ~/my/local/path/myfolder
remove the container
docker rm -f django-test
recreate the container with the volume that point to our local folder where before we have copy the folders of the project.
docker run --name django-test \
 -v ~/my/local/path/myfolder:/code \
 -p 8000:8000 \
 -d pierangelo1982/django

Using django and python commands:

docker exec django-test python manage.py

how install new packages:

docker exec django-pierangelo pip install
Source code:

Docker Hub:

Subscribe my weekly newsletter:

Bye!

martedì 10 aprile 2018

How to install a Jenkins Docker image with Chef

Inside the cookbooks folder in our chef-repo (read here for more info about chef-repo and here for open a free account on chef-server) we must create a cookbook named mydocker:
chef generate cookbook mydocker
in /mydocker folder we edit metadata.rb file and insert this:
depends 'docker', '~> 2.0'
edit default.rb in /mydocker/recipe/ and add this:
## install and start docker
docker_service 'default' do
  action [:create, :start]
end
## create a folder for volumes docker jenkins
execute "run a script" do
  user "root"
  command <<-EOH
  mkdir -p /home/jenkins_home/secrets/ /
  chmod -R 777 /home/jenkins_home/
  EOH
end
## pull the jenkins image
docker_image 'jenkins' do
    tag 'latest'
    action :pull
end

## run container
docker_container 'my_jenkins' do
    repo 'jenkins'
    tag 'latest'
    port '50000:50000'
    port '8081:8080'
    volumes "/home/jenkins_home/secrets/:/var/jenkins_home/secrets/"
end
with the terminal, inside the mydocker folder upload the cookbook on your chef-server:
berks install
berks upload
create a node relative to your machine (vps, vagrant etc…), for more info about this step read here:
knife bootstrap 127.0.0.1 --ssh-port 2200 --ssh-user myuser --sudo --identity-file /my/path/private_key -N nameofYourMachine
now add the mydocker cookbook to your node:
knife node run_list add nameOfYourNode "recipe[mydocker]"
From terminal, connected in ssh at your machine, launch chef-client command:
sudo chef-client
Now if we connect to the ip of the machine, at the port :8081 we shoud must see Jenkins run and ask us to insert the password:
we can find the password in our docker volumes; from terminal launch this command:
less /home/jenkins_home/secrets/initialAdminPassword
and we can see the password for jenkins installation:
Good Luck!

lunedì 9 aprile 2018

cooking Docker Nginx with Chef Server


from pexels.com
In the last couple of weeks, I started to study Chef; install Docker with Chef was one of my first steps into the wide world of server automation.
From your chef-server you must download your chef-repo.
Go inside the chef-repo folder and generate a cookbook named mydocker:
generate cookbook cookbooks/mydocker
edit the file metadata.rb in cookbooks/mydocker/ and add this:
depends 'docker', '~> 2.0'
in cookbooks/mydocker/recipes edit the file default.rb and add this:
docker_service 'default' do
  action [:create, :start]
end


# Pull latest image
docker_image 'nginx' do
  tag 'latest'
  action :pull
end


# Run container exposing ports
docker_container 'my_nginx' do
  repo 'nginx'
  tag 'latest'
  port '80:80'
  volumes "/home/docker/default.conf:/etc/nginx/conf.d/default.conf:ro"
  volumes "/home/docker/html:/usr/share/nginx/html"
end

# create file default.conf for volumes doccker
template "/home/docker/default.conf" do
  source "default.conf.erb"
  #notifies :reload, "service[default]"
end

# create file index.html for volumes docker
template '/home/docker/html/index.html' do
  source 'index.html.erb'
  variables(
    :ambiente => node.chef_environment
  )
  action :create
  #notifies :restart, 'service[httpd]', :immediately
end
Now we must create two template named index.html.erb and default.conf.erb
chef generate template index.html
chef generate template default.conf
edit index.html.erb in /cookbooks/mydocker/templates
<html>
  <body>
    <h1>Hello, World!</h1>
    <h3>HOSTNAME: <%= node['hostname'] %></h3>
    <h3>IPADDRESS: <%= node['ipaddress'] %></h3>
    <p><hr></p>
    <h3>ENVIRONMENT: <%= @ambiente %></h3>
</body>
</html>
edit default.conf in /cookbooks/mydocker/templates:
server {
    listen       80;
    server_name  localhost;
    #ssl
    # ssl    on;
    #ssl_certificate    /etc/nginx/ssl/nginx.crt;
    #ssl_certificate_key    /etc/nginx/ssl/nginx.key;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
    }
link the cookbook to the node (your machine…)
knife node run_list add docker1 "recipe[mydocker]"
Now from inside the mydocker (/cookbooks/mydocker) folder we can upload the new cookbook on the chef-server:
berks install
berks upload
Now, you can go on your server, and launch chef-client for update your machine with the new configuration.
sudo chef-client
Good Luck!

venerdì 30 marzo 2018

Dockerize an existing cakephp 2.x project

Can be useful as first thing, create a mysql and a phpmyadmin instance:
docker run --name docker-mysql -v /my/path/mysql/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mypassword --restart always -d mysql
docker run --name docker-phpmyadmin -d --link docker-mysql:db -p 8081:80 -d phpmyadmin/phpmyadmin
In the root folder of your cakephp app create three file (Dockerfile, docker-compose.yml and default.conf):

Dockerfile

FROM php:5-apache
MAINTAINER pierangelo orizio <pierangelo1982@gmail.com>

WORKDIR /var/www/html

COPY ./default.conf /etc/apache2/sites-available/000-default.conf

RUN docker-php-ext-install pdo pdo_mysql

RUN apt-get update && \
  apt-get -y install curl git libicu-dev libpq-dev zlib1g-dev zip && \
  docker-php-ext-install intl mbstring pcntl pdo_mysql pdo_pgsql zip && \
  usermod -u 1000 www-data && \
  usermod -a -G users www-data && \
  chown -R www-data:www-data /var/www && \
  curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
  a2enmod rewrite


COPY . /var/www/html

docker-compose.yml

version: '2'
services:
  web:
    build: .
    ports:
      - "8090:80"
    volumes:
      - ~/my/local/path/html:/var/www/html
    external_links:
      - docker-mysql:db
    network_mode: bridge

default.conf

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/app/webroot

        <Directory /var/www/html/app/webroot>
                Options FollowSymLinks
                AllowOverride all
                Require all granted
        </Directory>

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>
from command line inside the root folder of the project we can launch this commands:
docker-compose build
docker-compose up -d
for more info take a look here:

giovedì 29 marzo 2018

Dockerize an existing Laravel application with docker-compose

in the main folder of your Laravel app, create a file named Dockerfile and insert this code:
FROM php:7
RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo pdo_mysql
WORKDIR /app
COPY . /app
RUN composer install
CMD php artisan serve --host=0.0.0.0 --port=8181
EXPOSE 8181
In the same main folder of Dockerfile, create a file named docker-compose.yml and insert this code:
version: '2'
services:
  app:
    build: .
    ports:
      - "8009:8000"
    volumes:
      - .:/app
    env_file: .env
    working_dir: /app
    command: bash -c 'php artisan migrate && php artisan serve --host 0.0.0.0'
    depends_on:
      - db
    links:
      - db
  db:
    image: "mysql:5.7"
    environment:
      - MYSQL_ROOT_PASSWORD=yourpassword
      - MYSQL_DATABASE=yourdbname
      - MYSQL_USER=root
      - MYSQL_PASSWORD=yourpassword
    volumes:
      - ./data/:/var/lib/mysql
    ports:
      - "3306:3306"
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - 8090:80
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: yourpassword
Open the terminal command line and go inside the laravel folder, and launch this commands:
docker.compose build
docker-compose up -d
if have need to create and migrate the db, or use other commands, launch the Laravel commands in this way:
docker-compose run app php artisan
The app will available at the address http://0.0.0.0:8009

Django with mysql and phpmyadmin in Docker

Create a container Django in docker is a bit more complicated than other framework, because and I don’t know the why, the official Django image is deprecated from your same creators, and they advise to use an approach based on the creation of a own Dockerfile and docker-compose.
So we start creating a new empty folder, inside this folder we create a file named Dockerfile
In Dockerfile insert this:
FROM python:2.7
 ENV PYTHONUNBUFFERED 1
 RUN mkdir /code
 WORKDIR /code
 ADD requirements.txt /code/
 RUN pip install -r requirements.txt
 ADD . /code/
Now we go to the same folder to create a file called requirements.txt, and in this file we are going to include all the Django packages and libraries which we need in our project (it is the same thing of the command pip install).
Here an example of a file requirements.txt
Django==1.9.8
argcomplete==1.8.2
autopep8==1.2.4
beautifulsoup4==4.5.3
chardet==2.3.0
click==6.7
configparser==3.5.0
Django==1.9.8
django-appconf==1.0.1
django-carton==1.2.1
django-filer==1.2.5
django-filter==1.0.0
django-grappelli==2.9.1
django-image-cropping==1.0.4
django-mptt==0.8.6
Now ever in the same folder we go to create a file named docker-compose.yml, and we going the elements that we want run in docker, so our Django instance, a Mysql Database and a phpmyadmin.
Here how:
version: '2'
services:
  db:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: mypassword
      MYSQL_USER: root
      MYSQL_PASSWORD: mypassword
      MYSQL_DATABASE: django-test
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      - PMA_ARBITRARY=1
    restart: always
    ports:
      - 8082:80
    volumes:
      - /sessions
Now open the terminal, go inside the folder and launch this series of commands:
For create the instance:
docker-compose build
Now we check that instances are running:
docker-compose up
Now CTRL-C for stop the instance, and we go now to create a Django project inside the instance, in this way:
docker-compose run web django-admin.py startproject composeexample .
After we can start all the instance in daemon mode, with this:
docker-compose up -d
if it’s all ok, with the command docker ps we should get this in terminal:
And on our browser, at the address http://0.0.0.0:8000/
On the port :8082 (http://0.0.0.0:8082/) you will find phpmyadmin:
I hope that this post can be useful to someone, and for more info take a look at my repository here:

My weekly newsletter

Have a nice day!

Install Elasticsearch on Ubuntu

ELK STACK install Java sudo apt-get install default-jdk Elasticsearch 6 wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch...