Docker容器快速入门

2022-05-06 19:28:06 浏览数 (1)

0、Docker常用命令介绍

  • docker images:列出所有镜像(images)
  • docker ps:列出正在运行的(容器)containers
  • docker pull ubuntu:下载镜像
  • docker run -i -t ubuntu /bin/bash:运行ubuntu镜像
  • docker commit 3a09b2588478 ubuntu:mynewimage:提交你的变更,并且把容器保存成Tag为mynewimage的新的ubuntu镜像.(注意,这里提交只是提交到本地仓库,类似git)

1、官方镜像

1.1 查找镜像

(1)Docker Hub搜索 我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/

(2)命令搜索 我们也可以使用 docker search 命令来搜索镜像。

代码语言:javascript复制
[root@node3 ~]# docker search centos
INDEX       NAME                                     DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                         The official build of CentOS.                   3578      [OK]       
docker.io   docker.io/jdeathe/centos-ssh             CentOS-6 6.9 x86_64 / CentOS-7 7.3.1611 x8...   81                   [OK]
docker.io   docker.io/tutum/centos                   Simple CentOS docker image with SSH access      33                   
docker.io   docker.io/kinogmt/centos-ssh             CentOS with SSH                                 16                   [OK]
docker.io   docker.io/centos/postgresql-94-centos7   PostgreSQL 9.4 SQL database server              11                   
docker.io   docker.io/centos/mysql-57-centos7        MySQL 5.7 SQL database server                   9                    
docker.io   docker.io/centos/php-56-centos7          PHP 5.6 platform for building and running ...   8                    
docker.io   docker.io/centos/mysql-56-centos7        MySQL 5.6 SQL database server                   5                    
docker.io   docker.io/centos/python-35-centos7       Python 3.5 platform for building and runni...   5                    
docker.io   docker.io/centos/mongodb-26-centos7      MongoDB 2.6 NoSQL database server               4                    
docker.io   docker.io/centos/php-70-centos7          PHP 7.0 platform for building and running ...   4                    
docker.io   docker.io/centos/nginx-18-centos7        Nginx 1.8 server and a reverse proxy serve...   3                    
docker.io   docker.io/centos/redis                   Redis built for CentOS                          3                    [OK]
docker.io   docker.io/centos/ruby-23-centos7         Ruby 2.3 platform for building and running...   3                    
docker.io   docker.io/darksheer/centos               Base Centos Image -- Updated hourly             3                    [OK]
docker.io   docker.io/centos/httpd-24-centos7        Apache HTTP 2.4 Server                          2                    
docker.io   docker.io/centos/mongodb-32-centos7      MongoDB 3.2 NoSQL database server               2                    
docker.io   docker.io/centos/python-27-centos7       Python 2.7 platform for building and runni...   2                    
docker.io   docker.io/centos/python-34-centos7       Python 3.4 platform for building and runni...   2                    
docker.io   docker.io/blacklabelops/centos           CentOS Base Image! Built and Updates Daily!     1                    [OK]
docker.io   docker.io/centos/mariadb-101-centos7     MariaDB 10.1 SQL Database Server Docker image   1                    
docker.io   docker.io/centos/nodejs-4-centos7        NodeJS 4 platform for building and running...   1                    
docker.io   docker.io/centos/ruby-22-centos7         Ruby 2.2 platform for building and running...   1                    
docker.io   docker.io/pivotaldata/centos             Base centos, freshened up a little with a ...   0                    
docker.io   docker.io/smartentry/centos              centos with smartentry                          0                    [OK]
[root@node3 ~]# 

1.2 拖取镜像

使用命令 docker pull 来下载镜像

代码语言:javascript复制
[root@node3 ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
74f0853ba93b: Pull complete 
Digest: sha256:26f74cefad82967f97f3eeeef88c1b6262f9b42bc96f2ad61d6f3fdf544759b8
[root@node3 ~]# 

The centos:latest tag is always the most recent version currently available. centos:latest标签始终是当前可用的最新版本。

docker pull centos使用默认tag,相当于docker pull centos:latest

2、创建镜像

当我们从docker镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。 - 从已经创建的容器中更新镜像,并且提交这个镜像 - 使用 Dockerfile 指令来创建一个新的镜像

2.1 Dockerfile 文件

我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。

代码语言:javascript复制
[root@hadron ~]# cd docker/
[root@hadron docker]# vi Dockerfile
[root@hadron docker]# cat Dockerfile 
# 选择一个已有的os镜像作为基础  
FROM centos 
# 镜像的作者   
MAINTAINER hadron
# 安装openssh-server和sudo软件包,并且将sshd的UsePAM参数设置成no   
RUN yum install -y openssh-server sudo   
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config   
#安装openssh-clients 
RUN yum  install -y openssh-clients
# 添加测试用户root,密码root,并且将此用户添加到sudoers里   
RUN echo "root:123456" | chpasswd  
RUN echo "root   ALL=(ALL)       ALL" >> /etc/sudoers   
# 下面这两句比较特殊,在centos6上必须要有,否则创建出来的容器sshd不能登录   
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key   
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key  
# 启动sshd服务并且暴露22端口  
RUN mkdir /var/run/sshd   
EXPOSE 22   
CMD ["/usr/sbin/sshd", "-D"]
[root@hadron docker]#

每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的。 第一条FROM,指定使用哪个镜像源 RUN 指令告诉docker 在镜像内执行命令,安装了什么。。。

2.2 构建镜像

然后,我们使用 Dockerfile 文件,通过 docker build 命令来构建一个镜像。

参数说明:

  • -t :指定要创建的目标镜像名
  • .:Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
代码语言:javascript复制
[root@hadron docker]# docker build -t="centos7-ssh" .
Sending build context to Docker daemon 452.2 MB
Step 1 : FROM centos
 ---> 328edcd84f1b
Step 2 : MAINTAINER hadron
 ---> Running in 393aefdcb913
 ---> 6b5caa772080
Removing intermediate container 393aefdcb913
Step 3 : RUN yum install -y openssh-server sudo
 ---> Running in 33ecc245a07b
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.cn99.com
Resolving Dependencies
--> Running transaction check
---> Package openssh-server.x86_64 0:6.6.1p1-35.el7_3 will be installed
--> Processing Dependency: openssh = 6.6.1p1-35.el7_3 for package: openssh-server-6.6.1p1-35.el7_3.x86_64
--> Processing Dependency: fipscheck-lib(x86-64) >= 1.3.0 for package: openssh-server-6.6.1p1-35.el7_3.x86_64
--> Processing Dependency: libwrap.so.0()(64bit) for package: openssh-server-6.6.1p1-35.el7_3.x86_64
--> Processing Dependency: libfipscheck.so.1()(64bit) for package: openssh-server-6.6.1p1-35.el7_3.x86_64
---> Package sudo.x86_64 0:1.8.6p7-23.el7_3 will be installed
--> Running transaction check
---> Package fipscheck-lib.x86_64 0:1.4.1-5.el7 will be installed
--> Processing Dependency: /usr/bin/fipscheck for package: fipscheck-lib-1.4.1-5.el7.x86_64
---> Package openssh.x86_64 0:6.6.1p1-35.el7_3 will be installed
---> Package tcp_wrappers-libs.x86_64 0:7.6-77.el7 will be installed
--> Running transaction check
---> Package fipscheck.x86_64 0:1.4.1-5.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch        Version                  Repository    Size
================================================================================
Installing:
 openssh-server         x86_64      6.6.1p1-35.el7_3         updates      440 k
 sudo                   x86_64      1.8.6p7-23.el7_3         updates      735 k
Installing for dependencies:
 fipscheck              x86_64      1.4.1-5.el7              base          21 k
 fipscheck-lib          x86_64      1.4.1-5.el7              base          11 k
 openssh                x86_64      6.6.1p1-35.el7_3         updates      438 k
 tcp_wrappers-libs      x86_64      7.6-77.el7               base          66 k

Transaction Summary
================================================================================
Install  2 Packages ( 4 Dependent packages)

Total download size: 1.7 M
Installed size: 4.9 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/fipscheck-lib-1.4.1-5.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for fipscheck-lib-1.4.1-5.el7.x86_64.rpm is not installed
Public key for openssh-6.6.1p1-35.el7_3.x86_64.rpm is not installed
--------------------------------------------------------------------------------
Total                                              1.0 MB/s | 1.7 MB  00:01     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-3.1611.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : fipscheck-1.4.1-5.el7.x86_64                                 1/6 
  Installing : fipscheck-lib-1.4.1-5.el7.x86_64                             2/6 
  Installing : openssh-6.6.1p1-35.el7_3.x86_64                              3/6 
  Installing : tcp_wrappers-libs-7.6-77.el7.x86_64                          4/6 
  Installing : openssh-server-6.6.1p1-35.el7_3.x86_64                       5/6 
  Installing : sudo-1.8.6p7-23.el7_3.x86_64                                 6/6 
  Verifying  : openssh-6.6.1p1-35.el7_3.x86_64                              1/6 
  Verifying  : openssh-server-6.6.1p1-35.el7_3.x86_64                       2/6 
  Verifying  : sudo-1.8.6p7-23.el7_3.x86_64                                 3/6 
  Verifying  : tcp_wrappers-libs-7.6-77.el7.x86_64                          4/6 
  Verifying  : fipscheck-lib-1.4.1-5.el7.x86_64                             5/6 
  Verifying  : fipscheck-1.4.1-5.el7.x86_64                                 6/6 

Installed:
  openssh-server.x86_64 0:6.6.1p1-35.el7_3    sudo.x86_64 0:1.8.6p7-23.el7_3   

Dependency Installed:
  fipscheck.x86_64 0:1.4.1-5.el7       fipscheck-lib.x86_64 0:1.4.1-5.el7      
  openssh.x86_64 0:6.6.1p1-35.el7_3    tcp_wrappers-libs.x86_64 0:7.6-77.el7   

Complete!
 ---> aa6ec2b74d62
Removing intermediate container 33ecc245a07b
Step 4 : RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
 ---> Running in 05de5ccfcd34
 ---> dd486f1c21bc
Removing intermediate container 05de5ccfcd34
Step 5 : RUN yum  install -y openssh-clients
 ---> Running in 93c4b50eb511
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.cn99.com
Resolving Dependencies
--> Running transaction check
---> Package openssh-clients.x86_64 0:6.6.1p1-35.el7_3 will be installed
--> Processing Dependency: libedit.so.0()(64bit) for package: openssh-clients-6.6.1p1-35.el7_3.x86_64
--> Running transaction check
---> Package libedit.x86_64 0:3.0-12.20121213cvs.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch       Version                       Repository   Size
================================================================================
Installing:
 openssh-clients     x86_64     6.6.1p1-35.el7_3              updates     642 k
Installing for dependencies:
 libedit             x86_64     3.0-12.20121213cvs.el7        base         92 k

Transaction Summary
================================================================================
Install  1 Package ( 1 Dependent package)

Total download size: 735 k
Installed size: 2.4 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                              224 kB/s | 735 kB  00:03     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libedit-3.0-12.20121213cvs.el7.x86_64                        1/2 
  Installing : openssh-clients-6.6.1p1-35.el7_3.x86_64                      2/2 
  Verifying  : openssh-clients-6.6.1p1-35.el7_3.x86_64                      1/2 
  Verifying  : libedit-3.0-12.20121213cvs.el7.x86_64                        2/2 

Installed:
  openssh-clients.x86_64 0:6.6.1p1-35.el7_3                                     

Dependency Installed:
  libedit.x86_64 0:3.0-12.20121213cvs.el7                                       

Complete!
 ---> 3c0f86c70d1a
Removing intermediate container 93c4b50eb511
Step 6 : RUN echo "root:123456" | chpasswd
 ---> Running in 6bd8fad74d7c
 ---> 8d60100075d9
Removing intermediate container 6bd8fad74d7c
Step 7 : RUN echo "root   ALL=(ALL)       ALL" >> /etc/sudoers
 ---> Running in fbc4858d3cf8
 ---> 12ac426210fa
Removing intermediate container fbc4858d3cf8
Step 8 : RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
 ---> Running in faf069b7d775
Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private dsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
The key fingerprint is:
99:56:94:7b:47:b3:d5:15:dd:e9:06:e0:3b:c1:4e:8b root@c7947be2eb9d
The key's randomart image is:
 --[ DSA 1024]---- 
|          .o.  .O|
|         .   .o.=|
|          .* .o  |
|          = = oo |
|        SE * ..  |
|       .    .    |
|                 |
|                 |
|                 |
 ----------------- 
 ---> 5aea59496d4c
Removing intermediate container faf069b7d775
Step 9 : RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
 ---> Running in 0195a3faaba7
Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private rsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
53:74:e1:1e:26:63:bb:14:c2:42:94:b6:63:ec:83:15 root@c7947be2eb9d
The key's randomart image is:
 --[ RSA 2048]---- 
|     .o.  . o.   |
|     .E. . o     |
|     o.oo *      |
|      *.   B .   |
|     = .S o .    |
|    . o  o .     |
|       .  .      |
|                 |
|                 |
 ----------------- 
 ---> d88cddfbdd5d
Removing intermediate container 0195a3faaba7
Step 10 : RUN mkdir /var/run/sshd
 ---> Running in 863bb8fea88b
 ---> 3ea00a54726d
Removing intermediate container 863bb8fea88b
Step 11 : EXPOSE 22
 ---> Running in 1a554cd8407d
 ---> 7b2b450fc370
Removing intermediate container 1a554cd8407d
Step 12 : CMD /usr/sbin/sshd -D
 ---> Running in 1eea4cfa8bd5
 ---> 9fd1b9b60b8a
Removing intermediate container 1eea4cfa8bd5
Successfully built 9fd1b9b60b8a

2.3 查看镜像列表

代码语言:javascript复制
[root@hadron docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos7-ssh         latest              9fd1b9b60b8a        17 seconds ago      324.7 MB
docker.io/centos    latest              328edcd84f1b        12 days ago         192.5 MB

3、启动容器

3.1 网络配置

需要用到 pipework,用于给容器设置IP (1)下载pipework

代码语言:javascript复制
[root@hadron docker]# git clone https://github.com/jpetazzo/pipework.git
正克隆到 'pipework'...
remote: Counting objects: 497, done.
remote: Total 497 (delta 0), reused 0 (delta 0), pack-reused 497
接收对象中: 100% (497/497), 171.82 KiB | 0 bytes/s, done.
处理 delta 中: 100% (262/262), done.
[root@hadron docker]# 

[root@hadron docker]# cp pipework/pipework /usr/local/bin/

(2)安装bridge-utils

代码语言:javascript复制
[root@hadron docker]# yum -y install bridge-utils
已加载插件:fastestmirror, langpacks
HuaDongBD                                                                                                                                             | 2.9 kB  00:00:00     
base                                                                                                                                                  | 3.6 kB  00:00:00     
extras                                                                                                                                                | 3.4 kB  00:00:00     
os                                                                                                                                                    | 3.6 kB  00:00:00     
pkerling-seafile                                                                                                                                      | 3.0 kB  00:00:00     
updates                                                                                                                                               | 3.4 kB  00:00:00     
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
软件包 bridge-utils-1.5-9.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@hadron docker]# 

3.2 创建网络

创建挂载容器用的br1网桥

代码语言:javascript复制
[root@hadron docker]# brctl addbr br1
[root@hadron docker]# ip link set dev br1 up
[root@hadron docker]# ip addr add 192.168.3.1/24 dev br1

3.3 启动容器

(1)启动命令

代码语言:javascript复制
[root@hadron docker]# docker run -d --name=centos7-demo centos7-ssh
f5a002ad0f0e115737e7f4adc94584559e952508f91f195b23dd8a70e6c55039

(2)设置IP

代码语言:javascript复制
[root@hadron docker]# pipework br1 centos7-demo 192.168.3.10/24

(3)测试联通 分别使用 ping 与 ssh 命令进行验证,看是否可以ping通和成功登录

代码语言:javascript复制
[root@hadron docker]# ping -c 3 192.168.3.10
PING 192.168.3.10 (192.168.3.10) 56(84) bytes of data.
64 bytes from 192.168.3.10: icmp_seq=1 ttl=64 time=0.111 ms
64 bytes from 192.168.3.10: icmp_seq=2 ttl=64 time=0.081 ms
^C
--- 192.168.3.10 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.081/0.096/0.111/0.015 ms
[root@hadron docker]# ssh 192.168.3.10
Warning: Permanently added '192.168.3.10' (RSA) to the list of known hosts.
root@192.168.3.10's password: 
[root@f5a002ad0f0e ~]# ls -a
.  ..  .bash_logout  .bash_profile  .bashrc  .cshrc  .tcshrc  anaconda-ks.cfg  original-ks.cfg
[root@f5a002ad0f0e ~]# exit
logout
Connection to 192.168.3.10 closed.

3.4容器启停

(1)查看正在运行的容器

代码语言:javascript复制
[root@hadron docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS               NAMES
f5a002ad0f0e        centos7-ssh         "/usr/sbin/sshd -D"   3 minutes ago       Up 3 minutes        22/tcp              centos7-demo
[root@hadron docker]# 

(2)停止容器

代码语言:javascript复制
[root@hadron docker]# docker stop f5a002ad0f0e
f5a002ad0f0e
[root@hadron docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS                     PORTS               NAMES
f5a002ad0f0e        centos7-ssh         "/usr/sbin/sshd -D"   4 minutes ago       Exited (0) 6 seconds ago                       centos7-demo

(3)重新启动容器

代码语言:javascript复制
[root@hadron docker]# docker start f5a002ad0f0e
f5a002ad0f0e
[root@hadron docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS               NAMES
f5a002ad0f0e        centos7-ssh         "/usr/sbin/sshd -D"   4 minutes ago       Up 3 seconds        22/tcp              centos7-demo

(4)重启后IP地址丢失

代码语言:javascript复制
[root@hadron docker]# ping -c 3 192.168.3.10
PING 192.168.3.10 (192.168.3.10) 56(84) bytes of data.
From 192.168.3.1 icmp_seq=1 Destination Host Unreachable
From 192.168.3.1 icmp_seq=2 Destination Host Unreachable
From 192.168.3.1 icmp_seq=3 Destination Host Unreachable

--- 192.168.3.10 ping statistics ---
3 packets transmitted, 0 received,  3 errors, 100% packet loss, time 2000ms
pipe 3
[root@hadron docker]#
[root@hadron docker]# ssh 192.168.3.10
ssh: connect to host 192.168.3.10 port 22: No route to host

Pipework有个缺陷,容器重启后IP设置会自动消失,需要重新设置。

代码语言:javascript复制
[root@hadron docker]# pipework br1 centos7-demo 192.168.3.10/24
您在 /var/spool/mail/root 中有新邮件
[root@hadron docker]# ping -c 3 192.168.3.10
PING 192.168.3.10 (192.168.3.10) 56(84) bytes of data.
64 bytes from 192.168.3.10: icmp_seq=1 ttl=64 time=0.111 ms
64 bytes from 192.168.3.10: icmp_seq=2 ttl=64 time=0.066 ms
64 bytes from 192.168.3.10: icmp_seq=3 ttl=64 time=0.069 ms

--- 192.168.3.10 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.066/0.082/0.111/0.020 ms
[root@hadron docker]# ssh 192.168.3.10
root@192.168.3.10's password: 
Last login: Wed Aug 16 00:42:48 2017 from 192.168.3.1
[root@f5a002ad0f0e ~]#


[root@f5a002ad0f0e ~]# exit
logout
Connection to 192.168.3.10 closed.
[root@hadron docker]# docker stop f5a002ad0f0e
f5a002ad0f0e
[root@hadron docker]# 

0 人点赞