Docker——初級學(xué)習(xí)
標(biāo)簽:docker
學(xué)習(xí)內(nèi)容
- 學(xué)習(xí)鏡像泽疆、容器和虛擬機(jī)的區(qū)別
- 學(xué)習(xí)使用Dockerfile來定義一個容器
- 學(xué)習(xí)將本地容器push佛南,再從遠(yuǎn)程pull
鏡像、容器和虛擬機(jī)
鏡像與容器
A container is launched by running an image. An image is an executable package that includes everything needed to run an application--the code, a runtime, libraries, environment variables, and configuration files.
通過運(yùn)行一個鏡像來發(fā)起一個容器。鏡像是一個可執(zhí)行的包灰蛙,包含了需要運(yùn)行一個應(yīng)用的所有東西圣贸,如代碼殴泰、運(yùn)行時間于宙、庫浮驳、環(huán)境變量以及配置文件等。
A container is a runtime instance of an image--what the image becomes in memory when executed (that is, an image with state, or a user process). You can see a list of your running containers with the command, docker ps, just as you would in Linux.
容器是鏡像的運(yùn)行時實例限煞。鏡像被執(zhí)行時抹恳,是在內(nèi)存中的。
容器與虛擬機(jī)
A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.
容器在Linux上本地運(yùn)行署驻,并與其他容器共享主機(jī)的內(nèi)核。它運(yùn)行一個離散進(jìn)程健霹,不占用任何其他可執(zhí)行文件更多內(nèi)存旺上,從而使其輕巧。
By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.
相反糖埋,虛擬機(jī)運(yùn)行成熟的“guest”操作系統(tǒng)宣吱,并通過虛擬機(jī)管理程序來對主機(jī)資源進(jìn)行訪問。通常瞳别,VM為環(huán)境提供的資源比大多數(shù)應(yīng)用程序所需要的資源更多征候。
準(zhǔn)備Docker環(huán)境
測試Docker版本
docker --version #確定docker版本
docker info (or docker version) #查看更多細(xì)節(jié)
測試Docker安裝
- 通過運(yùn)行一個簡單的Docker鏡像(hello-world)來測試安裝可工作
docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
- 列出下載到本地的hello-world鏡像
docker image ls
- 列出hello-world容器,如果容器還在運(yùn)行祟敛,則不需要--all
docker container ls --all
用Dockerfile來定義容器
- 創(chuàng)建一個新的文件夾疤坝,在次文件夾下新建名為Dockerfile的文件,文件內(nèi)的內(nèi)容如下:
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
- 創(chuàng)建新的兩個文件requirements.txt和app.py
from flask import Flask
from redis import Redis, RedisError
import os
import socket
# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)
@app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis, counter disabled</i>"
html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
- 構(gòu)建app
進(jìn)入到新建的文件夾后馆铁,可以看到這里有三個文件
$ ls
Dockerfile app.py requirements.txt
然后運(yùn)行構(gòu)建命令跑揉,創(chuàng)建一個docker鏡像,使用--tag來命名
docker build --tag=friendlyhello .
- 運(yùn)行app
docker run -p 4000:80 friendlyhello
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
在瀏覽器中輸入http://localhost:4000
可以看到結(jié)果埠巨,也可以使用curl
:
curl http://localhost:4000
<h3>Hello World!</h3><b>Hostname:</b> 8fc990912a14<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>
也可以在后臺運(yùn)行:
docker run -d -p 4000:80 friendlyhello
使用docker container stop
來停止進(jìn)程:
共享鏡像
登陸Docker
$ docker login
標(biāo)記鏡像
docker tag image username/repository:tag
例如:
docker tag friendlyhello xiaoyaomakevin/get-started:part2
Publish鏡像
docker push username/repository:tag
例如
docker push xiaoyaomakevin/get-started:part2
從遠(yuǎn)程端拉取并運(yùn)行鏡像
docker run -p 4000:80 username/repository:tag
例如:
docker run -p 4000:80 xiaoyaomakevin/get-started:part2
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)