Docker學習筆記 - Docker Compose

一、概念

Docker Compose 用于定義運行使用多個容器的應用,可以一條命令啟動應用(多個容器)。

使用Docker Compose 的步驟:

  1. 定義容器 Dockerfile
  2. 定義應用的各個服務 docker-compose.yml
  3. 啟動應用 docker-compose up?

二、安裝

Note that Compose 1.8.0 requires Docker Engine 1.10.0 or later for version 2 of the Compose File format, and Docker Engine 1.9.1 or later for version 1.

# curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose

也可以用pip或官網的run.sh腳本安裝

安裝后確認

# docker-compose --version

三、運行

1、創建一個Python應用

? ? ? 使用Flask,將數值記入Redis

1.1 建立一個python應用目錄和文件

復制代碼
# mkdir python
# cd python# vi app.py 
from flask import Flask
from redis import Redisapp = Flask(__name__)
redis = Redis(host='redis', port=6379)@app.route('/')
def hello():redis.incr('hits')return 'Hello World! I have been seen %s times.' % redis.get('hits')if __name__ == "__main__":app.run(host="0.0.0.0", debug=True)# vi requirements.txt 
flask
redis
復制代碼

1.2 創建 Dockerfile

在同一目錄下,創建Dockerfile

# vi Dockerfile 
FROM python:2.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD python app.py

對上面的Dockerfile做一下簡單說明:

  • 容器使用Python 2.7的鏡像
  • 將當前目錄下文件拷貝到容器內/code
  • 指定工作目錄為/code
  • 安裝python需要的庫:flask, redis
  • 容器執行命令?python app.py

1.3 創建編排腳本

在同一目錄下,創建 docker-compose.yml

復制代碼
# cat docker-compose.yml 
version: '2'
services:web:build: .ports:- "5000:5000"volumes:- .:/codedepends_on:- redisredis:image: redis
復制代碼

對上面的編排腳本做一下簡單說明:

  • 這個應用定義了兩個服務:web, redis
  • web容器通過當前路徑下的Dockerfile生成
  • web容器內的5000端口映射到主機的5000端口
  • 將當前目錄掛載到web容器內/code
  • web容器依賴于redis容器
  • redis容器從Docker Hub獲取鏡像

1.4 啟動應用

會執行編排腳本,分別制作和抓取web,redis鏡像,啟動容器

# docker-compose up

1.5?訪問應用

http://localhost:5000/?

四、命令

1、daemon模式啟動/停止

# docker-compose up -d
# docker-compose stop

2 查看信息

# docker-compose ps

3 對容器執行命令(一次)

#docker-compose run services cmd

例如:查看web容器環境變量

# docker-compose run web env

五、例子

1、例一 -?docker-compose.yml

version: '2'
services:web:image: dockercloud/hello-worldports:- 8080networks:- front-tier- back-tierredis:image: redislinks:- webnetworks:- back-tierlb:image: dockercloud/haproxyports:- 80:80links:- webnetworks:- front-tier- back-tiervolumes:- /var/run/docker.sock:/var/run/docker.sock networks:front-tier:driver: bridgeback-tier:
driver: bridge

2、例二 - docker-compose.yml

復制代碼
# cat docker-compose.yml 
version: '2'
services:db:image: mysql:5.7volumes:- "./.data/db:/var/lib/mysql"restart: alwaysenvironment:MYSQL_ROOT_PASSWORD: wordpressMYSQL_DATABASE: wordpressMYSQL_USER: wordpressMYSQL_PASSWORD: wordpresswordpress:depends_on:- dbimage: wordpress:latestlinks:- dbports:- "8000:80"restart: alwaysenvironment:WORDPRESS_DB_HOST: db:3306WORDPRESS_DB_PASSWORD: wordpress
復制代碼

3、例三 - docker-compose.yml

?

轉載于:https://www.cnblogs.com/lexiaofei/p/7890249.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390732.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390732.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390732.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

創建shell腳本

1.寫一個腳本 a) 用touch命令創建一個文件:touch my_script b) 用vim編輯器打開my_script文件:vi my_script c) 用vim編輯器編輯my_script文件,內容如下: #!/bin/bash 告訴shell使用什么程序解釋腳本 #My first script l…

線性回歸算法數學原理_線性回歸算法-非數學家的高級數學

線性回歸算法數學原理內部AI (Inside AI) Linear regression is one of the most popular algorithms used in different fields well before the advent of computers. Today with the powerful computers, we can solve multi-dimensional linear regression which was not p…

您應該在2020年首先學習哪種編程語言? ????d???s????:???su?

Most people’s journey toward learning to program starts with a single late-night Google search.大多數人學習編程的旅程都是從一個深夜Google搜索開始的。 Usually it’s something like “Learn ______”通常它類似于“學習______” But how do they decide which la…

Linux 概述

UNIX發展歷程 第一個版本是1969年由Ken Thompson(UNIX之父)在AT& T貝爾實驗室實現Ken Thompson和Dennis Ritchie(C語言之父)使用C語言對整個系統進行了再加工和編寫UNIX的源代碼屬于SCO公司(AT&T ->Novell …

課程一(Neural Networks and Deep Learning),第四周(Deep Neural Networks)—— 0.學習目標...

Understand the key computations underlying deep learning, use them to build and train deep neural networks, and apply it to computer vision. 學習目標 See deep neural networks as successive blocks put one after each otherBuild and train a deep L-layer Neura…

使用ActionTrail Python SDK

ActionTrail提供官方的Python SDK。本文將簡單介紹一下如何使用ActionTrail的Python SDK。 安裝Aliyun Core SDK。 pip install aliyun-python-sdk-core 安裝ActionTrail Python SDK。 pip install aliyun-python-sdk-actiontrail 下面是測試的代碼。調用LookupEventsRequest獲…

泰坦尼克:機器從災難中學習_用于災難響應的機器學習研究:什么才是好的論文?...

泰坦尼克:機器從災難中學習For the first time in 2021, a major Machine Learning conference will have a track devoted to disaster response. The 16th Conference of the European Chapter of the Association for Computational Linguistics (EACL 2021) has a track on…

github持續集成的設置_如何使用GitHub Actions和Puppeteer建立持續集成管道

github持續集成的設置Lately Ive added continuous integration to my blog using Puppeteer for end to end testing. My main goal was to allow automatic dependency updates using Dependabot. In this guide Ill show you how to create such a pipeline yourself. 最近&…

shell與常用命令

虛擬控制臺 一臺計算機的輸入輸出設備就是一個物理的控制臺 ; 如果在一臺計算機上用軟件的方法實現了多個互不干擾獨立工作的控制臺界面,就是實現了多個虛擬控制臺; Linux終端的工作方式是字符命令行方式,用戶通過鍵盤輸入命令進…

C中的malloc:C中的動態內存分配

什么是C中的malloc()? (What is malloc() in C?) malloc() is a library function that allows C to allocate memory dynamically from the heap. The heap is an area of memory where something is stored.malloc()是一個庫函數,它允許C從堆動態分配…

Linux文本編輯器

Linux文本編輯器 Linux系統下有很多文本編輯器。 按編輯區域: 行編輯器 ed 全屏編輯器 vi 按運行環境: 命令行控制臺編輯器 vi X Window圖形界面編輯器 gedit ed 它是一個很古老的行編輯器,vi這些編輯器都是ed演化而來。 每次只能對一…

Alpha第十天

Alpha第十天 聽說 031502543 周龍榮(隊長) 031502615 李家鵬 031502632 伍晨薇 031502637 張檉 031502639 鄭秦 1.前言 任務分配是VV、ZQ、ZC負責前端開發,由JP和LL負責建庫和服務器。界面開發的教輔材料是《第一行代碼》,利用And…

Streamlit —使用數據應用程序更好地測試模型

介紹 (Introduction) We use all kinds of techniques from creating a very reliable validation set to using k-fold cross-validation or coming up with all sorts of fancy metrics to determine how good our model performs. However, nothing beats looking at the ra…

Spring MVC Boot Cloud 技術教程匯總(長期更新)

昨天我們發布了Java成神之路上的知識匯總,今天繼續。 Java成神之路技術整理(長期更新) 以下是Java技術棧微信公眾號發布的關于 Spring/ Spring MVC/ Spring Boot/ Spring Cloud 的技術干貨,本文長期更新。 Spring 系列 Java 必看的…

X Window系統

X Window系統 一種以位圖方式顯示的軟件窗口系統。誕生于1984,比Microsoft Windows要早。是一套獨立于內核的軟件 Linux上的X Window系統 X Window系統由三個基本元素組成:X Server、X Client和二者通信的通道。 X Server:是控制輸出及輸入…

冒名頂替上大學羅彩霞_什么是冒名頂替綜合癥,您如何克服?

冒名頂替上大學羅彩霞冒名頂替綜合癥 (Imposter Syndrome) Imposter Syndrome is a feeling of being a fraud or not being good enough to get the job done. Its common among software engineers, developers and designers working in tech companies, especially those n…

Linux命令----用戶管理

修改用戶密碼: sudo passwd (當前)用戶名  【sudo是super user do的簡寫,passwd是password的簡寫】 顯示當前正在操作系統的用戶:whoami   顯示當前登錄系統的用戶信息:who am i 注意: 普通…

lasso回歸和嶺回歸_如何計劃新產品和服務機會的回歸

lasso回歸和嶺回歸Marketers sometimes have to be creative to offer customers something new without the luxury of that new item being a brand-new product or built-from-scratch service. In fact, incrementally introducing features is familiar to marketers of c…

python代碼

原始字符串,不做任何特殊的處理 print("Newlines are indicated by \n")#Newlines are indicated by print(r"Newlines are indicated by \n")#Newlines are indicated by \n 格式輸出,轉化為字符串由format自動完成 ag…

Linux 設備管理和進程管理

設備管理 Linux系統中設備是用文件來表示的,每種設備都被抽象為設備文件的形式,這樣,就給應用程序一個一致的文件界面,方便應用程序和操作系統之間的通信。 設備文件集中放置在/dev目錄下,一般有幾千個,不…