Docker 入门

Docker 入门

文档

理解

Docker provides a way to run applications securely isolated in a container, packaged with all its dependencies and libraries.

即Docker提供了一种新的方式,来安全地,单独地运行应用。它会将应用所依赖的库和环境全部打包起来。

Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. The use of Linux containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.

Docker是一个封装过得Linux容器,成为我们开发,部署,运行应用的全过程平台。

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.

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.

解释了imagecontainer的关系。

  • 镜像是一个可执行的包,它包括代码,运行时,依赖,环境变量,配置文件等。

  • 运行一个镜像,就启动了一个容器。

  • 容器是一个运行实例。

安装

下载

入门

测试docker版本
docker version

Client:
 Version:      17.06.0-ce
 API version:  1.30
 Go version:   go1.8.3
 Git commit:   02c1d87
 Built:        Fri Jun 23 21:31:53 2017
 OS/Arch:      darwin/amd64

Server:
 Version:      17.06.0-ce
 API version:  1.30 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   02c1d87
 Built:        Fri Jun 23 21:51:55 2017
 OS/Arch:      linux/amd64
 Experimental: true

从这里我们看到,需要注意的是,docker分为客户端和服务端,且需要安装go环境,事实上,docker就是由go开发的。

测试docker安装正常
docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

正常。

Hello World
创建一个空文件夹
mkdir hello
创建Dockerfile
touch Dockerfile
vi ...
# 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
ADD . /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"]
创建app.py,requirements.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 = "cannot connect to Redis, counter disabled"

    html = "

Hello {name}!

" \ "Hostname: {hostname}
" \ "Visits: {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)
Flask
Redis

了解python的同学应该都可以看懂,以上代码使用python中的flask框架书写web服务。

事实上,我们此时就可以运行起来

cd hello
python app.py

但是,我们还没有运用到docker。

构建镜像

在程序根目录中

docker build -t hello .

上述命令表示,在当前环境中构建镜像,并使用-t给镜像指定一个名称。

docker将执行一个构建过程。

查看镜像
docker image ls

hello                     latest              08aec843783e        14 seconds ago

可以看到,该命令可以查看所有被创建的镜像,包括了镜像的名称,tag,id,创建时间。

docker运行镜像
docker run -p 4000:80 hello
docker run -d -p 4000:80 hello  // 后台运行

如果成功,我们将可以在4000端口访问。

打版本
docker tag image username/repository:tag
共享镜像

将自己的镜像推送到dockerhub。

docker push username/repository:tag
运行远程镜像
docker run -p 4000:80 username/repository:tag
使用阿里云镜像加速器

登录阿里云控制台,找到容器镜像服务中的镜像加速器,阿里云会提供一个专属加速器地址

以Mac为例:

右键点击桌面顶栏的 docker 图标,选择 Preferences ,在 Daemon 标签(Docker 17.03 之前版本为 Advanced 标签)下的 Registry mirrors 列表中将专属加速器地址加到"registry-mirrors"的数组里,点击 Apply & Restart按钮,等待Docker重启并应用配置的镜像加速器。

你可能感兴趣的:(Docker 入门)