美文网首页Docker
Docker-compose - build a test cl

Docker-compose - build a test cl

作者: 红薯爱帅 | 来源:发表于2018-12-23 22:35 被阅读0次

1. Installation

  • Install docker-ce and docker-compose
#!/usr/bin/env bash
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y curl
apt-get install -y software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update
apt-get install -y docker-ce

sudo curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

2. Prepare

  • dirs
$ tree .
.
├── docker-compose.yml
├── dockerfile
├── requirements.txt
├── run.py
└── web-data
    └── web.log
  • run.py
from flask import Flask
from redis import StrictRedis
import os


env = os.environ.get
REDIS_URI = env('REDIS_URI', '')
redis = StrictRedis.from_url(REDIS_URI)

app = Flask(__name__)


@app.route('/')
def hello():
    redis.incr('hits')
    log = 'I have been seen %s times.\n' % redis.get('hits')
    with open('./web-data/web.log', 'a') as f:
        f.write(log)
    return log
    

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)

  • dockerfile
FROM python:2.7

RUN mkdir -p /code
WORKDIR /code
COPY . /code

RUN pip install -r requirements.txt
CMD python run.py
  • requirements.txt
flask==1.0.2
redis==3.0.1

3. docker-compose

  • build docker image
$ docker build -t web .
  • docker-compose.yml
version: '2.1'
services:
  web:
    # build: .
    image: web
    container_name: web
    ports:
      - "5000:5000"
    volumes:
      - ./web-data:/code/web-data
    depends_on:
      - redis
    # restart: always
    environment:
      - REDIS_URI=redis://:my-password@redis:6379/0
    links:
      - redis
  redis:
    image: redis
    container_name: redis
    command: ['redis-server', '--requirepass my-password']
    # expose:
    #   - 6379

4. Test

  • docker-compose up
$ docker-compose up
Creating network "vm-files_default" with the default driver
Creating redis ... done
Creating web   ... done
Attaching to redis, web
redis    | 1:C 23 Dec 2018 14:11:46.646 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis    | 1:C 23 Dec 2018 14:11:46.649 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=1, just started
redis    | 1:C 23 Dec 2018 14:11:46.649 # Configuration loaded
redis    | 1:M 23 Dec 2018 14:11:46.650 * Running mode=standalone, port=6379.
redis    | 1:M 23 Dec 2018 14:11:46.650 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis    | 1:M 23 Dec 2018 14:11:46.650 # Server initialized
redis    | 1:M 23 Dec 2018 14:11:46.650 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis    | 1:M 23 Dec 2018 14:11:46.650 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis    | 1:M 23 Dec 2018 14:11:46.650 * Ready to accept connections
web      |  * Serving Flask app "run" (lazy loading)
web      |  * Environment: production
web      |    WARNING: Do not use the development server in a production environment.
web      |    Use a production WSGI server instead.
web      |  * Debug mode: on
web      |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web      |  * Restarting with stat
web      |  * Debugger is active!
web      |  * Debugger PIN: 205-936-972
web      | 192.168.112.1 - - [23/Dec/2018 14:12:01] "GET / HTTP/1.1" 200 -
web      | 192.168.112.1 - - [23/Dec/2018 14:12:11] "GET / HTTP/1.1" 200 -
web      | 192.168.112.1 - - [23/Dec/2018 14:13:19] "GET / HTTP/1.1" 200 -
web      | 192.168.112.1 - - [23/Dec/2018 14:13:27] "GET / HTTP/1.1" 200 -
  • test http, result is ok
$ http localhost:5000
HTTP/1.0 200 OK
Content-Length: 26
Content-Type: text/html; charset=utf-8
Date: Sun, 23 Dec 2018 14:25:51 GMT
Server: Werkzeug/0.14.1 Python/2.7.15

I have been seen 5 times.

5. Tips

  • Tip1, To enable experimental features, start the Docker daemon with the --experimental flag or enable the daemon flag in the /etc/docker/daemon.json configuration file. Then, could exec docker build --squash=true ...
$ sudo vi /etc/docker/daemon.json
{
    "experimental": true
}
$ sudo service docker restart
$ docker version -f '{{.Server.Experimental}}'
true
  • Tip2, To enable your-user to exec docker cmd. Need to log in again.
$ sudo usermod -aG docker $your-user
  • Tip3

The feature build: . in docker-compose.yml could build an image which name has a prefix of current dir name, be careful.

6. Refer

相关文章

网友评论

    本文标题:Docker-compose - build a test cl

    本文链接:https://www.haomeiwen.com/subject/hqpokqtx.html