2020年9月17日星期四

Docker-compose build dockerfile and run service

Ref 

https://github.com/lqwangxg/dockerstudy

STEP4:

1. Show folder and files by tree commad.

$ tree
.
├── README.md
├── api
│   ├── Dockerfile
│   ├── linkextractor.py
│   ├── main.py
│   └── requirements.txt
├── docker-compose.yml
└── www
    └── index.php

2. Show ./api/Dockerfile

$ cat api/Dockerfile
FROM       lqwangxg/python
LABEL      maintainer="lqwangxg@gmail.com"

WORKDIR    /app
COPY       requirements.txt /app/
RUN        pip install -r requirements.txt

COPY       *.py /app/
RUN        chmod a+x *.py

CMD        ["./main.py"]    

3. Show  docker-compose.yml

$ cat api/Dockerfile
version: '3'

services:
  api:
    image: study4
    build: ./api
    ports:
      - "5000:5000"
  web:
    image: php:7-apache
    ports:
      - "80:80"
    environment:
      - API_ENDPOINT=http://api:5000/api/
    volumes:
      - ./www:/var/www/html    

4. Build and run by docker-compose.yml

$ docker-compose up -d --build
Building api
Step 1/8 : FROM       lqwangxg/python
 ---> 6cf621cb1327
Step 2/8 : LABEL      maintainer="lqwangxg@gmail.com"
 ---> Using cache
 ---> 3514f593e3e4
Step 3/8 : WORKDIR    /app
 ---> Using cache
 ---> 32c31859c252
Step 4/8 : COPY       requirements.txt /app/
 ---> Using cache
 ---> 8352c208cfb7
Step 5/8 : RUN        pip install -r requirements.txt
 ---> Using cache
 ---> c61c2800b117
Step 6/8 : COPY       *.py /app/
 ---> Using cache
 ---> 9aeb4cc13650
Step 7/8 : RUN        chmod a+x *.py
 ---> Using cache
 ---> e8ee2eceb5e1
Step 8/8 : CMD        ["./main.py"]
 ---> Using cache
 ---> 85bb2729865d
Successfully built 85bb2729865d
Successfully tagged study4:latest
dockerstudy_web_1 is up-to-date
dockerstudy_api_1 is up-to-date 

5. Show Runing Container service

$ docker ps 
ONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS        PORTS                    NAMES
af1f3f14e5bf        php:7-apache        "docker-php-entrypoi…"   17 minutes ago      Up 17 minutes       0.0.0.0:80->80/tcp       dockerstudy_web_1
e5f9e49916ef        study4              "./main.py"              17 minutes ago      Up 17 minutes       0.0.0.0:5000->5000/tcp   dockerstudy_api_1 

6. Show docker images 

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
study4              latest              85bb2729865d        18 minutes ago      127MB
lqwangxg/python     latest              6cf621cb1327        6 days ago          113MB
php                 7-apache            7a935726473b        6 days ago          414MB

7. Add redis service to  docker-compose.yml

$ cat api/Dockerfile
version: '3'

services:
  api:
    image: study4
    build: ./api
    ports:
      - "5000:5000"
    enviroment:
      - REDIS_URL=redis://redis:6379
  web1:
    image: php:7-apache
    ports:
      - "80:80"
    environment:
      - API_ENDPOINT=http://api:5000/api/
    volumes:
      - ./www:/var/www/html 
  web2:
    image: php:7-apache
    build: ./www 
    ports:
      - "81:80"
    environment:
      - API_ENDPOINT=http://api:5000/api
  redis:
    image: redis

# web1:  don't build docker image, run php:7-apache directly.
# web2:  create a new docker image from php:7-apache, and run. 

8. Build and run by docker-compose.yml

$ docker-compose up -d --build
Building api
Step 1/9 : FROM       lqwangxg/python
 ---> 6cf621cb1327
Step 2/9 : LABEL      maintainer="lqwangxg@gmail.com"
 ---> Using cache
 ---> 3514f593e3e4
Step 3/9 : ENV        REDIS_URL="redis://localhost:6379"
 ---> Running in 0dd195b593ba
Removing intermediate container 0dd195b593ba
 ---> a8f8264daa08
Step 4/9 : WORKDIR    /app
 ---> Running in be0262a54304
Removing intermediate container be0262a54304
 ---> bc1e8588280a
Step 5/9 : COPY       requirements.txt /app/
 ---> 411f5b4456e8
Step 6/9 : RUN        pip install -r requirements.txt
 ---> Running in 8ab8378a058b
Collecting beautifulsoup4
...
Building web
Step 1/4 : FROM       php:7-apache
 ---> 7a935726473b
Step 2/4 : LABEL      maintainer="Sawood Alam <@ibnesayeed>"
 ---> Running in f591f0536f20
Removing intermediate container f591f0536f20
 ---> bcd8bd2a2bf3
Step 3/4 : ENV        API_ENDPOINT="http://localhost:5000/api/"
 ---> Running in 2ee1abfdbdaf
Removing intermediate container 2ee1abfdbdaf
 ---> 2a6357cb298d
Step 4/4 : COPY       . /var/www/html/
 ---> c11b8ee6d1d7
Successfully built c11b8ee6d1d7
Successfully tagged study5php:latest
Pulling redis (redis:)...
latest: Pulling from library/redis
d121f8d1c412: Already exists
2f9874741855: Pull complete
d92da09ebfd4: Pull complete
bdfa64b72752: Pull complete
e748e6f663b9: Pull complete
eb1c8b66e2a1: Pull complete
Digest: sha256:1cfb205a988a9dae5f025c57b92e9643ec0e7ccff6e66bc639d8a5f95bba928c
Status: Downloaded newer image for redis:latest
Recreating dockerstudy_web_1 ... done
Recreating dockerstudy_api_1 ... done
Creating dockerstudy_redis_1 ... done
Creating dockerstudy_web2_1  ... done 

9. Docker compose down

$ docker compose down 
Stopping dockerstudy_api_1   ... done
Stopping dockerstudy_web_1   ... done
Stopping dockerstudy_web2_1  ... done
Stopping dockerstudy_redis_1 ... done
Removing dockerstudy_api_1   ... done
Removing dockerstudy_web_1   ... done
Removing dockerstudy_web2_1  ... done
Removing dockerstudy_redis_1 ... done
Removing network dockerstudy_default


没有评论: