20190622 - docker 学习第二天- - ansible - container

贲俊才
2023-12-01

1; 先开始再创建的container 上面安装ssh, 发现根本没有yum / apt-get install 等关键字可以识别,原因是这个container 安装时的image 有问题。下面先下载一个ubuntu image, 来再create container 就可以了。

root@test3 ansible]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
5b7339215d1d: Pull complete
14ca88e9f672: Pull complete
a31c3b1caad4: Pull complete
b054a26005b7: Pull complete
Digest: sha256:9b1702dcfe32c873a770a32cfd306dd7fc1c4fd134adfb783db68defc8894b3c
Status: Downloaded newer image for ubuntu:latest
[root@test3 ansible]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              4c108a37151f        2 days ago          64.2MB
busybox             latest              e4db68de4ff2        6 days ago          1.22MB
httpd               latest              e77c77f17b46        10 days ago         140MB


2:开始安装container3:

[root@test3 ssh]# docker run -itd --name=container3 -p 23:22 ubuntu
b712e632044d127b03694039a67970043b78cee7b06d21617223ed22fb9ead2b

上面表示已经安装好了。


[root@test3 ssh]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
b712e632044d        ubuntu              "/bin/bash"         11 seconds ago      Up 11 seconds       0.0.0.0:23->22/tcp   container3
417698c45ca4        busybox             "sh"                8 hours ago         Up 25 minutes                            container1
[root@test3 ssh]# netstat -an | grep -i 23
tcp6       0      0 :::23                   :::*                    LISTEN     
udp        0      0 127.0.0.1:323           0.0.0.0:*                          
udp6       0      0 ::1:323                 :::*                               
unix  2      [ ACC ]     STREAM     LISTENING     39323    /var/run/docker/libcontainerd/docker-containerd.sock
unix  3      [ ]         STREAM     CONNECTED     40238    
unix  3      [ ]         STREAM     CONNECTED     40123    

下面进入container3:

[root@test3 ssh]# docker attach container3
root@b712e632044d:/# df -h
Filesystem               Size  Used Avail Use% Mounted on
overlay                  8.0G  1.8G  6.3G  22% /
tmpfs                    496M     0  496M   0% /dev
tmpfs                    496M     0  496M   0% /sys/fs/cgroup
/dev/mapper/centos-root  8.0G  1.8G  6.3G  22% /etc/hosts
shm                       64M     0   64M   0% /dev/shm
tmpfs                    496M     0  496M   0% /sys/firmware

3: 先修改一下root 密码:

root@b712e632044d:/# passwd  
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully


然后安装一下VIM, 因为要修改/etc/ssh/sshd_config 文件。

root@b712e632044d:/# apt install -y vim
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package vim

然后安装一下openssh-server.
root@b712e632044d:/# apt install -y openssh-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package openssh-server

上面报错: unable to locate package .... 这个可以通过更新package :apt-get update 来解决。

然后再安装vim / openssh-server 就可以了。

然后修改一下/etc/ssh/sshd_config

root@b712e632044d:/etc/ssh# vim sshd_config

注释掉PermitRootLogin prohibit-password

添加PermitRootLogin yes

添加UsePAM no


root@b712e632044d:/etc/ssh# service ssh start
 * Starting OpenBSD Secure Shell server sshd

上面表示 ssh 再container 里面已经启动啦。

因为这个container3 上已经安装好了很多东西,所以这个container3 可以创建一个新的image 来为后来使用:

[root@test3 ssh]# docker export -o ubuntu-ssh-20190621.tar container3

使用这个备份来创建image 可以用docker import:

[root@test3 ssh]# docker import ubuntu-ssh-20190621.tar ubuntu-ssh-20190621:ansible
sha256:56d13e683494e3b7ec71195f95a306c80ae0aeb51eb138123662b1f0d49a76a3
[root@test3 ssh]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
ubuntu-ssh-20190621   ansible             56d13e683494        11 seconds ago      240MB
ubuntu                latest              4c108a37151f        2 days ago          64.2MB
busybox               latest              e4db68de4ff2        6 days ago          1.22MB
httpd                 latest              e77c77f17b46        10 days ago         140MB

可以看到这个image 的size 变成240M 很大了,说明是安装了很多东西。上面export 的时候,要注意tar 文件存放的位置,方便以后找,可以一个path:

docker export -o /var/lib/docker/image/ubuntu-ssh-20190621.tar container3

4: 下面开始研究ansible:

先把key 配好:ssh-keygen -t rsa

建立连接后,再/etc/ansible/host 里面配置好host:

ansible 测试一下:

[root@test3 .ssh]# ansible docker -m ping
b712e632044d | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"

5: 下面把成功的container3 再建立一个新的image, 来以后使用:

docker export -o /var/lib/docker/image/ubuntu-ssh-20190622.tar container3

可以把原来的image 备份文件删除:

root@test3 image]# ls -lrt
total 486124
drwx------. 5 root root        81 Jun 21 10:06 overlay
-rw-------. 1 root root 248888832 Jun 21 23:06 ubuntu-ssh-20190621.tar
-rw-------. 1 root root 248899072 Jun 22 00:21 ubuntu-ssh-20190622.tar
[root@test3 image]# rm ubuntu-ssh-20190621.tar
rm: remove regular file ‘ubuntu-ssh-20190621.tar’? y
[root@test3 image]# df -h

可以看到 “/” 的文件系统利用降下来了。

再从 image 上删除:

[root@test3 image]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
ubuntu-ssh-20190621   ansible             56d13e683494        14 hours ago        240MB
ubuntu                latest              4c108a37151f        3 days ago          64.2MB
busybox               latest              e4db68de4ff2        7 days ago          1.22MB
httpd                 latest              e77c77f17b46        10 days ago         140MB


[root@test3 image]# docker import ubuntu-ssh-20190622.tar ubuntu-ssh-20190622:ansible
sha256:e91bf94613021802347a16be4967458ca8c723d3b614b076fef2f31c809ecb6f

[root@test3 image]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
ubuntu-ssh-20190622   ansible             e91bf9461302        7 seconds ago       240MB
ubuntu-ssh-20190621   ansible             56d13e683494        14 hours ago        240MB
ubuntu                latest              4c108a37151f        3 days ago          64.2MB
busybox               latest              e4db68de4ff2        7 days ago          1.22MB
httpd                 latest              e77c77f17b46        10 days ago         140MB

删除images,通过image的id来指定删除谁

docker rmi <image id>

[root@test3 image]# docker rmi 56d13e683494
Untagged: ubuntu-ssh-20190621:ansible
Deleted: sha256:56d13e683494e3b7ec71195f95a306c80ae0aeb51eb138123662b1f0d49a76a3
Deleted: sha256:d945d2d2e7c985a26cda768e370e0e879ec279d8b63840814d6ed913c334d0bb


[root@test3 image]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
ubuntu-ssh-20190622   ansible             e91bf9461302        5 minutes ago       240MB
ubuntu                latest              4c108a37151f        3 days ago          64.2MB
busybox               latest              e4db68de4ff2        7 days ago          1.22MB
httpd                 latest              e77c77f17b46        10 days ago         140MB


[root@test3 image]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
b712e632044d        ubuntu              "/bin/bash"         15 hours ago        Up 15 hours         0.0.0.0:23->22/tcp   container3
[root@test3 image]#

 

 类似资料: