linux之docker基础

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。开发人员和系统管理员使用容器开发、部署和运行应用程序的平台,使用linux容器部署应用称为容器化(lxc),docker版本分为docker ce  社区版和docker ee  企业版

一个完整的Docker有以下几个部分组成:

  1. dockerClient客户端

  2. Docker Daemon守护进程

  3. Docker Image镜像

  4. DockerContainer容器

通过运行image来启动容器
一个image是一个可以执行的包,包括运行应用程序所需的所有代码、配置等
容器是image被执行时候在存储器中的image

Docker 架构

Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。Docker 容器通过 Docker 镜像来创建。容器与镜像的关系类似于面向对象编程中的对象与类。 [3] 

Docker面向对象
容器对象
镜像

Docker采用 C/S架构 Docker daemon 作为服务端接受来自客户的请求,并处理这些请求(创建、运行、分发容器)。 客户端和服务端既可以运行在一个机器上,也可通过 socket 或者RESTful API 来进行通信。

Docker daemon 一般在宿主主机后台运行,等待接收来自客户端的消息。 Docker 客户端则为用户提供一系列可执行命令,用户用这些命令实现跟 Docker daemon 交互。

Docker的优点:
灵活:复杂应用可以集装箱化
轻量级:容器利用并且共享内核
可互换:随时部署更新、升级
便捷:本地构建、部署到云,在任何地方运行
可扩展:可以增加并且自动分发容器副本

Docker的安装(ubuntu)

sudo apt-get install docker.io

docker服务的启动和关闭

sudo /etc/init.d/docker start\stop

查看docker版本

sudo docker version
sudo docker info

测试docker是否正常

sudo docker run helloworld   
注意:run  启动容器

T2018@localhost:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete 
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest

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.
    (amd64)
 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://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

查看当前有哪些镜像(image)

sudo docker image ls

查看当前有哪些容器

sudo docker container ls

z@z:~$ sudo docker image ls
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
hello-world          latest              4ab4c602aa5e        7 weeks ago         1.84 kB
scrapinghub/splash   latest              3926e5aac017        8 months ago        1.22 GB
z@z:~$ sudo docker container ls
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                              NAMES
6a4ffaabc66a        scrapinghub/splash   "python3 /app/bin/..."   2 hours ago         Up 2 hours          5023/tcp, 0.0.0.0:8050->8050/tcp   competent_lovelace

搜索相关镜像

sudo docker search ubuntu

z@z:~$ sudo docker search ubuntu
NAME                                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                                                 Ubuntu is a Debian-based Linux operating s...   8626      [OK]       
dorowu/ubuntu-desktop-lxde-vnc                         Ubuntu with openssh-server and NoVNC            233                  [OK]
rastasheep/ubuntu-sshd                                 Dockerized SSH service, built on top of of...   178                  [OK]
consol/ubuntu-xfce-vnc                                 Ubuntu container with "headless" VNC sessi...   131                  [OK]
ansible/ubuntu14.04-ansible                            Ubuntu 14.04 LTS with ansible                   95                   [OK]
ubuntu-upstart                                         Upstart is an event-based replacement for ...   92        [OK]       
neurodebian                                            NeuroDebian provides neuroscience research...   54        [OK]       
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          47                   [OK]
ubuntu-debootstrap                                     debootstrap --variant=minbase --components...   40        [OK]       
nuagebec/ubuntu                                        Simple always updated Ubuntu docker images...   23                   [OK]
tutum/ubuntu                                           Simple Ubuntu docker images with SSH access     18                   
i386/ubuntu                                            Ubuntu is a Debian-based Linux operating s...   14                   
1and1internet/ubuntu-16-apache-php-7.0                 ubuntu-16-apache-php-7.0                        13                   [OK]
ppc64le/ubuntu                                         Ubuntu is a Debian-based Linux operating s...   12                   
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4      ubuntu-16-nginx-php-5.6-wordpress-4             6                    [OK]
eclipse/ubuntu_jdk8                                    Ubuntu, JDK8, Maven 3, git, curl, nmap, mc...   6                    [OK]
codenvy/ubuntu_jdk8                                    Ubuntu, JDK8, Maven 3, git, curl, nmap, mc...   5                    [OK]
darksheer/ubuntu                                       Base Ubuntu Image -- Updated hourly             4                    [OK]
pivotaldata/ubuntu                                     A quick freshening-up of the base Ubuntu d...   2                    
smartentry/ubuntu                                      ubuntu with smartentry                          1                    [OK]
1and1internet/ubuntu-16-sshd                           ubuntu-16-sshd                                  1                    [OK]
ossobv/ubuntu                                          Custom ubuntu image from scratch (based on...   0                    
paasmule/bosh-tools-ubuntu                             Ubuntu based bosh-cli                           0                    [OK]
1and1internet/ubuntu-16-healthcheck                    ubuntu-16-healthcheck                           0                    [OK]
pivotaldata/ubuntu-gpdb-dev                            Ubuntu images for GPDB development              0  

下载镜像

sudo docker pull ubuntu:14


dockerfile镜像制作

根据自己的需求及您镜像的制作
1.创建空目录
mkdir testfile
2.进入到该目录
cd testfile
3.新建dockerfile,写入内容
Dockerfile:

FROM python:2.7

WORKDIR /app

ADD .   /app

RUN pip install -r requirements.txt

EXPOSE 80

ENV NAME World

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__":
    app.run(host='0.0.0.0', port=80)

创建自己的容器(空格+.不能少)

sudo docker build -t friend .

启动自己创建的容器
sudo docker run -p 4000:80 friend
注意:-p 端口映射
4000 ubuntu的对应端口
80   容器内部端口


测试
浏览器中打开127.0.0.1:4000进行访问

 

14.镜像删除
docker rmi 镜像名称\id


15.容器删除
sudo docker rm 容器id


16.查看当前的容器
sudo docker ps
sudo docker os -a

17.停止容器运行
sudo docker stop 容器id、名字

 

18.镜像上传至docker hub
a.注册用户
b.准备上传镜像
c.登录dockerhub
    docker login
c.docker pull 上传
    sudo docker tag image username/repository:tag  打标
        sudo docker tag busybox osimeno/test
    sudo docker push username/repository:tag
sudo docker push osimeno/test

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值