Category: Docker

  • Docker compose start container on boot

    I have a docker container, that i need to start on server boot.

    The docker-compose.yml file i used to create this docker container is

    [root@cp03 serverok-rubycms]# cat docker-compose.yml 
    version: '3'
    services:
      web:
        image: serverok/rubycms:1.0
        command: bash -c "rm -f tmp/pids/server.pid && cd /myapp && source /etc/profile.d/rvm.sh && script/server -e production"
        volumes:
          - /home/naiwnyc/cms:/myapp
        ports:
          - "3000:3000"
    [root@cp03 serverok-rubycms]# 
    

    With above docker-compose.yml file. i have to start docker container manually after server reboot.

    To make it auto start, add the line

        restart: always
    

    Here is the modified docker-compose.yml file.

    [root@cp03 serverok-rubycms]# cat docker-compose.yml 
    version: '3'
    services:
      web:
        image: serverok/rubycms:1.0
        restart: always
        command: bash -c "rm -f tmp/pids/server.pid && cd /myapp && source /etc/profile.d/rvm.sh && script/server -e production"
        volumes:
          - /home/naiwnyc/cms:/myapp
        ports:
          - "3000:3000"
    [root@cp03 serverok-rubycms]#
    

    You need to rebuild docker container based on this docker-compose.yml file

    Change to the folder where your docker-compse.yml file is, in my case it was

    cd  ~/serverok-rubycms
    

    Run

    docker-compose down
    docker-compose up -d
    
  • docker-compose: error while loading shared libraries

    When running docker-compose on CentOS 7, i get following error

    [root@cp03 serverok-rubycms]# docker-compose up 
    docker-compose: error while loading shared libraries: libz.so.1: failed to map segment from shared object: Operation not permitted
    [root@cp03 serverok-rubycms]#
    

    To fix the error, do the following

    mv /usr/local/bin/docker-compose /usr/local/bin/docker-compose-bin
    

    Now create a new file

    vi /usr/local/bin/docker-compose
    

    Add following content

    #!/bin/bash
    
    export TMPDIR=/opt/compose-tmp
    /usr/local/bin/docker-compose-bin "$@"
    

    Make it executable

    chmod 755 /usr/local/bin/docker-compose
    

    Create temp folder

    mkdir -p /opt/compose-tmp
    

    Now docker-compose will work.

  • Create Python Flask Docker Container

    Create Python Flask Docker Container

    Create a folder and change to the folder

    mkdir python-flask
    cd python-flask
    

    Create file

    vi Dockerfile
    

    Add following content

    FROM python:2.7-slim
    
    WORKDIR /app
    COPY . /app
    
    RUN pip install -r requirements.txt
    
    EXPOSE 8080
    
    CMD ["python", "app.py"]
    

    Create file requirements.txt, add “Flask” to it.

    echo "Flask" > requirments.txt
    

    Now lets create our Python Flask Application

    vi app.py
    

    Add following content

    from flask import Flask
    import os
    import socket
    
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
    	return  "

    Ok, World

    " if __name__ == "__main__": app.run(host="0.0.0.0", port=8080)

    To test Flask application locally, install Flask using pip

    sudo pip install Flask
    

    Now run the application using

    python app.py
    

    Now you will be able to see web application at

    http://localhost:8080
    

    Press CTRL+C to stop the application.

    To build Docker image, run

    docker build -t python-flask-app .
    

    -t specify tag.

    If all worked properly, you will see

    You can see the container image listed

    docker images
    

    Now your docker image is ready. To build a container using this image, run

    docker container run -d --name my-flask-app -p 8080:8080 python-flask-app
    

    You can access your python application running inside docker container on URL

    http://localhost:8080
    

    To stop the application, run

    docker stop my-flask-app
    

    To start it, run

    docker start my-flask-app
    
  • Create Kubernetes cluser using gcloud

    Create Kubernetes cluser using gcloud

    Login to gcloud with

    gcloud auth login
    

    Set default region

    gcloud config set compute/zone us-central1-a
    

    To create a cluser, run

    gcloud container clusters create CLUSTER-NAME
    

    To create a cluster with 2 nodes, run

    gcloud container clusters create CLUSTER-NAME --num-nodes 2
    

    To specify compute engine type during cluster creation, run

    gcloud container clusters create CLUSTER-NAME --num-nodes 2 --machine-type n1-standard-2
    

    You can also specify scopes, this allow you to give the cluster permission to access other google cloud resources. For example to allow cluster access Cloud Source Repositories and Google Container Registry. run

    gcloud container clusters create CLUSTER-NAME --num-nodes 2 --machine-type n1-standard-2 --scopes "https://www.googleapis.com/auth/projecthosting,cloud-platform"
    

    To list your container, run

    gcloud container clusters list
    

    gcloud clusters

    Before you can use cluser, you need to get authentication credentials with command

    gcloud container clusters get-credentials CLUSTER-NAME
    

    To interact with Kubernetes, you need kubectl, on Ubuntu, yu can install it with

    sudo snap install kubectl --classic
    

    To deploy a container, run

    kubectl run hello-server --image=gcr.io/google-samples/hello-app:1.0 --port 8080
    

    This will deploy google provided hello-app container to the cluser.

    To make the application available to public, create a loadbalancer service.

    kubectl expose deployment hello-server --type="LoadBalancer"
    

    Now find IP of the LoadBalacner service with command

    kubectl get services
    

    From the result, we found EXTERNAL-IP for LoadBalancer is 35.232.226.216. To access the application, go to url


    http://35.232.226.216:8080

    See Kubernetes

  • Docker container start on boot

    Docker container start on boot

    To set a docker container to start on system reboot, run

    docker update --restart=unless-stopped CONTAINER_ID
    

    Replace CONTAINER_ID with actual ID of your container.

    You can specify

    --restart=unless-stopped
    

    On creating docker container, for example

    docker run -d -p 80:80 --restart=unless-stopped nginx
    
  • Docker Nginx Proxy

    Docker Nginx Proxy allow you to run multiple docker containers on same server behind nginx proxy. This is done using

    https://github.com/jwilder/nginx-proxy

    To do this, you need a server with port 80 and 443 unused.

    To setup nginx proxy, run following

    cd /root/
    git clone https://github.com/evertramos/docker-compose-letsencrypt-nginx-proxy-companion.git
    cd docker-compose-letsencrypt-nginx-proxy-companion
    cp .env.sample .env
    ./start.sh
    

    This will start nginx proxy. You can modify .env file if you want.

    Starting a Docker Web App Behind Proxy

    To start a web app, all you need is to start docker container on same network as nginx proxy. By default it is “webproxy”.

    Here is an example command to start a web server.

    docker run -d 
        -e VIRTUAL_HOST=test.serverok.in \
        -e LETSENCRYPT_HOST=test.serverok.in \
        -e [email protected] \
        --network=webproxy \
        --name my_app \
        httpd:alpine
    

    This will start a test web server. You need to point the domain specified to this servers IP, then only nginx proxy can get LetsEncrypt SSL installed.

    Replace test.serverok.in with your actual domain.

    If you don’t want LetsEncrypt SSL installed, you can remove following 2 options

        -e LETSENCRYPT_HOST=test.serverok.in \
        -e [email protected] \
    
  • Docker Build

    Build a docker container with Apache

    docker build command is used to create a docker image from Dockerfile.

    To create an image, first lets create a working directory and change to the directory.

    mkdir ~/my-docker-image
    cd ~/my-docker-image
    

    Create a file with name Dockerfile

    vi Dockerfile
    

    Put following content in it

    FROM ubuntu:18.04
    
    RUN apt update && apt upgrade -y
    
    CMD ["/bin/bash"]
    

    Now build the docker image with

    docker build -t my-ubuntu .
    

    This will create a docker image with name “my-ubuntu”.

    To run this, we need to create a container using this image, this can be done with command

    docker run -ti my-ubuntu
    

    Here i used -ti as we need to run interactively using terminal, this is because our the command we set to start is “/bin/bash” in the Dockerfile.

    Docker

  • Installing Docker on Ubuntu 16.04

    To install docker on Ubuntu 16.04, run

    wget -qO- https://get.docker.com/ | sh
    

    To run docker commands as your user, run

    usermod -aG docker your-user
    

    Docker

  • docker search

    docker search command allows you to search for images in docker hub.

    Example

    root@ok:~# docker search nginx
    NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
    nginx                                                  Official build of Nginx.                        8043                [OK]                
    jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1284                                    [OK]
    richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   526                                     [OK]
    jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   320                                     [OK]
    kong                                                   Open-source Microservice & API Management la…   160                 [OK]                
    webdevops/php-nginx                                    Nginx with PHP-FPM                              97                                      [OK]
    kitematic/hello-world-nginx                            A light-weight nginx container that demonstr…   95                                      
    bitnami/nginx                                          Bitnami nginx Docker Image                      44                                      [OK]
    linuxserver/nginx                                      An Nginx container, brought to you by LinuxS…   33                                      
    1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          26                                      [OK]
    tobi312/rpi-nginx                                      NGINX on Raspberry Pi / armhf                   18                                      [OK]
    wodby/drupal-nginx                                     Nginx for Drupal container image                9                                       [OK]
    webdevops/nginx                                        Nginx container                                 8                                       [OK]
    blacklabelops/nginx                                    Dockerized Nginx Reverse Proxy Server.          8                                       [OK]
    nginxdemos/nginx-ingress                               NGINX Ingress Controller for Kubernetes         8                                       
    centos/nginx-18-centos7                                Platform for running nginx 1.8 or building n…   6                                       
    nginxdemos/hello                                       NGINX webserver that serves a simple page co…   4                                       [OK]
    1science/nginx                                         Nginx Docker images that include Consul Temp…   4                                       [OK]
    behance/docker-nginx                                   Provides base OS, patches and stable nginx f…   2                                       [OK]
    pebbletech/nginx-proxy                                 nginx-proxy sets up a container running ngin…   2                                       [OK]
    toccoag/openshift-nginx                                Nginx reverse proxy for Nice running on same…   1                                       [OK]
    travix/nginx                                           NGinx reverse proxy                             1                                       [OK]
    mailu/nginx                                            Mailu nginx frontend                            0                                       [OK]
    goodguide/nginx-application-proxy                      No-configuration Nginx reverse proxy for a R…   0                                       [OK]
    ppc64le/nginx                                          Official build of Nginx.                        0                                       
    root@ok:~# 
    

    To see results with 4 or more stars, use

    docker search --filter stars=4
    

    docker

  • portainer – Web based UI for Docker

    Portainer is a web based UI for managing docker.

    https://portainer.io/

    You can run it with command

    docker volume create portainer_data
    docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
    

    Now login to portainer with url

    http://YOUR_SERVER_IP:9000

    Portainer  Docker Control Panel

    docker