欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker Compose 2020(1)Installation and Basic

程序员文章站 2022-03-30 19:25:04
...
Docker Compose 2020(1)Installation and Basic

Latest Docker Compose version VS Docker version
https://docs.docker.com/compose/compose-file/

On my server, check the docker version
> docker --version
Docker version 19.03.6, build 369ce74a3c

Open my docker 2375 ports if we need
> sudo vi /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H tcp://10.132.156.80:2375 -H unix:///var/run/docker.sock -H fd:// --containerd=/run/containerd/containerd.sock
> sudo systemctl daemon-reload
> sudo systemctl restart docker

I use portainer to clean up all the images and containers and other resources.

My testing server even more up to date
> docker --version
Docker version 19.03.8, build afacb8b

If we do not have docker installed, we may use this script
> curl -sSL https://get.docker.com/ | sh

Verify if my version is cool
> docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world

Check the latest version for compose https://github.com/docker/compose/releases/

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

Check version
> docker-compose --version
docker-compose version 1.25.4, build 8d51620a

Do a simple Sample
> mkdir compose-sample
> cd compose-sample/
> mkdir src
> mkdir docker
Python source
> vi src/app.py
from flask import Flask
from redis import Redis
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
@app.route('/')
def hello():
        count = redis.incr('hits')
        return 'hello, Carl. I see you {} times.\n'.format(count)
if __name__ == "__main__":
        app.run(host="0.0.0.0", debug=True)

A simple flask server increase the redis count.

Running requirement file
> vi src/requirements.txt
flask
redis

Prepare the Docker Image
> mkdir docker/web
> vi docker/web/Dockerfile
FROM python:3.4
ADD . /docker
ADD ../../src /src
WORKDIR /src
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

../../src is wrong in the Dockerfile, we can fix that later.

> vi docker/docker-compose.yml
version: '3'
services:
  web:
    build: ./web/
    ports:
     - "5000:5000"
  redis:
    image: "redis:3.0.7"

Change to use volumes
version: '3'
services:
  web:
    build: ./web/
    ports:
     - "5000:5000"
    volumes:
     - ../src:/src
  redis:
    image: "redis:3.0.7"

> cat docker/web/Dockerfile
FROM python:3.4
WORKDIR /src
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

Try to run that
> docker-compose up

Not working, change to this
version: '3'
services:
  web:
    build: ./web/
    ports:
     - "5000:5000"
    volumes:
     - /home/carl/work/compose-sample/src:/src
  redis:
    image: "redis:3.0.7"

Check logging
> docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
f74e3bacc623        redis:3.0.7         "docker-entrypoint.s…"   8 seconds ago       Up 6 seconds               6379/tcp            docker_redis_1
85dfcacda306        docker_web          "python3"                8 seconds ago       Exited (0) 6 seconds ago                       docker_web_1

Try to start the web and check why, it seems the src is not there at all.
> docker run -it --name docker_web1 docker_web:latest /bin/bash

Adjust the directory from the example as follow:
web/Dockerfile
web/src/app.py
web/src/requirements.txt
docker-compose.yml

> docker-compose up

Visit the page, it works pretty well.
http://rancher-home:5000/

References:
https://beginor.github.io/2017/06/08/use-compose-instead-of-run.html
https://docs.docker.com/compose/compose-file/
https://blog.csdn.net/pushiqiang/article/details/78682323