docker使用入门(二):容器container

docker层次结构可以分为三层,从下往上是:容器(container)、服务(services)、堆栈(stack),其中services定义了容器的行为,stack 定义了services的交互

接下来是尝试如何使用docker在容器中启动一个应用程序

创建容器

  1. 创建一个空的文件夹,其中包含Dockerfile、app.py、requirements.txt三个文件,文件内容分别如下

Dockerfile

# 使用python的运行环境作为parent image
FROM python:2.7-slim # 设置工作路径
WORKDIR /app # 将当前目录的内容复制到相应路径下
COPY . /app # 下载requirements.txt中要求的包
RUN pip install --trusted-host pypi.python.org -r requirements.txt # 暴露容器的80端口
EXPOSE 80 # 定义环境变量
ENV NAME World #当docker开始运行时执行文件
CMD ["python", "app.py"]

requirements.txt

Flask
Redis

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__":

构建应用程序

使用以下命令build应用程序

docker build --tag=friendlyhello  .
# or docker build -t=friendlyhello .

使用docker image ls可以查看构建的image

 $  docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest 7ecc82fd960a 53 minutes ago 148MB
python 2.7-slim 5f759d36aead 7 hours ago 137MB
hello-world latest fce289e99eb9 7 months ago 1.84kB

其中TAG默认为latest,可以使用--tag=friendlyhello:v0.0.1.来指定tag

运行应用程序

使用以下语句运行程序

docker run -p 4000:80 friendlyhello

其中-p 4000:80 表示将image的80端口映射到主机的4000端口。

运行http://localhost:4000可以查看程序运行结果

使用-d可以让程序后台运行

$ docker run -d -p 4000:80 friendlyhello
c21b81020e77e9f15df5fafbfdaf2791599c6233b4169615ea9226f243ff68b8 $ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c21b81020e77 friendlyhello "python app.py" About a minute ago Up About a minute 0.0.0.0:4000->80/tcp elegant_raman # 关闭程序
$ docker container stop c21b $ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

共享iamge

登录账户

我们可以将image上传到dockerhub中,从而能够在任何地方运行image。

首先我们需要先注册一个账号,然后输入docker login在本地登录账号

使用镜像加速器

直接访问dockerhub时,访问速度会较慢,这时候可以采用镜像加速器。国内的镜像加速器有

/etc/docker/daemon.json中写入以下内容

{
"registry-mirrors": [
"https://dockerhub.azk8s.cn",
"https://reg-mirror.qiniu.com"
]
}

之后重启服务

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

执行docker info可以查看镜像是否添加成功

$ docker info
...
Registry Mirrors:
https://dockerhub.azk8s.cn/
https://reg-mirror.qiniu.com/
Live Restore Enabled: false

关联仓库

使用username/repository:tag可已将本地image和远程仓库关联到一起,其中tag是可选的,当tag时,仓库会为相应的image打上标签。其完整的指令是

docker tag image username/repository:tag

例如

docker tag friendlyhello   yezh01/getstart:v1.0

$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest 7ecc82fd960a 23 hours ago 148MB
yezh01/getstart v1.0 7ecc82fd960a 23 hours ago 148MB
python 2.7-slim 5f759d36aead 30 hours ago 137MB
hello-world latest fce289e99eb9 7 months ago 1.84kB

检查image可以发现其中多了一个yezh01/getstart

上推image

我们需要先将image上传到dockerhub中,才能在不同机器上运行image,上传指令为:

$ docker push yezh01/getstart:v1.0

上传完成后,,我们可以通过在任何机器上通过以下命令运行image

docker run -p 4000:80 username/repository:tag

系统执行以上指令时,首先会在本地找相应的image,若本地不存在则会自动尝试从dockerhub中获取image,因此无论我们可以方便的在不同机器上部署开发docker

相关命令

docker build -t friendlyhello .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello # Run "friendlyhello" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello # Same thing, but in detached mode
docker container ls # List all running containers
docker container ls -a # List all containers, even those not running
docker container stop <hash> # Gracefully stop the specified container
docker container kill <hash> # Force shutdown of the specified container
docker container rm <hash> # Remove specified container from this machine
docker container rm $(docker container ls -a -q) # Remove all containers
docker image ls -a # List all images on this machine
docker image rm <image id> # Remove specified image from this machine
docker image rm $(docker image ls -a -q) # Remove all images from this machine
docker login # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag # Tag <image> for upload to registry
docker push username/repository:tag # Upload tagged image to registry
docker run username/repository:tag # Run image from a registry

相关链接

https://docs.docker.com/get-started/part2/

docker (二):容器container的更多相关文章

  1. Docker(二十)-Docker容器CPU、memory资源限制

    背景 在使用 docker 运行容器时,默认的情况下,docker没有对容器进行硬件资源的限制,当一台主机上运行几百个容器,这些容器虽然互相隔离,但是底层却使用着相同的 CPU.内存和磁盘资源.如果不 ...

  2. Docker学习笔记之docker volume 容器卷的那些事(二)

    预览目录 更改目录拥有者 Data Container 切换用户 参考文章 0x00 概述 如果你读了docker volume 容器卷的那些事(一),我想应该不会遇到下面这些问题的,毕竟是具有指导意 ...

  3. Docker 容器(container)及资源限制

    Container: 既然container是由image运行起来的,那么是否可以理解为container和image有某种关系?先来看张图: 其实可以理解为container只是基于image之后的 ...

  4. Docker概念学习系列之Docker核心概念之容器container

    不多说,直接上干货! Docker 利用容器来运行应用. 容器是从镜像创建的运行实例. 它可以被启动.开始.停止.删除.每个容器都是相互隔离的.保证安全的平台. 可以把容器看做是一个简易版的 Linu ...

  5. 理解Docker单机容器网络

    在” 理解Docker单机容器网络 “一文中,还有一个Docker容器网络的功能尚未提及,那就是Docker容器的端口映射.即将容器的服务端口P’ 绑定到宿主机的端口P上,最终达到一种效果:外部程序通 ...

  6. Docker 定制容器镜像的2种方法

    一.需求 由于在测试环境中使用了docker官网的centos 镜像,但是该镜像里面默认没有安装ssh服务,在做测试时又需要开启ssh.所以上网也查了查资料.下面详细的纪录下.在centos 容器内安 ...

  7. Docker:容器间互联的应用zabbix监控项目 [十]

    一.docker容器间的互联 1.创建两个容器 [root@luoahong ~]# docker run -d --name luoahong httpd:latest 8f771f043391e7 ...

  8. Docker:容器的四种网络类型 [十三]

    一.None类型 简介:不为容器配置任何网络功能,--net=none 1.创建容器 docker run -it --network none busubox:latest 2.功能测试 [root ...

  9. Java程序运行在Docker等容器环境有哪些新问题

    基本回答 一.  对于Java来说,Docker毕竟是一个较新的环境,其内存.CPU等资源限制是通过ControlGroup实现的.早期的JDK版本并不能识别这些限制,进而会导致一些基础问题. 1.如 ...

随机推荐

  1. PDB文件会影响性能吗?

    有人问了这样的问题:"我工作的公司正极力反对用生成的调试信息构建发布模式二进制文件,这也是我注册该类的原因之一.他们担心演示会受到影响.我的问题是,在发布模式下生成符号的最佳命令行参数是什么 ...

  2. 移动端touch触摸事件(滑动效果和手势操作)

    一.定义 ①touch是移动端的触摸事件,而且是一组事件,主要有以下事件: touchstart 事件:当手指触摸屏幕的时候触发 touchmove 事件:当手指在屏幕来回滑动的时候触发 touche ...

  3. windows 获取时间戳

    Windows 批处理时间戳 1.时间戳格式: 取年份: echo %date:~,% 取月份: echo %date:~,% 取日期: echo %date:~,% 取星期: echo %date: ...

  4. P1099 树网的核——模拟+树形结构

    P1099 树网的核 无根树,在直径上找到一条长度不超过s的路径,使得最远的点距离这条路径的距离最短: 首先两遍dfs找到直径(第二次找的时候一定要吧father[]清零) 在找到的直径下枚举长度不超 ...

  5. 再见,Eclipse。

    阅读本文大概需要 5 分钟. 来源:cnblogs.com/ouyida3/p/9901312.html 最近,改用了 IDEA,同事都说我投敌了.当然,这些同事都是和我一样的“老”程序员.不说毕业生 ...

  6. 刷题记录:Shrine

    目录 刷题记录:Shrine 刷题记录:Shrine 题目复现链接:https://buuoj.cn/challenges 参考链接:Shrine 解此题总结一下flask的SSTI:CTF SSTI ...

  7. 第06组 Alpha冲刺(6/6)

    队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 组长:宋奕 过去两天完成了哪些任务 主要完成了个人主页模块的接口设计 完善后端的信息处理 GitHub签入记录 接下来的计划 ...

  8. CgLib实现AOP

    一.CgLib实现动态代理的例子 1.创建Person类 package com.example.cglib; public class Person { public void study(){ S ...

  9. presto 日期函数和操作

    https://prestodb.github.io/docs/current/functions/datetime.html date '2012-08-08' + interval '2' day ...

  10. oracle update from多表性能优化一例

    这几天测试java内存数据库,和oracle比较时发下一个update from语句很慢,如下: update business_new set fare1_balance_ratio = (sele ...