docker practice record

Posted by malcome_thompson on Fri, 29 Nov 2019 17:04:41 +0100

install

Ubuntu 14.04/16.04 (install using apt get)

Refer to Appendix 2

# step 1: install some necessary system tools
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: install GPG certificate
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: write software source information
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Official source sudo add apt repository "DEB [arch = AMD64] https://download.docker.com/linux/ubuntu $(lsb_release - CS) stable"
# Step 4: update and install docker CE
sudo apt-get -y update
sudo apt-get -y install docker-ce
# Step 5: image acceleration
# File / etc/docker/daemon.json
{
  "registry-mirrors":[
    "https://registry.docker-cn.com"
  ]
}
# Step 6 start
sudo systemctl enable docker
sudo systemctl start docker
# Step 7 test for proper installation
docker run hello-world

Basic command

# docker needs to be run with root
# sudo su
# Enter root
# List mirroring
docker images
# List containers running
docker container ls
# List all containers
docker container ls -a
# Delete container
docker rm Vessel name or number
# Delete all containers
docker rm $(docker ps -a -q)
# delete mirror
docker rmi Mirror name
# Image search
docker search python
# Drag the image (if the image is not followed by a tag, the default is latest)
docker pull python:2.7
# Call up the command line of the container
docker run -it python /bin/bash

practice

nginx

# pull mirror image
docker pull nginx
# Run nginx
docker run --network host -v /host/path/nginx.config:/etc/nginx/conf.d/default.conf -d nginx
# -v host file: container file, mapping the host's file to the container
# The configuration file of nginx is mapped here
# When the container reads / etc/nginx/conf.d/default.conf, it will actually read / write the host's / host/path/nginx.config
# --network host, set network type
# By default, the container has its own independent ip address and port
# The container accesses the host port either through ip or port mapping
# Here, by configuring -- network host, the container can share the ip and port of the host
# -d. let the container run in the background

redis

# pull mirror image
docker pull redis
# Run redis
docker run --network host redis
# If you run redis cli on the host, you can access the redis of port 6379 by default

flask

  • prepare documents

    1. File directory
    # File directory structure
    .
    ├── app
    │   ├── app.py
    │   └── run.fcgi
    └── img
         ├── Dockerfile
         └── requirements.txt
    

    Files in img directory are used to create new images The file in app directory is the flash application

    1. Dockerfile
    FROM python
    COPY . /app
    WORKDIR /app
    RUN pip install --no-cache-dir -r requirements.txt
    

    FROM python means to create a new image based on the image python Copy. / APP copy files to the image WORKDIR /app set working directory RUN runs the command pip to install the required python package,

    1. requirements.txt, python package required, increase or decrease as needed
    redis
    mysqlclient
    flask
    flask_restful
    flask-sqlalchemy
    flup
    
    1. Application of flask
    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
        return "Hello, flask!"
    
    if __name__=='__main__':
        app.run(host='0.0.0.0', debug=True)
    
    1. run.fcgi for application deployment
    #!/usr/local/bin/python
    from flup.server.fcgi import WSGIServer
    from app import app
    
    if __name__=='__main__':
    	     WSGIServer(app, bindAddress=('127.0.0.1', 5000)).run()
    
  • Generating mirrors

    # Enter img directory
    cd img
    # Generating mirrors
    docker build -t myflask .
    # View generated images
    docker images
    
  • Running container

    1. Test version of the flash application
    docker run --network host -v /host/path/app:/app/data myflask python /app/data/app.py
    # The browser opens, and the flash application runs on the default 5000 port
    
    1. deploy
    # Give run.fcgi execute permission
    chmod +x /host/path/app/run.fcgi
    # Starting container
    docker run --network host -v /host/path/app:/app/data myflask /app/data/run.fcgi
    # At this time, the application cannot be opened in the browser. nginx is required
    

    Configuration of nginx

    location / { try_files $uri @yourapplication; }
    location @yourapplication {
        include fastcgi_params;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        fastcgi_param SCRIPT_NAME "";
        fastcgi_pass 127.0.0.1:5000;
    }
    

    Then start the container of nginx, and you can browse the flash application in the browser.

appendix

  1. Docker - from entry to practice https://yeasy.gitbooks.io/docker_practice/
  2. Docker quick start, alicloud https://help.aliyun.com/document_detail/60742.html
  3. Rookie tutorial http://www.runoob.com/docker/docker-tutorial.html

Topics: Programming Docker Nginx sudo Python