轻松掌握docker使用-基础入门(代码片段)

葡萄干是个程序员 葡萄干是个程序员     2022-12-10     494

关键词:

前言

Docker自2013年以来非常火热,很多人也津津乐道于它,网上也很多关于Docker的文章和学习教程。这里我再重新提及“docker的入门使用”无非是想整理一下以前学的知识,方便查阅和用于分享。
  在学习Docker使用之前,我们有必要认识一下Docker:

Docker 是一个开源的应用容器引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。

简单来说,Docker就是软件化的集装箱平台,我们每个集装箱的使用互不干扰,运行在Docker上。也意味着,我们通过Docker将我们的应用及所有依赖打包成一个集装箱(容器),快速地运输到机器上进行运行,非常易于装箱打包运行(这也是Docker Logo的含义),而每个在Docker上运行的应用相互独立,互不影响。(我们可以使用Docker技术实现项目的快速部署&快速发布。使用 代码托管平台 + jenkins + k8s 实现CIDI。)
  

  接下来我们来看下Docker安装和使用吧。

Docker安装&容器初运行

Docker安装部署

  1. 确认Linux内核版本3.8以上
~]# uname -r
  1. 安装Docker
~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
~]# yum install -y docker-ce
  1. 初始化配置
~]# cd /etc/
etc]# mkdir docker
~]# vim /etc/docker/daemon.json

  "graph": "/data/docker",
  "storage-driver": "overlay2",
  "insecure-registries": ["registry.access.redhat.com","quay.io"],
  "registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"],
  "bip": "172.17.23.1/24",
  "exec-opts": ["native.cgroupdriver=systemd"],
  "live-restore": true

# daemon.json 配置介绍

# 配置项注意点:
# graph: 该关键字未来将被弃用,可以采用 "data-root" 替代
# storage-driver: 存储驱动,即分层文件系统
# insecure-registries: 不安全的docker registries,即使用http协议推拉镜象
# registry-mirrors: 加速站点,一般可以使用阿里、网易云、docker中国(https://registry.docker-cn.com)的地址
# bip: 指定docker bridge地址(不能以.0结尾),生产中建议采用 172.xx.yy.1/24,其中xx.yy为宿主机ip后四位,方便定位问题
# 若启动失败,查看 /var/log/message 日志排错
  1. 创建Docker数据存放目录
~]# mkdir -p /data/docker
  1. 启动Docker
~]# systemctl start docker && systemctl enable docker
  1. 确认一下配置的Docker bridge地址
~]# ip addr show dev docker0
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    link/ether 02:42:e4:3d:98:db brd ff:ff:ff:ff:ff:ff
    inet 172.17.23.1/24 brd 172.17.23.255 scope global docker0
       valid_lft forever preferred_lft forever
  1. 查看Docker版本
~]# docker version
Client: Docker Engine - Community
 Version:           20.10.7
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        f0df350
 Built:             Wed Jun  2 11:56:24 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.7
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       b0f5bc3
  Built:            Wed Jun  2 11:54:48 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.6
  GitCommit:        d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc:
  Version:          1.0.0-rc95
  GitCommit:        b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

来一起试着运行docker容器

  • 运行docker官方仓库提供的hello-word镜像:
~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
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/

容器启动过程四个步骤:

  • Docker客户端联系Docker服务端。
  • Docker服务端从Docker中心拉取“hello-world”镜像。
  • Docker服务端(用新拉的镜像)创建了一个新的容器,该容器运行可执行文件(脚本),生成您当前读取的输出。
  • Docker服务端将信息流推到Docker客户端,由客户端展示在你的终端。

Docker的组成

在进一步学习Docker使用前,有必要了解一下Docker的组成。

  • 通过docker客户端操作docker管理本地镜像和容器。
  • 我们可以将我们的应用构建成镜像,而使用这里所说的镜像(可以认为是运行模板),我们运行多个相同的应用。构建的镜像可以push到docker仓库,也可以是我们自建的私有仓库。
  • 使用Docker拉取到镜像后,我们可以使用镜像运行多个容器。

Docker的镜像管理

我们可以从远程仓库(默认仓库为Docker官方提供的仓库)拉取镜像,也可以从本地推送镜像到远程仓库。(当然远程仓库也有可能在本地。)

Docker镜像结构

首先来了解一下docker镜像的名称结构:

$registry_name/$repository_name/$image_name:$tag_name
说明:仓库地址/仓库名称/镜像名称:标签

如:

docker.io/library/alpine:3.10.1
指的是:docker.io仓库下library仓库的apline镜像,标签为3.10.1

Docker镜像使用

(我们使用docker的官方仓库进行学习)

  1. 创建docker镜像仓库
    docker提供了官方的镜像仓库dockerhub,我们将使用docker提供的远程仓库。首先在 hub.docker 注册账号。
  2. 登录docker远程仓库
~]# docker login docker.io
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: dreamboyplus
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
  1. 查看已登录的信息
~]# cat /root/.docker/config.json

        "auths": 
                "https://index.docker.io/v1/": 
                        "auth": "对格式为: 账号:密码 进行base64编码后结果"
                
        

可以通过base64解码auth字段对应的值就可以得到登录时的密码。如:

~]# echo "auth字段里的内容" | base64 -d
  1. 查看本地已有的docker镜像
~]# docker images

~]# docker image ls
  1. 在dockerhub中搜索镜像,如搜索alpine镜像:(Alpine是一个面向安全的轻型 Linux 发行版,alpine镜像相比于其他 Docker 镜像,它的容量非常小,仅仅只有 5 MB 左右(对比 Ubuntu 系列镜像接近 200 MB),且拥有非常友好的包管理机制。)
~]# docker search alpine
NAME                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
alpine                                 A minimal Docker image based on Alpine Linux…   7591      [OK]
mhart/alpine-node                      Minimal Node.js built on Alpine Linux           484
anapsix/alpine-java                    Oracle Java 8 (and 7) with GLIBC 2.28 over A…   470                  [OK]
frolvlad/alpine-glibc                  Alpine Docker image with glibc (~12MB)          261                  [OK]
alpine/git                             A  simple git container running in alpine li…   184                  [OK]
yobasystems/alpine-mariadb             MariaDB running on Alpine Linux [docker] [am…   90                   [OK]
alpine/socat                           Run socat command in alpine container           68                   [OK]
kiasaki/alpine-postgres                PostgreSQL docker image based on Alpine Linux   44                   [OK]
jfloff/alpine-python                   A small, more complete, Python Docker image …   41                   [OK]
zenika/alpine-chrome                   Chrome running in headless mode in a tiny Al…   34                   [OK]
byrnedo/alpine-curl                    Alpine linux with curl installed and set as …   34                   [OK]
hermsi/alpine-sshd                     Dockerize your OpenSSH-server with rsync and…   33                   [OK]
hermsi/alpine-fpm-php                  FPM-PHP 7.0 to 8.0, shipped along with tons …   25                   [OK]
etopian/alpine-php-wordpress           Alpine WordPress Nginx PHP-FPM WP-CLI           25                   [OK]
bashell/alpine-bash                    Alpine Linux with /bin/bash as a default she…   18                   [OK]
roribio16/alpine-sqs                   Dockerized ElasticMQ server + web UI over Al…   14                   [OK]
davidcaste/alpine-java-unlimited-jce   Oracle Java 8 (and 7) with GLIBC 2.21 over A…   13                   [OK]
spotify/alpine                         Alpine image with `bash` and `curl`.            11                   [OK]
cfmanteiga/alpine-bash-curl-jq         Docker Alpine image with Bash, curl and jq p…   6                    [OK]
hermsi/alpine-varnish                  Dockerize Varnish upon a lightweight alpine-…   3                    [OK]
dwdraju/alpine-curl-jq                 Alpine Docker Image with curl, jq, bash         1                    [OK]
apteno/alpine-jq                       Weekly build of alpine image with curl, wget…   1
bushrangers/alpine-caddy               Alpine Linux Docker Container running Caddys…   1                    [OK]
goodguykoi/alpine-curl-internal        simple alpine image with curl installed no C…   1                    [OK]
smartentry/alpine                      alpine with smartentry                          0                    [OK]
  1. 拉取镜像,如拉取alpine镜像(不加$registry_name/$repository_name/的情况下,默认拉取的是docker.io/library下的镜像)
  • 不指定tag的情况下默认拉取latesttag
~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
5843afab3874: Pull complete
Digest: sha256:234cb88d3020898631af0ccbbcca9a66ae7306ecd30c9720690858c1b007d2a0
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
  • 也可以指定tag下载(这里可以认为指定的是alpine镜像的版本)
~]# docker pull alpine:3.10.3
3.10.3: Pulling from library/alpine
89d9c30c1d48: Pull complete
Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a
Status: Downloaded newer image for alpine:3.10.3
docker.io/library/alpine:3.10.3
  1. 拉取到镜像后,我们可以给本地镜像打上不同的tag
~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
alpine        latest    d4ff818577bc   10 days ago     5.6MB
hello-world   latest    d1165f221234   3 months ago    13.3kB
alpine        3.10.3    965ea09ff2eb   20 months ago   5.55MB
~]# docker tag 965ea09ff2eb docker.io/dreamboyplus/alpine:v3.10.3
~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
alpine                latest    d4ff818577bc   10 days ago     5.6MB
hello-world           latest    d1165f221234   3 months ago    13.3kB
dreamboyplus/alpine   v3.10.3   965ea09ff2eb   20 months ago   5.55MB
alpine                3.10.3    965ea09ff2eb   20 months ago   5.55MB
  1. 将打上新tag的镜像推送远程仓库
~]# docker push docker.io/dreamboyplus/alpine:v3.10.3
The push refers to repository [docker.io/dreamboyplus/alpine]
77cae8ab23bf: Mounted from library/alpine
v3.10.3: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528

推送成功后,在dockerhub页面上刷新我们账号仓库列表可以查看alpine v3.10.3 tag的镜像。
9. 删除本地镜像

~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
alpine                latest    d4ff818577bc   11 days ago     5.6MB
hello-world           latest    d1165f221234   3 months ago    13.3kB
dreamboyplus/alpine   v3.10.3   965ea09ff2eb   20 months ago   5.55MB
alpine                3.10.3    965ea09ff2eb   20 months ago   5.55MB
~]# docker rmi 965ea09ff2eb
Error response from daemon: conflict: unable to delete 965ea09ff2eb (must be forced) - image is referenced in multiple repositories # 镜像存在多个tag无法进行删除,可以加入 -f
~]# docker rmi -f 965ea09ff2eb
Untagged: dreamboyplus/alpine:v3.10.3
Untagged: dreamboyplus/alpine@sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a
Untagged: alpine:3.10.3
Untagged: alpine@sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a
Deleted: sha256:965ea09ff2ebd2b9eeec88cd822ce156f6674c7e99be082c7efac3c62f3ff652
Deleted: sha256:77cae8ab23bf486355d1b3191259705374f4a11d483b24964d2f729dd8c076a0

删除本地镜像后,远程仓库中的镜像仍存在。
10. 重新将我们前面打了tag的alpine镜像拉取回来

~]# docker pull docker.io/dreamboyplus/alpine:v3.10.3
v3.10.3: Pulling from dreamboyplus/alpine
89d9c30c1d48: Pull complete
Digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a
Status: Downloaded newer image for dreamboyplus/alpine:v3.10.3
docker.io/dreamboyplus/alpine:v3.10.3
 ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
alpine                latest    d4ff818577bc   11 days ago     5.6MB
hello-world           latest    d1165f221234   3 months ago    13.3kB
dreamboyplus/alpine   v3.10.3   965ea09ff2eb   20 months ago   5.55MB
  1. 导入/导出镜像
  • 导出镜像
 ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
dreamboyplus/alpine   v_1.txt   4a17be3db6b7   2 minutes ago   5.55MB
alpine                latest    d4ff818577bc   11 days ago     5.6MB
hello-world           latest    d1165f221234   3 months ago    13.3kB
dreamboyplus/alpine   latest    965ea09ff2eb   20 months ago   5.55MB
dreamboyplus/alpine   v3.10.3   965ea09ff2eb   20 months ago   5.55MB
~]# ls
software
~]# mkdir tmp
~]# cd tmp/
tmp]# pwd
/root/tmp
tmp]# ll
total 0
tmp]# docker save 4a17be3db6b7 > dreamboyplus_alpine_v_1.txt.tar
[root@iZuf6aoqssezcm3p2u4jlkZ tmp]# ls
dreamboyplus_alpine_v_1.txt.tar
  • 导入镜像
    为了实验,我们先删除dreamboyplus/alpine v_1.txt 4a17be3db6b7 2 minutes ago 5.55MB镜像,再重新导入回来
tmp]# docker rmi 4a17be3db6b7
Untagged: dreamboyplus/alpine:v_1.txt
Deleted: sha256:4a17be3db6b76d9b8c300ecdd4f426383a515dc11a3d96f39e57ace8b93567df
Deleted: sha256:11d9c4ffeb1fe6173e968978613efdf33fe4a3504478a2638ca138ad820a5cd8
tmp]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
alpine                latest    d4ff818577bc   11 days ago     5.6MB
hello-world           latest    d1165f221234   3 months ago    13.3kB
dreamboyplus/alpine   latest    965ea09ff2eb   20 months ago   5.55MB
dreamboyplus/alpine   v3.10.3   965ea09ff2eb   20 months ago   5.55MB

导入镜像

tmp]# docker load < dreamboyplus_alpine_v_1.txt.tar
a5a8f98f49e1: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image ID: sha256:4a17be3db6b76d9b8c300ecdd4f426383a515dc11a3d96f39e57ace8b93567df
tmp]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
<none>                <none>    4a17be3db6b7   7 minutes ago   5.55MB
alpine                latest    d4ff818577bc   11 days ago     5.6MB
hello-world           latest    d1165f221234   3 months ago    13.3kB
dreamboyplus/alpine   latest    965ea09ff2eb   20 months ago   5.55MB
dreamboyplus/alpine   v3.10.3   965ea09ff2eb   20 months ago   5.55MB
tmp]# docker tag 4a17be3db6b7 docker.io/dreamboyplus/alpine:v_1.txt
tmp]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
dreamboyplus/alpine   v_1.txt   4a17be3db6b7   8 minutes ago   5.55MB
alpine                latest    d4ff818577bc   11 days ago     5.6MB
hello-world           latest    d1165f221234   3 months ago    13.3kB
dreamboyplus/alpine   latest    965ea09ff2eb   20 months ago   5.55MB
dreamboyplus/alpine   v3.10.3   965ea09ff2eb   20 months ago   5.55MB

Docker容器基本操作

使用本地镜像,我们可以运行起docker容器。我们先来感受一下容器相关的操作吧:

  1. 查询容器列表
~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED        STATUS                    PORTS     NAMES
c8a82e81f856   hello-world   "/hello"   18 hours ago   Exited (0) 18 hours ago             loving_johnson
  1. 启动容器(运行镜像)
    命令格式:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
    说明:
OPTIONS :选项
-i
 :表示启动-一个可交互的容器, 并持续打开标准输入
-t
 :表示使用终端关联到容器的标准输入输出上
-d
 :表示将容器放置后台运行
--rm
 :退出后即删除容器
--name
 :表示定义容器唯一名称
IMAGE :表示要运行的镜像
COMMAND :表示启动容器时要运行的命令*
ARG :参数
  • 启动交互式容器
~]# docker run -it dreamboyplus/alpine
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:11:17:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.23.2/24 brd 172.17.23.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # exit
  • 启动非交互式容器
~]# docker run --name alpine_sleep -d dreamboyplus/alpine /bin/sleep 300
dd0b41ac8227f8c6fbf966f1ae36553e8bd89ca0bfa41d0c39d278ea60b0ddd9
~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND            CREATED         STATUS                     PORTS     NAMES
dd0b41ac8227   dreamboyplus/alpine   "/bin/sleep 300"   6 seconds ago   Up 5 seconds                         alpine_sleep
c618b23b2a58   dreamboyplus/alpine   "/bin/sh"          4 minutes ago   Exited (0) 3 minutes ago             amazing_rubin
c8a82e81f856   hello-world           "/hello"           18 hours ago    Exited (0) 18 hours ago              loving_johnson

在宿主机中查看该docker容器内执行的进程:

~]# ps aux | grep sleep | grep -v grep
root       28894  0.0  0.0   1552     4 ?        Ss   15:50   0:00 /bin/sleep 300
  • 使用-p host_port:container_port参数进行端口映射
~]# docker pull nginx:1.12.2
1.12.2: Pulling from library/nginx
f2aa67a397c4: Pull complete
e3eaf3d87fe0: Pull complete
38cb13c1e4c9: Pull complete
Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Status: Downloaded newer image for nginx:1.12.2
docker.io/library/nginx:1.12.2
~]# docker images |grep nginx
nginx                 1.12.2    4037a5562b03   3 years ago      108MB
~]# docker run --name nginx -d -p 83:80 nginx:1.12.2
7f567fa37a171299af535da1ed2f3f48e7dd7be4d4dcb1d31338526ddddad087
~]# docker ps -a | grep nginx
7f567fa37a17   nginx:1.12.2          "nginx -g 'daemon of…"   12 seconds ago   Up 11 seconds               0.0.0.0:83->80/tcp, :::83->80/tcp   nginx
~]# curl http://localhost:83
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body 
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
  • 使用-v host_path:container_path参数挂载目录
~]# mkdir -vp server/nginx/html
mkdir: created directory 'server'
mkdir: created directory 'server/nginx'
mkdir: created directory 'server/nginx/html'
~]# cd server/nginx/html/
html]# wget  www.baidu.com -O index.html
--2021-06-27 16:48:01--  http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 112.80.248.76, 112.80.248.75
Connecting to www.baidu.com (www.baidu.com)|112.80.248.76|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: ‘index.html’

index.html                         100%[=============================================================>]   2.33K  --.-KB/s    in 0s

2021-06-27 16:48:01 (205 MB/s) - ‘index.html’ saved [2381/2381]

html]# ls
index.html
html]# docker run --name nginx_with_html -d -p 84:80 -v /root/server/nginx/html:/usr/share/nginx/html nginx:1.12.2
8bc8c5681368e8b06b6e20917e35299a2a471ef9af0db557023c578eb4e84257
html]# docker ps -a | grep nginx
8bc8c5681368   nginx:1.12.2          "nginx -g 'daemon of…"   11 seconds ago   Up 10 seconds               0.0.0.0:84->80/tcp, :::84->80/tcp   nginx_with_html
html]# curl http://localhost:84
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

进入容器,查看容器内/usr/share/nginx/html的内容

html]# docker exec -it 8bc8c5681368 /bin/sh
# cd /usr/share/nginx/html
# ls
index.html
# cat index.html
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

查看挂载的详细信息

html]# docker inspect nginx_with_html | grep -A 9 'Mounts'
        "Mounts": [
            
                "Type": "bind",
                "Source": "/root/server/nginx/html",
                "Destination": "/usr/share/nginx/html",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            
        ],
  • 使用-e variate_name=variate_value传递环境变量
~]# docker run --rm -e ENV_USERNAME=dreamboy -e ENV_PASSWORD=123456 dreamboyplus/alpine printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=411dcf81d9da
ENV_USERNAME=dreamboy
ENV_PASSWORD=123456
HOME=/root
  1. 进入容器
~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND            CREATED          STATUS                     PORTS     NAMES
9660a42e107f   dreamboyplus/alpine   "/bin/sleep 300"   17 seconds ago   Up 16 seconds                        alpine_sleep
c618b23b2a58   dreamboyplus/alpine   "/bin/sh"          10 minutes ago   Exited (0) 9 minutes ago             amazing_rubin
c8a82e81f856   hello-world           "/hello"           19 hours ago     Exited (0) 19 hours ago              loving_johnson
~]# docker exec -it 9660a42e107f /bin/sh
/ # ps -ef
PID   USER     TIME  COMMAND
    1 root      0:00 /bin/sleep 300
    7 root      0:00 /bin/sh
   13 root      0:00 ps -ef
/ # exit
  1. 停止容器
~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND            CREATED          STATUS                      PORTS     NAMES
9660a42e107f   dreamboyplus/alpine   "/bin/sleep 300"   2 minutes ago    Up 2 minutes                          alpine_sleep
c618b23b2a58   dreamboyplus/alpine   "/bin/sh"          12 minutes ago   Exited (0) 11 minutes ago             amazing_rubin
c8a82e81f856   hello-world           "/hello"           19 hours ago     Exited (0) 19 hours ago               loving_johnson
~]# docker stop 9660a42e107f
9660a42e107f
~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND            CREATED          STATUS                        PORTS     NAMES
9660a42e107f   dreamboyplus/alpine   "/bin/sleep 300"   2 minutes ago    Exited (137) 10 seconds ago             alpine_sleep
c618b23b2a58   dreamboyplus/alpine   "/bin/sh"          12 minutes ago   Exited (0) 12 minutes ago               amazing_rubin
c8a82e81f856   hello-world           "/hello"           19 hours ago     Exited (0) 19 hours ago                 loving_johnson

也可以使用容器名称进行停止

docker stop alpine_sleep
  1. 启动容器
~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND            CREATED          STATUS                        PORTS     NAMES
9660a42e107f   dreamboyplus/alpine   "/bin/sleep 300"   2 minutes ago    Exited (137) 10 seconds ago             alpine_sleep
c618b23b2a58   dreamboyplus/alpine   "/bin/sh"          12 minutes ago   Exited (0) 12 minutes ago               amazing_rubin
c8a82e81f856   hello-world           "/hello"           19 hours ago     Exited (0) 19 hours ago                 loving_johnson
~]# docker start 9660a42e107f
9660a42e107f
~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND            CREATED          STATUS                      PORTS     NAMES
9660a42e107f   dreamboyplus/alpine   "/bin/sleep 300"   3 minutes ago    Up 3 seconds                          alpine_sleep
c618b23b2a58   dreamboyplus/alpine   "/bin/sh"          13 minutes ago   Exited (0) 12 minutes ago             amazing_rubin
c8a82e81f856   hello-world           "/hello"           19 hours ago     Exited (0) 19 hours ago               loving_johnson

也可以使用容器名称进行启动

docker start alpine_sleep
  1. 重启容器
~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND            CREATED          STATUS                      PORTS     NAMES
9660a42e107f   dreamboyplus/alpine   "/bin/sleep 300"   3 minutes ago    Up 3 seconds                          alpine_sleep
c618b23b2a58   dreamboyplus/alpine   "/bin/sh"          13 minutes ago   Exited (0) 12 minutes ago             amazing_rubin
c8a82e81f856   hello-world           "/hello"           19 hours ago     Exited (0) 19 hours ago               loving_johnson
~]# docker restart 9660a42e107f
9660a42e107f
~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND            CREATED          STATUS                      PORTS     NAMES
9660a42e107f   dreamboyplus/alpine   "/bin/sleep 300"   4 minutes ago    Up 6 seconds                          alpine_sleep
c618b23b2a58   dreamboyplus/alpine   "/bin/sh"          14 minutes ago   Exited (0) 14 minutes ago             amazing_rubin
c8a82e81f856   hello-world           "/hello"           19 hours ago     Exited (0) 19 hours ago               loving_johnson

也可以使用容器名称进行重启

docker restart alpine_sleep
  1. 在宿主机和容器之间传输文件
  • 宿主机->容器
docker cp 要拷贝的文件路径 容器名/容器ID:要存放在文件中的路径
  • 容器->宿主机
docker cp 容器名/容器ID:要拷贝的文件 宿主机中要存放的文件路径
  1. 删除容器
  • 删除已退出的容器
~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND            CREATED          STATUS                      PORTS     NAMES
9660a42e107f   dreamboyplus/alpine   "/bin/sleep 300"   9 minutes ago    Up 4 minutes                          alpine_sleep
c618b23b2a58   dreamboyplus/alpine   "/bin/sh"          19 minutes ago   Exited (0) 18 minutes ago             amazing_rubin
c8a82e81f856   hello-world           "/hello"           19 hours ago     Exited (0) 19 hours ago               loving_johnson
~]# docker rm c618b23b2a58
c618b23b2a58
~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND            CREATED         STATUS                      PORTS     NAMES
9660a42e107f   dreamboyplus/alpine   "/bin/sleep 300"   9 minutes ago   Exited (0) 10 seconds ago             alpine_sleep
c8a82e81f856   hello-world           "/hello"           19 hours ago    Exited (0) 19 hours ago               loving_johnson
  • 删除正在运行的容器,加入-f参数
docker rm -f 容器ID/容器名称
  • 删除所有未在运行的容器
docker rm `docker ps -a -q`
  • 删除所有容器(包含正在运行中的)
docker rm -f `docker ps -a -q`
  1. 修改容器中的内容后保存为新的镜像
~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND            CREATED          STATUS                      PORTS     NAMES
9660a42e107f   dreamboyplus/alpine   "/bin/sleep 300"   26 minutes ago   Exited (0) 16 minutes ago             alpine_sleep
c8a82e81f856   hello-world           "/hello"           19 hours ago     Exited (0) 19 hours ago               loving_johnson
~]# docker start 9660a42e107f
9660a42e107f
~]# docker exec -it 9660a42e107f /bin/sh
/ # ls
bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # echo "hello world" >> test.txt
/ # ls
bin       etc       lib       mnt       proc      run       srv       test.txt  usr
dev       home      media     opt       root      sbin      sys       tmp       var
/ # cat test.txt
hello world
/ # exit

写入到容器的文件并不会保存在镜像里,需要使用comit -p命令将容器保存为新的镜像。

 ~]# docker commit -p alpine_sleep docker.io/dreamboyplus/alpine:v_1.txt
sha256:4a17be3db6b76d9b8c300ecdd4f426383a515dc11a3d96f39e57ace8b93567df
~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
dreamboyplus/alpine   v_1.txt   4a17be3db6b7   6 seconds ago   5.55MB
alpine                latest    d4ff818577bc   11 days ago     5.6MB
hello-world           latest    d1165f221234   3 months ago    13.3kB
dreamboyplus/alpine   latest    965ea09ff2eb   20 months ago   5.55MB
dreamboyplus/alpine   v3.10.3   965ea09ff2eb   20 months ago   5.55MB
  1. 查看容器日志
~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND            CREATED          STATUS                     PORTS     NAMES
9660a42e107f   dreamboyplus/alpine   "/bin/sleep 300"   41 minutes ago   Exited (0) 8 minutes ago             alpine_sleep
c8a82e81f856   hello-world           "/hello"           19 hours ago     Exited (0) 19 hours ago              loving_johnson
~]# docker logs c8a82e81f856

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/
  1. 查询容器详细信息
docker inspect 容器ID/容器名称
  1. 容器内下载软件
    参考Docker容器中安装curl、telnet、vim基础工具
  • 进入容器查看系统版本
cat /etc/os-release
- Debian基础镜像
#先添加163源
tee /etc/apt/sources.list << EOF
deb http://mirrors.163.com/debian/ jessie main non-ffree contrib
deb http://mirrirs.163.com/debian/ jessie-updates main non-free contrib
EOF

- Alpine基础镜像
#先添加阿里源
cat > /etc/apk/repositories << EOF
http://mirrors.aliyun.com/alpine/v3.12/main/
http://mirrors.aliyun.com/alpine/v3.12/community
EOF
  • 安装curl
~]# docker exec -it nginx_with_html /bin/bash
root@8bc8c5681368:/# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root@8bc8c5681368:/# tee /etc/apt/sources.list << EOF
> deb http://mirrors.163.com/debian/ jessie main non-ffree contrib
> deb http://mirrirs.163.com/debian/ jessie-updates main non-free contrib
> EOF
deb http://mirrors.163.com/debian/ jessie main non-ffree contrib
deb http://mirrirs.163.com/debian/ jessie-updates main non-free contrib
root@8bc8c5681368:/# apt-get update && apt-get install -y curl
Err:1 http://mirrirs.163.com/debian jessie-updates InRelease
  Could not resolve 'mirrirs.163.com'
Ign:2 http://mirrors.163.com/debian jessie InRelease
Get:3 http://mirrors.163.com/debian jessie Release [77.3 kB]
Get:4 http://mirrors.163.com/debian jessie Release.gpg [1652 B]
Get:5 http://mirrors.163.com/debian jessie/contrib amd64 Packages [59.2 kB]
Get:6 http://mirrors.163.com/debian jessie/main amd64 Packages [9098 kB]
Fetched 9100 kB in 1s (8360 kB/s)
Reading package lists... Done
W: Failed to fetch http://mirrirs.163.com/debian/dists/jessie-updates/InRelease  Could not resolve 'mirrirs.163.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  ca-certificates krb5-locales libcurl3 libffi6 libgmp10 libgnutls-deb0-28 libgssapi-krb5-2 libhogweed2 libidn11 libk5crypto3
  libkeyutils1 libkrb5-3 libkrb5support0 libldap-2.4-2 libnettle4 libp11-kit0 librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db
  libssh2-1 libssl1.0.0 libtasn1-6 openssl
Suggested packages:
  gnutls-bin krb5-doc krb5-user libsasl2-modules-otp libsasl2-modules-ldap libsasl2-modules-sql libsasl2-modules-gssapi-mit
  | libsasl2-modules-gssapi-heimdal
The following NEW packages will be installed:
  ca-certificates curl krb5-locales libcurl3 libffi6 libgmp10 libgnutls-deb0-28 libgssapi-krb5-2 libhogweed2 libidn11 libk5crypto3
  libkeyutils1 libkrb5-3 libkrb5support0 libldap-2.4-2 libnettle4 libp11-kit0 librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db
  libssh2-1 libssl1.0.0 libtasn1-6 openssl
0 upgraded, 25 newly installed, 0 to remove and 1 not upgraded.
Need to get 7883 kB of archives.
After this operation, 15.2 MB of additional disk space will be used.
Get:1 http://mirrors.163.com/debian jessie/main amd64 libssl1.0.0 amd64 1.0.1t-1+deb8u8 [1044 kB]
Get:2 http://mirrors.163.com/debian jessie/main amd64 libgmp10 amd64 2:6.0.0+dfsg-6 [253 kB]
Get:3 http://mirrors.163.com/debian jessie/main amd64 libnettle4 amd64 2.7.1-5+deb8u2 [176 kB]
Get:4 http://mirrors.163.com/debian jessie/main amd64 libhogweed2 amd64 2.7.1-5+deb8u2 [125 kB]
Get:5 http://mirrors.163.com/debian jessie/main amd64 libffi6 amd64 3.1-2+deb8u1 [20.2 kB]
Get:6 http://mirrors.163.com/debian jessie/main amd64 libp11-kit0 amd64 0.20.7-1 [81.2 kB]
Get:7 http://mirrors.163.com/debian jessie/main amd64 libtasn1-6 amd64 4.2-3+deb8u3 [49.2 kB]
Get:8 http://mirrors.163.com/debian jessie/main amd64 libgnutls-deb0-28 amd64 3.3.8-6+deb8u7 [696 kB]
Get:9 http://mirrors.163.com/debian jessie/main amd64 libkeyutils1 amd64 1.5.9-5+b1 [12.0 kB]
Get:10 http://mirrors.163.com/debian jessie/main amd64 libkrb5support0 amd64 1.12.1+dfsg-19+deb8u4 [59.4 kB]
Get:11 http://mirrors.163.com/debian jessie/main amd64 libk5crypto3 amd64 1.12.1+dfsg-19+deb8u4 [116 kB]
Get:12 http://mirrors.163.com/debian jessie/main amd64 libkrb5-3 amd64 1.12.1+dfsg-19+deb8u4 [303 kB]
Get:13 http://mirrors.163.com/debian jessie/main amd64 libgssapi-krb5-2 amd64 1.12.1+dfsg-19+deb8u4 [152 kB]
Get:14 http://mirrors.163.com/debian jessie/main amd64 libidn11 amd64 1.29-1+deb8u2 [136 kB]
Get:15 http://mirrors.163.com/debian jessie/main amd64 libsasl2-modules-db amd64 2.1.26.dfsg1-13+deb8u1 [67.1 kB]
Get:16 http://mirrors.163.com/debian jessie/main amd64 libsasl2-2 amd64 2.1.26.dfsg1-13+deb8u1 [105 kB]
Get:17 http://mirrors.163.com/debian jessie/main amd64 libldap-2.4-2 amd64 2.4.40+dfsg-1+deb8u4 [218 kB]
Get:18 http://mirrors.163.com/debian jessie/main amd64 librtmp1 amd64 2.4+20150115.gita107cef-1+deb8u1 [60.0 kB]
Get:19 http://mirrors.163.com/debian jessie/main amd64 libssh2-1 amd64 1.4.3-4.1+deb8u1 [125 kB]
Get:20 http://mirrors.163.com/debian jessie/main amd64 libcurl3 amd64 7.38.0-4+deb8u11 [260 kB]
Get:21 http://mirrors.163.com/debian jessie/main amd64 krb5-locales all 1.12.1+dfsg-19+deb8u4 [2649 kB]
Get:22 http://mirrors.163.com/debian jessie/main amd64 openssl amd64 1.0.1t-1+deb8u8 [664 kB]
Get:23 http://mirrors.163.com/debian jessie/main amd64 ca-certificates all 20141019+deb8u3 [207 kB]
Get:24 http://mirrors.163.com/debian jessie/main amd64 curl amd64 7.38.0-4+deb8u11 [201 kB]
Get:25 http://mirrors.163.com/debian jessie/main amd64 libsasl2-modules amd64 2.1.26.dfsg1-13+deb8u1 [101 kB]
Fetched 7883 kB in 1s (4037 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libssl1.0.0:amd64.
(Reading database ... 7027 files and directories currently installed.)
Preparing to unpack .../00-libssl1.0.0_1.0.1t-1+deb8u8_amd64.deb ...
Unpacking libssl1.0.0:amd64 (1.0.1t-1+deb8u8) ...
Selecting previously unselected package libgmp10:amd64.
Preparing to unpack .../01-libgmp10_2%3a6.0.0+dfsg-6_amd64.deb ...
Unpacking libgmp10:amd64 (2:6.0.0+dfsg-6) ...
Selecting previously unselected package libnettle4:amd64.
Preparing to unpack .../02-libnettle4_2.7.1-5+deb8u2_amd64.deb ...
Unpacking libnettle4:amd64 (2.7.1-5+deb8u2) ...
Selecting previously unselected package libhogweed2:amd64.
Preparing to unpack .../03-libhogweed2_2.7.1-5+deb8u2_amd64.deb ...
Unpacking libhogweed2:amd64 (2.7.1-5+deb8u2) ...
Selecting previously unselected package libffi6:amd64.
Preparing to unpack .../04-libffi6_3.1-2+deb8u1_amd64.deb ...
Unpacking libffi6:amd64 (3.1-2+deb8u1) ...
Selecting previously unselected package libp11-kit0:amd64.
Preparing to unpack .../05-libp11-kit0_0.20.7-1_amd64.deb ...
Unpacking libp11-kit0:amd64 (0.20.7-1) ...
Selecting previously unselected package libtasn1-6:amd64.
Preparing to unpack .../06-libtasn1-6_4.2-3+deb8u3_amd64.deb ...
Unpacking libtasn1-6:amd64 (4.2-3+deb8u3) ...
Selecting previously unselected package libgnutls-deb0-28:amd64.
Preparing to unpack .../07-libgnutls-deb0-28_3.3.8-6+deb8u7_amd64.deb ...
Unpacking libgnutls-deb0-28:amd64 (3.3.8-6+deb8u7) ...
Selecting previously unselected package libkeyutils1:amd64.
Preparing to unpack .../08-libkeyutils1_1.5.9-5+b1_amd64.deb ...
Unpacking libkeyutils1:amd64 (1.5.9-5+b1) ...
Selecting previously unselected package libkrb5support0:amd64.
Preparing to unpack .../09-libkrb5support0_1.12.1+dfsg-19+deb8u4_amd64.deb ...
Unpacking libkrb5support0:amd64 (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package libk5crypto3:amd64.
Preparing to unpack .../10-libk5crypto3_1.12.1+dfsg-19+deb8u4_amd64.deb ...
Unpacking libk5crypto3:amd64 (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package libkrb5-3:amd64.
Preparing to unpack .../11-libkrb5-3_1.12.1+dfsg-19+deb8u4_amd64.deb ...
Unpacking libkrb5-3:amd64 (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package libgssapi-krb5-2:amd64.
Preparing to unpack .../12-libgssapi-krb5-2_1.12.1+dfsg-19+deb8u4_amd64.deb ...
Unpacking libgssapi-krb5-2:amd64 (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package libidn11:amd64.
Preparing to unpack .../13-libidn11_1.29-1+deb8u2_amd64.deb ...
Unpacking libidn11:amd64 (1.29-1+deb8u2) ...
Selecting previously unselected package libsasl2-modules-db:amd64.
Preparing to unpack .../14-libsasl2-modules-db_2.1.26.dfsg1-13+deb8u1_amd64.deb ...
Unpacking libsasl2-modules-db:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Selecting previously unselected package libsasl2-2:amd64.
Preparing to unpack .../15-libsasl2-2_2.1.26.dfsg1-13+deb8u1_amd64.deb ...
Unpacking libsasl2-2:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Selecting previously unselected package libldap-2.4-2:amd64.
Preparing to unpack .../16-libldap-2.4-2_2.4.40+dfsg-1+deb8u4_amd64.deb ...
Unpacking libldap-2.4-2:amd64 (2.4.40+dfsg-1+deb8u4) ...
Selecting previously unselected package librtmp1:amd64.
Preparing to unpack .../17-librtmp1_2.4+20150115.gita107cef-1+deb8u1_amd64.deb ...
Unpacking librtmp1:amd64 (2.4+20150115.gita107cef-1+deb8u1) ...
Selecting previously unselected package libssh2-1:amd64.
Preparing to unpack .../18-libssh2-1_1.4.3-4.1+deb8u1_amd64.deb ...
Unpacking libssh2-1:amd64 (1.4.3-4.1+deb8u1) ...
Selecting previously unselected package libcurl3:amd64.
Preparing to unpack .../19-libcurl3_7.38.0-4+deb8u11_amd64.deb ...
Unpacking libcurl3:amd64 (7.38.0-4+deb8u11) ...
Selecting previously unselected package krb5-locales.
Preparing to unpack .../20-krb5-locales_1.12.1+dfsg-19+deb8u4_all.deb ...
Unpacking krb5-locales (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package openssl.
Preparing to unpack .../21-openssl_1.0.1t-1+deb8u8_amd64.deb ...
Unpacking openssl (1.0.1t-1+deb8u8) ...
Selecting previously unselected package ca-certificates.
Preparing to unpack .../22-ca-certificates_20141019+deb8u3_all.deb ...
Unpacking ca-certificates (20141019+deb8u3) ...
Selecting previously unselected package curl.
Preparing to unpack .../23-curl_7.38.0-4+deb8u11_amd64.deb ...
Unpacking curl (7.38.0-4+deb8u11) ...
Selecting previously unselected package libsasl2-modules:amd64.
Preparing to unpack .../24-libsasl2-modules_2.1.26.dfsg1-13+deb8u1_amd64.deb ...
Unpacking libsasl2-modules:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Setting up libssl1.0.0:amd64 (1.0.1t-1+deb8u8) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.24.1 /usr/local/share/perl/5.24.1 /usr/lib/x86_64-linux-gnu/perl5/5.24 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.24 /usr/share/perl/5.24 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Setting up libsasl2-modules-db:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Setting up libsasl2-2:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Setting up libtasn1-6:amd64 (4.2-3+deb8u3) ...
Setting up libgmp10:amd64 (2:6.0.0+dfsg-6) ...
Setting up libssh2-1:amd64 (1.4.3-4.1+deb8u1) ...
Setting up krb5-locales (1.12.1+dfsg-19+deb8u4) ...
Setting up libnettle4:amd64 (2.7.1-5+deb8u2) ...
Setting up openssl (1.0.1t-1+deb8u8) ...
Setting up libffi6:amd64 (3.1-2+deb8u1) ...
Setting up libkeyutils1:amd64 (1.5.9-5+b1) ...
Setting up libsasl2-modules:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Setting up ca-certificates (20141019+deb8u3) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.24.1 /usr/local/share/perl/5.24.1 /usr/lib/x86_64-linux-gnu/perl5/5.24 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.24 /usr/share/perl/5.24 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Updating certificates in /etc/ssl/certs... 174 added, 0 removed; done.
Setting up libidn11:amd64 (1.29-1+deb8u2) ...
Setting up libhogweed2:amd64 (2.7.1-5+deb8u2) ...
Setting up libkrb5support0:amd64 (1.12.1+dfsg-19+deb8u4) ...
Setting up libp11-kit0:amd64 (0.20.7-1) ...
Setting up libgnutls-deb0-28:amd64 (3.3.8-6+deb8u7) ...
Setting up libk5crypto3:amd64 (1.12.1+dfsg-19+deb8u4) ...
Setting up librtmp1:amd64 (2.4+20150115.gita107cef-1+deb8u1) ...
Setting up libldap-2.4-2:amd64 (2.4.40+dfsg-1+deb8u4) ...
Setting up libkrb5-3:amd64 (1.12.1+dfsg-19+deb8u4) ...
Setting up libgssapi-krb5-2:amd64 (1.12.1+dfsg-19+deb8u4) ...
Setting up libcurl3:amd64 (7.38.0-4+deb8u11) ...
Setting up curl (7.38.0-4+deb8u11) ...
Processing triggers for ca-certificates (20141019+deb8u3) ...
Updating certificates in /etc/ssl/certs... 0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....done.
root@8bc8c5681368:/# curl
curl: try 'curl --help' or 'curl --manual' for more information
root@8bc8c5681368:/#

容器nginx_with_html安装好curl之后可以制作成镜像推送到我们的仓库,推送完成后可以在我们dockerhub的仓库列表看到nginx:curl镜像。

~]# docker ps -a | grep nginx
8bc8c5681368   nginx:1.12.2          "nginx -g 'daemon of…"   23 minutes ago      Up 23 minutes               0.0.0.0:84->80/tcp, :::84->80/tcp   nginx_with_html
7f567fa37a17   nginx:1.12.2          "nginx -g 'daemon of…"   30 minutes ago      Up 30 minutes               0.0.0.0:83->80/tcp, :::83->80/tcp   nginx
~]# docker commit -p 8bc8c5681368 dreamboyplus/nginx:curl
sha256:a15d70a71198ad5034532dc6177794b73850f3852792485735cb454c3f01f8e0
~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
dreamboyplus/nginx    curl      a15d70a71198   5 seconds ago    136MB
dreamboyplus/alpine   v_1.txt   4a17be3db6b7   47 minutes ago   5.55MB
alpine                latest    d4ff818577bc   11 days ago      5.6MB
hello-world           latest    d1165f221234   3 months ago     13.3kB
dreamboyplus/alpine   latest    965ea09ff2eb   20 months ago    5.55MB
dreamboyplus/alpine   v3.10.3   965ea09ff2eb   20 months ago    5.55MB
nginx                 1.12.2    4037a5562b03   3 years ago      108MB
~]# docker push docker.io/dreamboyplus/nginx:curl
The push refers to repository [docker.io/dreamboyplus/nginx]
55dff8bca326: Pushed
4258832b2570: Mounted from library/nginx
683a28d1d7fd: Mounted from library/nginx
d626a8ad97a1: Mounted from library/nginx
curl: digest: sha256:b2da2b1b5d5dd9e0a29497f323e4e02d795bcc698ad335b0144a1fcef709792b size: 1160

Docker基础命令总结

Dokcer命令说明示例
docker version查询docker版本信息docker version
docker login 仓库地址登录docker仓库docker login docker.io
------------- docker镜像管理 -------------
docker images查看本地已有的镜像列表,作用同 docker image lsdocker images
docker image ls查看本地已有的镜像列表,作用同 docker imagesdocker image ls
docker search 镜像名称搜索仓库中的镜像docker search alpine
docker pull 镜像名称拉取远程仓库的镜像,未指定仓库地址时拉取的是docker.io/library下的镜像,未指定tag时,默认拉取的是latest tagdocker pull alpine 相当于 docker pull docker.io/library/latest
docker tag 本地镜像ID 新的Tag为本地镜像打上新的Tagdocker tag 965ea09ff2eb docker.io/dreamboyplus/alpine:v3.10.3
docker push 本地镜像将本地镜像推送到远程仓库docker push docker.io/dreamboyplus/alpine:v3.10.3
docker rmi [-f] 本地镜像ID删除本地镜像,镜像存在多个tag无法进行删除,可以加入 -fdocker rmi -f 965ea09ff2eb
docker save 镜像ID > 镜像文件名称.txt.tar导出本地镜像docker save 4a17be3db6b7 > dreamboyplus_alpine_v_1.txt.tar
docker load < 镜像文件名称.txt.tar导入镜像docker load < dreamboyplus_alpine_v_1.txt.tar
------------- docker容器操作 -------------
docker ps -a查询容器列表docker ps -a
docker run [OPTIONS] IMAGE [COMMAND] [ARG…]运行docker容器
OPTIONS:选项
-i: 表示启动-一个可交互的容器, 并持续打开标准输入
-t: 表示使用终端关联到容器的标准输入输出上
-d: 表示将容器放置后台运行
--rm: 退出后即删除容器
--name: 表示定义容器唯一名称
IMAGE: 表示要运行的镜像
COMMAND: 表示启动容器时要运行的命令
ARG :参数
- 启动交互式容器:docker run -it dreamboyplus/alpine
- 启动非交互式容器 docker run --name alpine_sleep -d dreamboyplus/alpine /bin/sleep 300
- 使用-p host_port:container_port参数进行端口映射:docker run --name nginx -d -p 83:80 nginx:1.12.2
- 使用-v host_path:container_path参数挂载目录:docker run --name nginx_with_html -d -p 84:80 -v /root/server/nginx/html:/usr/share/nginx/html
- 使用-e variate_name=variate_value传递环境变量:docker run --rm -e ENV_USERNAME=dreamboy -e ENV_PASSWORD=123456 dreamboyplus/alpine printenv
docker exec -it 容器名称或容器ID /bin/sh或bash进入容器docker exec -it 9660a42e107f /bin/sh
docker stop 容器名称或容器ID停止容器docker stop alpine_sleep
docker start 容器名称或容器ID启动容器docker start alpine_sleep
docker restart 容器名称或容器ID重启容器docker restart alpine_sleep
docker cp 要拷贝的文件路径 容器名/容器ID:要存放在文件中的路径拷贝宿主机的文件到容器中
docker cp 容器名/容器ID:要拷贝的文件 宿主机中要存放的文件路径拷贝容器中的文件到宿主机
docker rm 容器ID或容器名称删除已退出的容器docker rm c618b23b2a58
docker rm -f 容器ID或容器名称删除正在运行的容器
docker rm `docker ps -a -q`删除所有未在运行的容器
docker rm -f `docker ps -a -q`删除所有容器(包含正在运行中的)
docker commit -p 容器ID或容器名称 镜像名称保存容器为本地镜像docker commit -p alpine_sleep docker.io/dreamboyplus/alpine:v_1.txt
docker logs 容器ID或容器名称查看容器日志docker logs c8a82e81f856
docker inspect 容器ID或容器名称查询容器详细信息
docker inspect 容器名称grep -A 9 ‘Mounts’查看容器挂载的详细信息

参考资料

轻松掌握docker使用-进阶操作(代码片段)

前言在上一节《轻松掌握Docker使用-基础入门(一)》中我们了解到:Docker是什么?Docker的镜像管理&基础命令Docker容器的基本操作  这一节,我们来学习:如何定制Docker镜像?使用Dockerfile定制镜像... 查看详情

轻松掌握docker使用-进阶操作(代码片段)

前言在上一节《轻松掌握Docker使用-基础入门(一)》中我们了解到:Docker是什么?Docker的镜像管理&基础命令Docker容器的基本操作  这一节,我们来学习:如何定制Docker镜像?使用Dockerfile定制镜像... 查看详情

轻松掌握docker使用-基础入门(代码片段)

...下Docker:Docker是一个开源的应用容器引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。简单来说,Docker就是软件化的集装箱平台,我们每个集装箱的使用互不干扰,运行在Docker上... 查看详情

轻松掌握docker使用-进阶操作(代码片段)

前言在上一节《轻松掌握Docker使用-基础入门(一)》中我们了解到:Docker是什么?Docker的镜像管理&基础命令Docker容器的基本操作  这一节,我们来学习:如何定制Docker镜像?使用Dockerfile定制镜像... 查看详情

docker入门基础(代码片段)

目录一、简介1、docker架构2、docker的概念二、docker的安装和管理1、docker安装2、docker配置阿里云镜像加速3、基础命令一、简介参考https://www.cnblogs.com/linuxk/p/8984242.html1、docker架构Docker使用客户端-服务器(C/S)架构模式,使用远程API来... 查看详情

docker基础用法(代码片段)

...资源更快速的启动时间一致的运行环境持续交付和部署更轻松的迁移更轻松的维护和扩展更快速的交付和部署更高效的虚拟化更轻松的迁移和扩展更简单的管理对比传统虚拟机总结OCI(OpenContainerInitiative)&OCF(OpenContainerFormat)容器... 查看详情

docker基础实战教程:入门(代码片段)

Docker基础实战教程:入门前言HelloDocker!Docker使用的基本过程Docker使用基本实例拉取镜像获取镜像dockerpull命令背后的工作dockerpull的两个基本实例设置镜像加速器导入导出容器dockerexport和dockersave的区别前言Docker是一个操作系统... 查看详情

docker基础知识和命令使用入门(代码片段)

本文介绍了Docker相关的基础知识和命令的简单使用。基础知识部分包括Docker的用途和意义,Docker的镜像、容器、仓库、Dockerfile和DockerCompose的理解,以及Docker图形管理工具Portainer的基础功能。Docker命令的使用包括镜像使用、容器... 查看详情

docker容器技术基础入门(代码片段)

docker容器技术基础入门容器(Container)传统虚拟化与容器的区别:LinuxNamespacesCGroupsLXCdocker基本概念docker工作方式docker容器编排容器(Container)容器是一种基础工具;泛指任何可以用于容纳其他物品的工具,可以部分或完全封闭... 查看详情

要入门urlos应用开发首先要了解docker容器的使用方法(代码片段)

...基于docker容器运行,在入门URLOS开发之前,我们首先需要掌握docker的相关基础知识,本篇就以docker容器的基本使用方法为例,快速的让大家对docker有一个全面的印象。Docker简介Docker是一个开源的应用容器引擎,基于Go语言并遵从Ap... 查看详情

docker从入门到初步掌握(代码片段)

提纲:Docker和Kubernetes关系本人开始今天分享之前想提以前简单的问题,大家可以试试回答。Kubernetes和Docker关系?Kubernetes的作用是管理容器,docker一旦变多,重启,容器通信等一系列过程就需要一个工具来进行管理,这就... 查看详情

一文掌握docker的基本使用方法(代码片段)

Docker基础知识可以参考之前的文章Docker基础知识学会查看帮助可以先敲docker用Tab键补齐,可以看到接下来可以使用的docker--help可以查看详细的命令参数dockerimages--help,类似这样,哪儿不会了看哪儿镜像的拉取/使用从ta... 查看详情

docker基础入门安装以及常见命令(代码片段)

1、docker常见命令以及安装知识详解1、docker安装1、安装版本统一:Dockerversion18.03.0-ce,build0520e242、安装方式:使用dockerrepository安装3、sudoapt-getupdate4、InstallpackagestoallowapttousearepositoryoverHTTPSsudoapt-getinstallapt-trans 查看详情

helm3入门教程全系列,26小时轻松掌握helm(代码片段)

很多人都使用过Ubuntu下的ap-get或者CentOS下的yum,这两者都是Linux系统下的包管理工具。采用apt-get/yum,应用开发者可以管理应用包之间的依赖关系,发布应用;用户则可以以简单的方式查找、安装、升级、卸载应用程序。我... 查看详情

helm3入门教程全系列,26小时轻松掌握helm(代码片段)

很多人都使用过Ubuntu下的ap-get或者CentOS下的yum,这两者都是Linux系统下的包管理工具。采用apt-get/yum,应用开发者可以管理应用包之间的依赖关系,发布应用;用户则可以以简单的方式查找、安装、升级、卸载应用程序。我... 查看详情

docker基础知识及入门(代码片段)

什么是Docker?Docker是由dotcloud公司使用golang语言进行开发的,基于Linux内核的cgroup,namespace,以及OverlayFS类的UnionFS等技术,对进程进行封装隔离,属于操作系统层面的虚拟化技术。由于隔离的进程独立于宿主和其它的隔离的进程,... 查看详情

docker基础入门总结(代码片段)

一、什么是Docker   官方文档描述:“Dockerisanopenplatformfordeveloping,shipping,andrunningapplications.Dockerenablesyoutoseparateyourapplicationsfromyourinfrastructuresoyoucandeliversoftwarequickly.With 查看详情

docker快速入门-情况介绍和安装(代码片段)

...署应用程序称为容器化。容器并不是新事物,但它们用于轻松部署应用程序却是新鲜的。容器化越来越受欢迎 查看详情