Docker学习(9) 使用Docker Compose部署应用

2019-10-29  本文已影响0人  August________

Docker学习(9) 使用Docker Compose部署应用

使用Docker Compose部署应用——简介

使用Docker Compose部署应用——详解

Docker Compose的背景

安装Docker Compose

lhf@lhf-virtual-machine:~$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   617    0   617    0     0    326      0 --:--:--  0:00:01 --:--:--   326
100 15.4M  100 15.4M    0     0   874k      0  0:00:18  0:00:18 --:--:-- 1358k
lhf@lhf-virtual-machine:~$ sudo chmod  +x /usr/local/bin/docker-compose 
lhf@lhf-virtual-machine:~$ docker-compose --version
docker-compose version 1.24.1, build 4667896b

Compose文件

lhf@lhf-virtual-machine:~/docker/counter-app-master$ cat docker-compose.yml 
version: "3.5"
services:
  web-fe:
    build: .
    command: python app.py
    ports:
      - target: 5000
        published: 5000
    networks:
      - counter-net
    volumes:
      - type: volume
        source: counter-vol
        target: /code
  redis:
    image: "redis:alpine"
    networks:
      counter-net:

networks:
  counter-net:

volumes:
  counter-vol:

使用Docker Compose部署应用

lhf@lhf-virtual-machine:~/docker/counter-app-master$ ls -l
总用量 20
-rw-rw-r-- 1 lhf lhf 599 10月 23 23:11 app.py
-rw-rw-r-- 1 lhf lhf 367 10月 23 23:11 docker-compose.yml
-rw-rw-r-- 1 lhf lhf 109 10月 23 23:11 Dockerfile
-rw-rw-r-- 1 lhf lhf 128 10月 23 23:11 README.md
-rw-rw-r-- 1 lhf lhf  11 10月 23 23:11 requirements.txt

lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose up  -d prod-equus-bass.yml -d
[1] 2141
lhf@lhf-virtual-machine:~/docker/counter-app-master$ Creating network "counter-app-master_counter-net" with the default driver
Creating volume "counter-app-master_counter-vol" with default driver
Building web-fe
Step 1/5 : FROM python:3.4-alpine

lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker image ls
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
counter-app-master_web-fe            latest              5378f5a84df1        29 minutes ago      84.5MB
multi                                stage               8b69a716c1a2        5 hours ago         210MB
<none>                               <none>              ffd654cfccfe        5 hours ago         757MB
<none>                               <none>              639a8715b042        6 hours ago         1.1GB
lhfdocker/web                        latest              983497a6f68f        32 hours ago        71.4MB
web                                  latest              983497a6f68f        32 hours ago        71.4MB
node                                 latest              4ac0e1872789        4 days ago          933MB
redis                                alpine              6f63d037b592        6 days ago          29.3MB
alpine                               latest              965ea09ff2eb        6 days ago          5.55MB
maven                                latest              3b2476ab3d10        9 days ago          616MB
ubuntu                               latest              cf0f3ca922e0        9 days ago          64.2MB
python                               3.4-alpine          c06adcf62f6e        7 months ago        72.9MB
java                                 8-jdk-alpine        3fd9dd82815c        2 years ago         145MB

lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker container ls
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS                    NAMES
ca03b3e55ec2        counter-app-master_web-fe   "python app.py"          31 minutes ago      Up 31 minutes       0.0.0.0:5000->5000/tcp   counter-app-master_web-fe_1
e3a7f618d62c        redis:alpine                "docker-entrypoint.s…"   31 minutes ago      Up 31 minutes       6379/tcp                 counter-app-master_redis_1
12.png
lhf@lhf-virtual-machine:~/docker/counter-app-master$ firefox localhost:5000
libGL error: No matching fbConfigs or visuals found
libGL error: failed to load driver: swrast
web-fe_1  | 172.18.0.1 - - [28/Oct/2019 15:02:10] "GET / HTTP/1.1" 200 -
web-fe_1  | 172.18.0.1 - - [28/Oct/2019 15:02:11] "GET /favicon.ico HTTP/1.1" 404 -
web-fe_1  | 172.18.0.1 - - [28/Oct/2019 15:03:56] "GET / HTTP/1.1" 200 -
web-fe_1  | 172.18.0.1 - - [28/Oct/2019 15:04:03] "GET / HTTP/1.1" 200 -
13.png

使用Dokcer Compose管理应用

lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose down
Stopping counter-app-master_web-fe_1 ... 
Stopping counter-app-master_redis_1  ... 
redis_1   | 1:signal-handler (1572275254) Received SIGTERM scheduling shutdown...
redis_1   | 1:M 28 Oct 2019 15:07:34.876 # User requested shutdown...
redis_1   | 1:M 28 Oct 2019 15:07:35.029 * Saving the final RDB snapshot before exiting.
redis_1   | 1:M 28 Oct 2019 15:07:35.031 * DB saved on disk
Stopping counter-app-master_web-fe_1 ... done
counter-app-master_redis_1 exited with code 0
counter-app-master_web-fe_1 exited with code 0
Removing counter-app-master_web-fe_1 ... done
Removing counter-app-master_redis_1  ... done
Removing network counter-app-master_counter-net
[1]+  已完成               docker-compose up

lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker volume ls
DRIVER              VOLUME NAME
local               1c7824e5eccb82556e165af9b773cb7a10b4aa3fdebec6ac767b1591cb292692
local               counter-app-master_counter-vol

lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose up -d
Creating network "counter-app-master_counter-net" with the default driver
Creating counter-app-master_redis_1  ... done
Creating counter-app-master_web-fe_1 ... done
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose ps
           Name                          Command               State           Ports         
---------------------------------------------------------------------------------------------
counter-app-master_redis_1    docker-entrypoint.sh redis ...   Up      6379/tcp              
counter-app-master_web-fe_1   python app.py                    Up      0.0.0.0:5000->5000/tcp

lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose top
counter-app-master_redis_1
UID   PID    PPID   C   STIME   TTY     TIME         CMD     
-------------------------------------------------------------
999   3750   3712   0   23:11   ?     00:00:00   redis-server

counter-app-master_web-fe_1
UID    PID    PPID   C   STIME   TTY     TIME                    CMD                
------------------------------------------------------------------------------------
root   3787   3743   0   23:11   ?     00:00:01   python app.py                     
root   3939   3787   1   23:11   ?     00:00:01   /usr/local/bin/python /code/app.py

lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose top
counter-app-master_redis_1
UID   PID    PPID   C   STIME   TTY     TIME         CMD     
-------------------------------------------------------------
999   3750   3712   0   23:11   ?     00:00:00   redis-server

counter-app-master_web-fe_1
UID    PID    PPID   C   STIME   TTY     TIME                    CMD                
------------------------------------------------------------------------------------
root   3787   3743   0   23:11   ?     00:00:01   python app.py                     
root   3939   3787   1   23:11   ?     00:00:01   /usr/local/bin/python /code/app.py
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose stop
Stopping counter-app-master_web-fe_1 ... done
Stopping counter-app-master_redis_1  ... done
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose ps
           Name                          Command               State    Ports
-----------------------------------------------------------------------------
counter-app-master_redis_1    docker-entrypoint.sh redis ...   Exit 0        
counter-app-master_web-fe_1   python app.py                    Exit 0        
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker container ls -a
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                          PORTS                  NAMES
728c56f892d0        counter-app-master_web-fe   "python app.py"          5 minutes ago       Exited (0) About a minute ago                          counter-app-master_web-fe_1
321c0e64e8bb        redis:alpine                "docker-entrypoint.s…"   5 minutes ago       Exited (0) About a minute ago                          counter-app-master_redis_1
eefa35f2eba5        lhfdocker/web:latest        "node ./app.js"          32 hours ago        Exited (255) 9 hours ago        0.0.0.0:80->8080/tcp   c1

lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose restart
Restarting counter-app-master_web-fe_1 ... done
Restarting counter-app-master_redis_1  ... done
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose ps
           Name                          Command               State           Ports         
---------------------------------------------------------------------------------------------
counter-app-master_redis_1    docker-entrypoint.sh redis ...   Up      6379/tcp              
counter-app-master_web-fe_1   python app.py                    Up      0.0.0.0:5000->5000/tcp

使用Docker Compose部署应用的——命令

上一篇下一篇

猜你喜欢

热点阅读