docker入门1-docker container

image和container介绍

一个image是一个可被docker执行的包,它包括程序运行的所有东西,包括代码,运行时,库,环境变量和配置文件。

一个container是image在内存中的运行实例,一个image可以产生多个container实例。

docker container命令

查看docker版本

# docker --version
Docker version 18.09.2, build 6247962

使用docker version(不带--)会得到更详细的版本信息.

查看docker状态

docker info

测试docker安装

docker run hello-world测试运行hello-world image.

docker image lsdocker images列出所有已下载的image.

docker container ls --all列出所有container.

构建docker app

app.dockerfile

# 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 8001 available to the world outside this container
EXPOSE 8001
# define enviornment veriable
ENV NAME World
# run app.py when the container launches
CMD ["python", "app.py"]

docker build -f ./app.dockerfile --tag=friendlyhello .

-f参数指定dockerfile的位置,--tag参数指定输出的docker image名称.
构建的image文件可能依赖于别的父image.

容器内的工作目录是/app,这个文件还把当前构建目录的所有文件COPY到了容器内的/app文件夹下。这里还差两个文件app.pyrequirements.txt。等下创建它们,它们也应该跟dockerfile在一起以便于等会COPY进容器。

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)

requirements.txt

Flask
Redis

运行docker app

docker run -p 8001:8001 friendlyhello

如果端口被占用,使用命令lsof -i tcp:8001查看本机8001端口被哪个应用占用.

后台运行docker app

docker run -d -p 8001:8001 friendlyhello

-d指定容器以deamon方式运行,上述命令会返回一长串容器ID.
这里将外部的8001端口映射到container中的8001端口,访问http://localhost:8001/就能得到返回内容。如果启动命令的端口部分改为-p 8080:8001,那就需要访问http://localhost:8080/。container内中的服务,只监听8001,只能通过修改代码监听的端口和dockerfile暴露出的端口。

查看container内的文件

对运行的docker,使用docker提供的exec命令docker exec <container ID> <Command>在容器内执行ls命令,就可以查看到container内的内容,当前这个image运行后的container中有app.dockerfile,app.pyrequirements.txt三个文件。

退出docker app

在前台运行时,使用Ctrl + C即可结束进程.

也可以使用container的stop命令docker container stop <Container NAME or ID>.查看当前运行container的命令是docker container ls,要查看所有状态的container,加-a参数.

给docker app添加tag

推荐添加tag的格式是docker tag image username/repository:tag.

示例:

docker tag friendlyhello cchenyang/get-started:part2

cchenyang是docker hub的username,之后的get-started:part2就是把image friendlyhello改名为get-started并将默认标记latest改为part2.这个标记习惯用法是设置为repository的版本号.

分享image

docker login登录docker hub,将添加tag后的image push到docker hub中docker push cchenyang/get-started:part2

之后,就可以在任何有网络并安装docker的机器中使用docker run -p 8001:8001 cchenyang/get-started:part2获得这个image并运行~!!

系列导航

docker入门2-docker service
docker入门3-docker swarm
docker入门4-docker stack

posted @ 2019-02-24 17:35  agichen  阅读(361)  评论(0编辑  收藏  举报