東川印記

一本東川,笑看爭龍斗虎;寰茫兦者,度橫佰昧人生。

ubuntu18.04 安装学习 docker

2021年4月6日星期二



继上篇在盒子里通过LinuxDeploy安装上docker一堆问题后。。。。

1,Ubuntu 18.04 开启 ssh

1)安装

senrsl@senrsl-ubuntu:~$ sudo apt-get install openssh-server
[sudo] senrsl 的密码:
正在读取软件包列表... 完成
正在分析软件包的依赖关系树      
正在读取状态信息... 完成

2)配置

senrsl@senrsl-ubuntu:~$ sudo vi /etc/ssh/sshd_config

#LoginGraceTime 2m
#PermitRootLogin prohibit-password
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

3)启动

senrsl@senrsl-ubuntu:~$ sudo service ssh restart
senrsl@senrsl-ubuntu:~$ sudo service ssh status
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2021-03-12 16:49:02 CST; 6s ago
  Process: 6963 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
 Main PID: 6964 (sshd)
    Tasks: 1 (limit: 4632)
   CGroup: /system.slice/ssh.service
           └─6964 /usr/sbin/sshd -D

3月 12 16:49:02 senrsl-ubuntu systemd[1]: Starting OpenBSD Secure Shell server...
3月 12 16:49:02 senrsl-ubuntu sshd[6964]: Server listening on 0.0.0.0 port 22.
3月 12 16:49:02 senrsl-ubuntu sshd[6964]: Server listening on :: port 22.
3月 12 16:49:02 senrsl-ubuntu systemd[1]: Started OpenBSD Secure Shell server.
senrsl@senrsl-ubuntu:~$

4)连接

ssh的时候 用户名竟然区分大小写。。。。

SENRSL:Downloads senrsl$ ssh senrsl@192.168.7.89
senrsl@192.168.7.89's password:
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.18.0-15-generic x86_64)


599 个可升级软件包。
415 个安全更新。

有新版本"20.04.2 LTS"可供使用
运行"do-release-upgrade"来升级到新版本。

Your Hardware Enablement Stack (HWE) is supported until April 2023.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

senrsl@senrsl-ubuntu:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:47:cd:fa brd ff:ff:ff:ff:ff:ff
    inet 192.168.7.89/23 brd 192.168.7.255 scope global dynamic noprefixroute ens33
       valid_lft 85772sec preferred_lft 85772sec
    inet6 fe80::6c46:763b:895a:2dfa/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
senrsl@senrsl-ubuntu:~$

如丝般顺滑。。。。


2,安docker

senrsl@senrsl-ubuntu:~$ sudo apt-get update

# 添加 docker 官方 GPG

senrsl@senrsl-ubuntu:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Command 'curl' not found, but can be installed with:

sudo apt install curl

gpg: 找不到有效的 OpenPGP 数据。
senrsl@senrsl-ubuntu:~$ sudo apt install curl

Restarting services possibly affected by the upgrade:

Services restarted successfully.

senrsl@senrsl-ubuntu:~$ curl --version
curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3
Release-Date: 2018-01-24
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL
senrsl@senrsl-ubuntu:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK

# 添加docker库

senrsl@senrsl-ubuntu:~$ sudo add-apt-repository \
>    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
>    $(lsb_release -cs) \
>    stable"
senrsl@senrsl-ubuntu:~$ sudo apt-get update

senrsl@senrsl-ubuntu:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io

安装完成,配置用户组

senrsl@senrsl-ubuntu:~$ docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)

Server:
ERROR: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/info: dial unix /var/run/docker.sock: connect: permission denied
errors pretty printing info
senrsl@senrsl-ubuntu:~$ sudo groupadd docker
groupadd:"docker"组已存在
senrsl@senrsl-ubuntu:~$ sudo gpasswd -a $USER docker
正在将用户"senrsl"加入到"docker"组中
senrsl@senrsl-ubuntu:~$ newgrp docker
senrsl@senrsl-ubuntu:~$ docker version
Client: Docker Engine - Community
 Version:           20.10.5
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        55c4c88
 Built:             Tue Mar  2 20:18:05 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.5
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       363e9a8
  Built:            Tue Mar  2 20:16:00 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
senrsl@senrsl-ubuntu:~$ docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.5
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc version: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 4.18.0-15-generic
 Operating System: Ubuntu 18.04.2 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 3.829GiB
 Name: senrsl-ubuntu
 ID: NK7B:UXTH:OJ5W:LB5O:2C7O:JKSL:US7G:QUPB:VQY4:GPFT:IS4X:QSJC
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: No swap limit support
senrsl@senrsl-ubuntu:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
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/

senrsl@senrsl-ubuntu:~$

hello world 成功。。。。


3,安装 docker-compose

这玩意看起来 是用来 做多容器自动化部署的。。。。

Docker Compose is a tool for running multi-container applications on Docker defined using the Compose file format.

senrsl@senrsl-ubuntu:~/Downloads$ sudo curl -L https://github.com/docker/compose/releases/download/1.28.5/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:02:10 --:--:--     0curl: (7) Failed to connect to github.com port 443: 连接超时
senrsl@senrsl-ubuntu:~/Downloads$ sudo curl -L https://github.com/docker/compose/releases/download/1.28.5/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   633  100   633    0     0    752      0 --:--:-- --:--:-- --:--:--   752
100 11.6M  100 11.6M    0     0   300k      0  0:00:39  0:00:39 --:--:--  253k
senrsl@senrsl-ubuntu:~/Downloads$  sudo chmod +x /usr/local/bin/docker-compose
senrsl@senrsl-ubuntu:~/Downloads$ docker-compose --version
docker-compose version 1.28.5, build c4eb3a1f
senrsl@senrsl-ubuntu:~/Downloads$

两会应该快闭幕了吧。。。。


4,docker 换国内镜像

虽然没有低速困扰,不过也可以换一换。。。。

senrsl@senrsl-ubuntu:~/Downloads$ sudo vi /etc/docker/daemon.json
senrsl@senrsl-ubuntu:~/Downloads$ cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
senrsl@senrsl-ubuntu:~/Downloads$ systemctl daemon-reload
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ===
重新载入 systemd 状态需要认证。
Authenticating as: senRsl,,, (senrsl)
Password:
==== AUTHENTICATION COMPLETE ===
senrsl@senrsl-ubuntu:~/Downloads$ systemctl restart docker
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
重新启动"docker.service"需要认证。
Authenticating as: senRsl,,, (senrsl)
Password:
==== AUTHENTICATION COMPLETE ===
senrsl@senrsl-ubuntu:~/Downloads$ sudo groupadd docker
groupadd:"docker"组已存在
senrsl@senrsl-ubuntu:~/Downloads$ sudo gpasswd -a $USER docker
正在将用户"senrsl"加入到"docker"组中
senrsl@senrsl-ubuntu:~/Downloads$ newgrp docker
senrsl@senrsl-ubuntu:~/Downloads$

5,常用命令

1)显示本地镜像库内容

senrsl@senrsl-ubuntu:~/Downloads$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED      SIZE
hello-world   latest    d1165f221234   6 days ago   13.3kB
senrsl@senrsl-ubuntu:~/Downloads$

2)帮助

senrsl@senrsl-ubuntu:~/Downloads$ docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/home/senrsl/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with
                           "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/home/senrsl/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/home/senrsl/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/home/senrsl/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Build with BuildKit (Docker Inc., v0.5.1-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
senrsl@senrsl-ubuntu:~/Downloads$

3)显示运行中的容器

senrsl@senrsl-ubuntu:~/Downloads$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
senrsl@senrsl-ubuntu:~/Downloads$

4)显示所有容器

senrsl@senrsl-ubuntu:~/Downloads$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
8a18a55b7def   hello-world   "/hello"   27 minutes ago   Exited (0) 27 minutes ago             competent_ishizaka
senrsl@senrsl-ubuntu:~/Downloads$

5)


6,docker 安装 centos7

6.1)版本搜索

可以直接 search

senrsl@senrsl-ubuntu:~/Downloads$ docker search centos
NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                             The official build of CentOS.                   6450      [OK]      
ansible/centos7-ansible            Ansible on Centos7                              132                  [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   126                  [OK]
jdeathe/centos-ssh                 OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …   117                  [OK]
centos/systemd                     systemd enabled base container.                 96                   [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              58                   [OK]
tutum/centos                       Simple CentOS docker image with SSH access      46                  
kinogmt/centos-ssh                 CentOS with SSH                                 29                   [OK]
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   13                  
guyton/centos6                     From official centos6 container with full up…   10                   [OK]
centos/tools                       Docker image that has systems administration…   7                    [OK]
drecom/centos-ruby                 centos ruby                                     6                    [OK]
pivotaldata/centos                 Base centos, freshened up a little with a Do…   5                   
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   3                   
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   3                   
darksheer/centos                   Base Centos Image -- Updated hourly             3                    [OK]
mamohr/centos-java                 Oracle Java 8 Docker image based on Centos 7    3                    [OK]
indigo/centos-maven                Vanilla CentOS 7 with Oracle Java Developmen…   2                    [OK]
amd64/centos                       The official build of CentOS.                   2                   
miko2u/centos6                     CentOS6 日本語環境                                   2                    [OK]
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                    [OK]
mcnaughton/centos-base             centos base image                               1                    [OK]
smartentry/centos                  centos with smartentry                          0                    [OK]
pivotaldata/centos6.8-dev          CentosOS 6.8 image for GPDB development         0                   
pivotaldata/centos7-dev            CentosOS 7 image for GPDB development           0                   
senrsl@senrsl-ubuntu:~/Downloads$

这种没有版本号,版本号可以从网页查看 https://hub.docker.com/search?q=centos&type=image

7的版本显示是 centos7, 7, centos7.9.2009, 7.9.2009

此处配图dcoker1


6.2)指定版本下载

senrsl@senrsl-ubuntu:~/Downloads$ docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
senrsl@senrsl-ubuntu:~/Downloads$


6.3)查看本地镜像库

senrsl@senrsl-ubuntu:~/Downloads$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   6 days ago     13.3kB
centos        7         8652b9f0cb4c   3 months ago   204MB
senrsl@senrsl-ubuntu:~/Downloads$

6.4)装载镜像运行

senrsl@senrsl-ubuntu:~/Downloads$ docker run -dit --name centos7 centos:7 /bin/bash
af4db6bd8e5b1dbc361d01610a007e8992a9c20fe8c6f44b4cedeb0c0e66a303
senrsl@senrsl-ubuntu:~/Downloads$

参数:

-d : 后台运行容器并返回容器ID;

-i : 交互模式运行容器;

-t : 为容器重新输入一个伪输入终端;


6.5)查看正在运行容器

senrsl@senrsl-ubuntu:~/Downloads$ docker ps
CONTAINER ID   IMAGE      COMMAND       CREATED         STATUS         PORTS     NAMES
af4db6bd8e5b   centos:7   "/bin/bash"   2 minutes ago   Up 2 minutes             centos7
senrsl@senrsl-ubuntu:~/Downloads$

6.6)进出容器

senrsl@senrsl-ubuntu:~/Downloads$ docker container ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                      PORTS     NAMES
af4db6bd8e5b   centos:7      "/bin/bash"   4 minutes ago    Up 3 minutes                          centos7
8a18a55b7def   hello-world   "/hello"      55 minutes ago   Exited (0) 55 minutes ago             competent_ishizaka
senrsl@senrsl-ubuntu:~/Downloads$ docker container ps
CONTAINER ID   IMAGE      COMMAND       CREATED         STATUS         PORTS     NAMES
af4db6bd8e5b   centos:7   "/bin/bash"   4 minutes ago   Up 4 minutes             centos7
senrsl@senrsl-ubuntu:~/Downloads$ docker attach af4db6bd8e5b
[root@af4db6bd8e5b /]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

[root@af4db6bd8e5b /]# exit
exit
senrsl@senrsl-ubuntu:~/Downloads$

进进出出,好不快活。。。。


6.7)docker container 帮助

senrsl@senrsl-ubuntu:~/Downloads$ docker container --help

Usage:  docker container COMMAND

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.
senrsl@senrsl-ubuntu:~/Downloads$


6.8)可以不用加 container

senrsl@senrsl-ubuntu:~/Downloads$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
senrsl@senrsl-ubuntu:~/Downloads$ docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED             STATUS                         PORTS     NAMES
af4db6bd8e5b   centos:7      "/bin/bash"   8 minutes ago       Exited (0) 2 minutes ago                 centos7
8a18a55b7def   hello-world   "/hello"      About an hour ago   Exited (0) About an hour ago             competent_ishizaka
senrsl@senrsl-ubuntu:~/Downloads$ docker start af4db6bd8e5b
af4db6bd8e5b
senrsl@senrsl-ubuntu:~/Downloads$ docker stop af4db6bd8e5b
af4db6bd8e5b
senrsl@senrsl-ubuntu:~/Downloads$

6.9)也可以直接用名称操作

senrsl@senrsl-ubuntu:~/Downloads$ docker start centos7
centos7
senrsl@senrsl-ubuntu:~/Downloads$ docker ps
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS         PORTS     NAMES
af4db6bd8e5b   centos:7   "/bin/bash"   12 minutes ago   Up 4 seconds             centos7
senrsl@senrsl-ubuntu:~/Downloads$

6.10)容器化

uname显示 外面的虚拟机 Ubuntu,但是里面还是有centos的。。。。

senrsl@senrsl-ubuntu:~$ docker attach centos7
[root@af4db6bd8e5b /]# pwd
/
[root@af4db6bd8e5b /]# uname -a
Linux af4db6bd8e5b 4.18.0-15-generic #16~18.04.1-Ubuntu SMP Thu Feb 7 14:06:04 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
[root@af4db6bd8e5b /]# ls /etc/
Display all 118 possibilities? (y or n)
[root@af4db6bd8e5b /]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

[root@af4db6bd8e5b /]#


6.11)centos安ssh


[root@af4db6bd8e5b /]# yum install passwd openssl openssh-server -y

...

Complete!

[root@af4db6bd8e5b /]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
[root@af4db6bd8e5b /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
[root@af4db6bd8e5b /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
[root@af4db6bd8e5b /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key  -N ''
Generating public/private dsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_ed25519_key.
Your public key has been saved in /etc/ssh/ssh_host_ed25519_key.pub.
The key fingerprint is:
SHA256:DipA/vPJEXGh+8IiliVtxxgjCXbhRkjipvzhV9unFtE root@af4db6bd8e5b
The key's randomart image is:
+---[DSA 1024]----+
|+o.+.  .         |
|=.=   . .        |
| * = o .  .      |
|* + = +  . E     |
|o+ * =..S .      |
| .O +.+oo.       |
| +.*.= o....     |
|. ..* +  .o      |
|     +  ..       |
+----[SHA256]-----+
[root@af4db6bd8e5b /]# vi /etc/ssh/sshd_config

    UsePAM yes 改为 UsePAM no;
    UsePrivilegeSeparation sandbox 改为 UsePrivilegeSeparation no。

[root@af4db6bd8e5b /]# /usr/sbin/sshd
WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several problems.
[root@af4db6bd8e5b /]#

6.12)设置密码

[root@af4db6bd8e5b /]# passwd root
Changing password for user root.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
[root@af4db6bd8e5b /]#

6.13)查看ip

[root@af4db6bd8e5b /]# ip addr | grep eth0
bash: ip: command not found
[root@af4db6bd8e5b /]# ifconfig
bash: ifconfig: command not found
[root@af4db6bd8e5b /]# ip
bash: ip: command not found
[root@af4db6bd8e5b /]# ping 192.168.2.60
PING 192.168.2.60 (192.168.2.60) 56(84) bytes of data.
64 bytes from 192.168.2.60: icmp_seq=1 ttl=62 time=2.72 ms
64 bytes from 192.168.2.60: icmp_seq=2 ttl=62 time=10.0 ms
^C
--- 192.168.2.60 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 2.725/6.363/10.002/3.639 ms
[root@af4db6bd8e5b /]#

[root@af4db6bd8e5b /]# yum install net-tools
Loaded plugins: fastestmirror, ovl
...

[root@af4db6bd8e5b /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 12045  bytes 17239954 (16.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7844  bytes 536338 (523.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@af4db6bd8e5b /]#

看起来是个NAT模式 -> 7)网络模式

6.14)保存修改后镜像,端口映射

终于到了皮毛之地了

退出容器,查看当前运行容器

[root@af4db6bd8e5b /]# exit
exit
senrsl@senrsl-ubuntu:~$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
senrsl@senrsl-ubuntu:~$ docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED      STATUS                     PORTS     NAMES
af4db6bd8e5b   centos:7      "/bin/bash"   3 days ago   Exited (0) 5 seconds ago             centos7
8a18a55b7def   hello-world   "/hello"      3 days ago   Exited (0) 3 days ago                competent_ishizaka
senrsl@senrsl-ubuntu:~$

端口映射

保存修改后的容器为镜像

senrsl@senrsl-ubuntu:~$ docker commit af4db6bd8e5b local/centos7-ssh
sha256:dbccb6fd0747ecec107e18d34bfa10b62b496f0334ed80b6f92d157f57355387

本地所有镜像

senrsl@senrsl-ubuntu:~$ docker images
REPOSITORY          TAG       IMAGE ID       CREATED         SIZE
local/centos7-ssh   latest    dbccb6fd0747   4 seconds ago   312MB
hello-world         latest    d1165f221234   10 days ago     13.3kB
centos              7         8652b9f0cb4c   4 months ago    204MB

基于新镜像运行新容器

     -p 映射宿主机 10033到容器22端口,即sshd默认端口;

     /usr/sbin/sshd -D ——执行容器的/usr/sbin/sshd命令,-D将sshd作为前台进程运行,而不是脱离控制台成为后台守护进程。

senrsl@senrsl-ubuntu:~$ docker run -d --name centos7-ssh -p 10033:22 local/centos7-ssh:latest /usr/sbin/sshd -D
c55b4d286288888f0eb9c4bfb5da6b9f104c4ad5a8f72bbca70a2012627c9e59

查看端口映射情况

senrsl@senrsl-ubuntu:~$ docker container ps -a
CONTAINER ID   IMAGE                      COMMAND               CREATED          STATUS                     PORTS                   NAMES
c55b4d286288   local/centos7-ssh:latest   "/usr/sbin/sshd -D"   29 seconds ago   Up 28 seconds              0.0.0.0:10033->22/tcp   centos7-ssh
af4db6bd8e5b   centos:7                   "/bin/bash"           3 days ago       Exited (0) 4 minutes ago                           centos7
8a18a55b7def   hello-world                "/hello"              3 days ago       Exited (0) 3 days ago                              competent_ishizaka
senrsl@senrsl-ubuntu:~$ docker port c55b4d286288
22/tcp -> 0.0.0.0:10033
senrsl@senrsl-ubuntu:~$

查看当前所有容器

senrsl@senrsl-ubuntu:~$ docker ps -a
CONTAINER ID   IMAGE                      COMMAND               CREATED          STATUS                      PORTS                   NAMES
c55b4d286288   local/centos7-ssh:latest   "/usr/sbin/sshd -D"   29 minutes ago   Up 29 minutes               0.0.0.0:10033->22/tcp   centos7-ssh
af4db6bd8e5b   centos:7                   "/bin/bash"           3 days ago       Exited (0) 33 minutes ago                           centos7
8a18a55b7def   hello-world                "/hello"              3 days ago       Exited (0) 3 days ago                               competent_ishizaka
senrsl@senrsl-ubuntu:~$

6.15) docker attach centos7-ssh

一直卡着不动

重新建试试,跳转8


7,docker的网络模式

senrsl@senrsl-ubuntu:~$ docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
10e0e8f01a49   bridge    bridge    local
13c3a262fc8b   host      host      local
306a9d8b05b5   none      null      local
senrsl@senrsl-ubuntu:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:47:cd:fa brd ff:ff:ff:ff:ff:ff
    inet 192.168.7.89/23 brd 192.168.7.255 scope global dynamic noprefixroute ens33
       valid_lft 61337sec preferred_lft 61337sec
    inet6 fe80::6c46:763b:895a:2dfa/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    link/ether 02:42:c4:4d:07:b4 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:c4ff:fe4d:7b4/64 scope link
       valid_lft forever preferred_lft forever
senrsl@senrsl-ubuntu:~$

几种网络模式

1、bridge模式,--net=bridge(默认)

2、host模式,--net=host

3、container模式 使用–net =container:指定容器名

4、none模式,--net=none

5、用户自定义模式


8,docker 衍生子镜像

senrsl@senrsl-ubuntu:~/Downloads$ docker commit af4db6bd8e5b local/centos7-ssh-2
sha256:e7a80db8302c1796682d04ee4954623434e3fd39258f90574ef0656df1d4553b
senrsl@senrsl-ubuntu:~/Downloads$ docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
local/centos7-ssh-2   latest    e7a80db8302c   6 seconds ago   318MB
centos                7         8652b9f0cb4c   4 months ago    204MB
senrsl@senrsl-ubuntu:~/Downloads$

这样就只提交了一个镜像,也就是 黑箱镜像

使用此镜像创建容器,

senrsl@senrsl-ubuntu:~/Downloads$ docker run -d --name centos7-ssh2 -p 10033:22 local/centos7-ssh-2:latest /usr/sbin/sshd -D
f7d36d1269bf3247f01eb9a26888e6a74059d7844a5daba70c6bd89b32ab9877
senrsl@senrsl-ubuntu:~/Downloads$

奇怪,还是attach 不进去。。。。

root@senrsl-ubuntu:/home/senrsl# lsof -i:22
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     841   root    3u  IPv4  43848      0t0  TCP *:ssh (LISTEN)
sshd     841   root    4u  IPv6  43850      0t0  TCP *:ssh (LISTEN)
sshd    4604   root    3u  IPv4  71988      0t0  TCP senrsl-ubuntu:ssh->192.168.6.99:49164 (ESTABLISHED)
sshd    4669 senrsl    3u  IPv4  71988      0t0  TCP senrsl-ubuntu:ssh->192.168.6.99:49164 (ESTABLISHED)
sshd    5387   root    3u  IPv4  77981      0t0  TCP senrsl-ubuntu:ssh->192.168.6.99:63944 (ESTABLISHED)
sshd    5476 senrsl    3u  IPv4  77981      0t0  TCP senrsl-ubuntu:ssh->192.168.6.99:63944 (ESTABLISHED)
root@senrsl-ubuntu:/home/senrsl# lsof -i:23
root@senrsl-ubuntu:/home/senrsl# exit
exit
senrsl@senrsl-ubuntu:~$

换了个23端口也不行,也不是端口问题

senrsl@senrsl-ubuntu:~$ docker run -it -d -p 10033:22 --name centos7x2 e7a80db8302c
67bc0e3f47a909889bb4691f753782d1011ae0b0d9cd4001a660a79961ada855
senrsl@senrsl-ubuntu:~$ docker ps
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS          PORTS                   NAMES
67bc0e3f47a9   e7a80db8302c   "/bin/bash"   7 seconds ago   Up 6 seconds    0.0.0.0:10033->22/tcp   centos7x2
af4db6bd8e5b   centos:7       "/bin/bash"   2 weeks ago     Up 33 minutes                           centos7
senrsl@senrsl-ubuntu:~$ docker attach 67bc0e3f47a9
[root@67bc0e3f47a9 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 21  bytes 2669 (2.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@67bc0e3f47a9 /]#

执行这条命令就行了,原来是装载的时候没有装载 -it 终端啥的。。。。。。

最后,汇总一下,这样就行了

senrsl@senrsl-ubuntu:~$ docker run -it -d --name centos7-ssh-3 -p 10033:22 local/centos7-ssh-2:latest /usr/sbin/sshd -D
14939ce5316f5420228974633b40a2ec40681a5255b8657cf4c5a0a4e993735a
senrsl@senrsl-ubuntu:~$

出入平安

senrsl@senrsl-ubuntu:~/Downloads$ ssh root@172.17.0.3
root@172.17.0.3's password:
Last login: Wed Mar 31 03:34:29 2021 from 172.17.0.1
[root@14939ce5316f ~]# exit
logout
Connection to 172.17.0.3 closed.
senrsl@senrsl-ubuntu:~/Downloads$

实际上线的时候,应该不需要加 -it参数,因为这个新的镜像只是衍生品,只用来运行,修改应该在父镜像中进行。。。。

补充:

过了几天在复习这里发现只加-it不行,比对一下两个容器

senrsl@senrsl-ubuntu:~$ docker run -it -d --name centos7-ssh5 -p 10033:22 local/centos7-ssh4:latest /usr/sbin/sshd -D
6c95b2784beb7dbe79780f7cd7307c415f70ef873b8add3a3ac8fc45161a2fb2
senrsl@senrsl-ubuntu:~$ docker ps
CONTAINER ID   IMAGE                       COMMAND               CREATED         STATUS         PORTS                  NAMES
6c95b2784beb   local/centos7-ssh4:latest   "/usr/sbin/sshd -D"   5 seconds ago   Up 4 seconds   0.0.0.0:1033->22/tcp   centos7-ssh5
af4db6bd8e5b   centos:7                    "/bin/bash"           3 weeks ago     Up 6 days                             centos7
senrsl@senrsl-ubuntu:~$

可以attach的容器 命令是 bin/bash;不能进的容器命令是启动sshd的命令,所以如果需要进可能得改改

尝试

senrsl@senrsl-ubuntu:~$ docker run -dit --name centos7-ssh6 -p 10033:22 local/centos7-ssh4:latest sh -c '/bin/bash && /usr/sbin/sshd -D'

这么写 能attach又不能ssh了。。。。

试了一遭,都不行,最后。。。。

senrsl@senrsl-ubuntu:~$ docker run -dit --name centos7-ssh6 -p 10033:22 local/centos7-ssh4:latest /bin/bash

senrsl@senrsl-ubuntu:~$ docker ps
CONTAINER ID   IMAGE                       COMMAND       CREATED         STATUS         PORTS                   NAMES
a7eabfd63f68   local/centos7-ssh4:latest   "/bin/bash"   7 seconds ago   Up 6 seconds   0.0.0.0:10033->22/tcp   centos7-ssh6
senrsl@senrsl-ubuntu:~$ docker attach a7eabfd63f68
[root@a7eabfd63f68 /]# /usr/sbin/sshd -D
WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several problems.
read escape sequence
senrsl@senrsl-ubuntu:~$

senrsl@senrsl-ubuntu:~$ telnet 172.17.0.2  22
Trying 172.17.0.2...
Connected to 172.17.0.2.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4
telnet> q
Connection closed.
senrsl@senrsl-ubuntu:~$

先启动bash,再attach进去启动sshd.....

不过正常的使用场景,应该不需要衍生品需要bash吧。。。。

或者bash进去执行个脚本。。。。

跳转11


9,docker 退出但不停止运行

在docker 镜像中执行 exit后 docker 会停止运行,此命令  同 Ctrl + D 快捷键;

使用 Ctrl + P + Q 快捷键可使其 退出但不停止运行


[root@af4db6bd8e5b /]# exit     //这个exit对应 Ctrl +D快捷键
senrsl@senrsl-ubuntu:~/Downloads$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
senrsl@senrsl-ubuntu:~/Downloads$ docker start centos7
centos7
senrsl@senrsl-ubuntu:~/Downloads$ docker attach centos7
[root@af4db6bd8e5b /]# /usr/sbin/sshd -D &
[1] 16
WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several problems.

[root@af4db6bd8e5b /]#
[root@af4db6bd8e5b /]# read escape sequence    //这个对应 Ctrl +P+Q快捷键
senrsl@senrsl-ubuntu:~/Downloads$

对应 docker 的 ssh

[root@af4db6bd8e5b ~]#   //Ctrl +D 或 exit 自动断开

Connection to 172.17.0.2 closed by remote host.
Connection to 172.17.0.2 closed.
senrsl@senrsl-ubuntu:~/Downloads$ ssh root@172.17.0.2
ssh: connect to host 172.17.0.2 port 22: Connection refused
senrsl@senrsl-ubuntu:~/Downloads$ ssh root@172.17.0.2
root@172.17.0.2's password:
Last login: Wed Mar 31 03:30:38 2021 from 172.17.0.1
[root@af4db6bd8e5b ~]# ls
anaconda-ks.cfg
[root@af4db6bd8e5b ~]#    //Ctrl + P +Q  不断开

10,docker 删除容器和镜像

senrsl@senrsl-ubuntu:~/Downloads$ docker ps -a
CONTAINER ID   IMAGE                      COMMAND               CREATED       STATUS                      PORTS     NAMES
c55b4d286288   local/centos7-ssh:latest   "/usr/sbin/sshd -D"   2 weeks ago   Exited (0) 41 minutes ago             centos7-ssh
af4db6bd8e5b   centos:7                   "/bin/bash"           2 weeks ago   Up 9 minutes                          centos7
8a18a55b7def   hello-world                "/hello"              2 weeks ago   Exited (0) 2 weeks ago                competent_ishizaka
senrsl@senrsl-ubuntu:~/Downloads$ docker rm centos7-ssh
centos7-ssh
senrsl@senrsl-ubuntu:~/Downloads$ docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED       STATUS                   PORTS     NAMES
af4db6bd8e5b   centos:7      "/bin/bash"   2 weeks ago   Up 9 minutes                       centos7
8a18a55b7def   hello-world   "/hello"      2 weeks ago   Exited (0) 2 weeks ago             competent_ishizaka
senrsl@senrsl-ubuntu:~/Downloads$ docker images
REPOSITORY          TAG       IMAGE ID       CREATED        SIZE
local/centos7-ssh   latest    dbccb6fd0747   2 weeks ago    312MB
hello-world         latest    d1165f221234   3 weeks ago    13.3kB
centos              7         8652b9f0cb4c   4 months ago   204MB
senrsl@senrsl-ubuntu:~/Downloads$ docker rmi local/centos7-ssh
Untagged: local/centos7-ssh:latest
Deleted: sha256:dbccb6fd0747ecec107e18d34bfa10b62b496f0334ed80b6f92d157f57355387
Deleted: sha256:871e5d5d44746738e53fc7c28fc10be042ffbf5b4d888c90a1f0dbc40ed48081

senrsl@senrsl-ubuntu:~/Downloads$ docker image rm d1165f221234
Untagged: hello-world:latest
Untagged: hello-world@sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
Deleted: sha256:f22b99068db93900abe17f7f5e09ec775c2826ecfe9db961fea68293744144bd
senrsl@senrsl-ubuntu:~/Downloads$

senrsl@senrsl-ubuntu:~/Downloads$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   3 weeks ago    13.3kB
centos        7         8652b9f0cb4c   4 months ago   204MB
senrsl@senrsl-ubuntu:~/Downloads$

需要先删除 容器,再删除镜像,不然就

senrsl@senrsl-ubuntu:~/Downloads$ docker image rm d1165f221234
Error response from daemon: conflict: unable to delete d1165f221234 (must be forced) - image is being used by stopped container 8a18a55b7def
senrsl@senrsl-ubuntu:~/Downloads$

11,进入容器

虽然创建容器的时候没有给它挂载终端和bash,但是 进入的时候依然可以挂载。。。。

senrsl@senrsl-ubuntu:~$ docker run -d --name centos7-ssh7 -p 10033:22 local/centos7-ssh4:latest /usr/sbin/sshd -D
7925700221a067fa66e647dba9db4f69c484a1ecae9aefad9a791835b0d34d46
senrsl@senrsl-ubuntu:~$ docker ps
CONTAINER ID   IMAGE                       COMMAND               CREATED         STATUS              PORTS                   NAMES
7925700221a0   local/centos7-ssh4:latest   "/usr/sbin/sshd -D"   3 minutes ago   Up About a minute   0.0.0.0:10033->22/tcp   centos7-ssh7
af4db6bd8e5b   centos:7                    "/bin/bash"           3 weeks ago     Up About a minute                           centos7
senrsl@senrsl-ubuntu:~$ docker exec -it 7925700221a0 /bin/bash
[root@7925700221a0 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 25  bytes 3066 (2.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@7925700221a0 /]#

senrsl@senrsl-ubuntu:~$ telnet 172.17.0.2  22
Trying 172.17.0.2...
Connected to 172.17.0.2.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4

telnet> q
Connection closed.
senrsl@senrsl-ubuntu:~$

这样既不耽误启动时启动sshd,也不耽误进入容器,而且在容器中exit时还不会像attach那样stop容器。。。。

Thunderbird,真特么难用的让人吐血。。。。

12,容器导出,导入容器快照到镜像

senrsl@senrsl-ubuntu:~$ docker export 7925700221a0 > centos7-ssh7-export.tar
senrsl@senrsl-ubuntu:~$ ls
centos7-ssh7-export.tar
senrsl@senrsl-ubuntu:~$ cat centos7-ssh7-export.tar | docker import - local/centos-ssh7-import:v1
sha256:1bf605c4da2bce9c801b4f52a9d445128a120ac6d50c1a33176ebf2046298938
senrsl@senrsl-ubuntu:~$ docker images
REPOSITORY                 TAG       IMAGE ID       CREATED             SIZE
local/centos-ssh7-import   v1        1bf605c4da2b   50 seconds ago      290MB
local/centos7-ssh4         latest    4fbed876e3d7   About an hour ago   318MB
centos                     7         8652b9f0cb4c   4 months ago        204MB
senrsl@senrsl-ubuntu:~$

又是进进出出,太频繁了身体扛不住啊。。。。

还可以导入线上的。。。。

docker import http://192.168.6.99/centos7-ssh7-export.tar local/centos7-import  大概是这样。。。。


13,docker 运行web应用

测试这个 https://hub.docker.com/r/training/webapp

senrsl@senrsl-ubuntu:~$ docker pull training/webapp
Using default tag: latest
latest: Pulling from training/webapp
Image docker.io/training/webapp:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
10bbbc0fc0ff: Pull complete
fca59b508e9f: Pull complete
e7ae2541b15b: Pull complete
9dd97ef58ce9: Pull complete
a4c1b0cb7af7: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
docker.io/training/webapp:latest
senrsl@senrsl-ubuntu:~$ docker run -d -p 8088:5000 training/webapp python app.py
21ac726c4eb7df2e060de78172d4d578b43630f9abc57919121fb670a54ca9b4
senrsl@senrsl-ubuntu:~$ docker ps
CONTAINER ID   IMAGE                       COMMAND               CREATED          STATUS          PORTS                    NAMES
21ac726c4eb7   training/webapp             "python app.py"       2 minutes ago    Up 2 minutes    0.0.0.0:8088->5000/tcp   elegant_benz
7925700221a0   local/centos7-ssh4:latest   "/usr/sbin/sshd -D"   31 minutes ago   Up 29 minutes   0.0.0.0:10033->22/tcp    centos7-ssh7
af4db6bd8e5b   centos:7                    "/bin/bash"           3 weeks ago      Up 29 minutes                            centos7
senrsl@senrsl-ubuntu:~$

然后访问 宿主机的 http://192.168.7.89:8088/,看到了 Hello World!。。。。

-P 参数为大写时使用随机端口。。。。


14,查看容器端口映射

senrsl@senrsl-ubuntu:~$ docker port 21ac726c4eb7
5000/tcp -> 0.0.0.0:8088
senrsl@senrsl-ubuntu:~$ docker port centos7-ssh7
22/tcp -> 0.0.0.0:10033
senrsl@senrsl-ubuntu:~$

15,docker logs查看容器内部日志输出

senrsl@senrsl-ubuntu:~$ docker logs 7925700221a0
WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several problems.
WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several problems.
senrsl@senrsl-ubuntu:~$ docker logs 21ac726c4eb7
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.6.99 - - [06/Apr/2021 09:16:14] "GET / HTTP/1.1" 200 -
192.168.6.99 - - [06/Apr/2021 09:16:14] "GET /favicon.ico HTTP/1.1" 404 -
senrsl@senrsl-ubuntu:~$ docker logs -f 21ac726c4eb7
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.6.99 - - [06/Apr/2021 09:16:14] "GET / HTTP/1.1" 200 -
192.168.6.99 - - [06/Apr/2021 09:16:14] "GET /favicon.ico HTTP/1.1" 404 -
192.168.6.99 - - [06/Apr/2021 09:21:41] "GET / HTTP/1.1" 200 -
^C
senrsl@senrsl-ubuntu:~$

加了-f参数就类似于 tail -f,可以实时查看输出

16,查看容器内进程

senrsl@senrsl-ubuntu:~$ docker top centos7-ssh7
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                16419               16389               0                   16:49               ?                   00:00:00            /usr/sbin/sshd -D
senrsl@senrsl-ubuntu:~$ docker top 21ac726c4eb7
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                17363               17333               0                   17:16               ?                   00:00:00            python app.py
senrsl@senrsl-ubuntu:~$

17,docker inspect

这个功能看起来就像是一个底层状态的查看器。。。。

senrsl@senrsl-ubuntu:~$ docker inspect centos7-ssh7
[
    {
        "Id": "7925700221a067fa66e647dba9db4f69c484a1ecae9aefad9a791835b0d34d46",
        "Created": "2021-04-06T08:47:37.718361182Z",
        "Path": "/usr/sbin/sshd",
        "Args": [
            "-D"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 16419,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-04-06T08:49:05.297811029Z",
            "FinishedAt": "2021-04-06T08:48:58.659700098Z"
        },
        "Image": "sha256:4fbed876e3d727ddfbc7396d286971b67db28d943f7ca7ada9f8c9043ea767a2",
        "ResolvConfPath": "/var/lib/docker/containers/7925700221a067fa66e647dba9db4f69c484a1ecae9aefad9a791835b0d34d46/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/7925700221a067fa66e647dba9db4f69c484a1ecae9aefad9a791835b0d34d46/hostname",
        "HostsPath": "/var/lib/docker/containers/7925700221a067fa66e647dba9db4f69c484a1ecae9aefad9a791835b0d34d46/hosts",
        "LogPath": "/var/lib/docker/containers/7925700221a067fa66e647dba9db4f69c484a1ecae9aefad9a791835b0d34d46/7925700221a067fa66e647dba9db4f69c484a1ecae9aefad9a791835b0d34d46-json.log",
        "Name": "/centos7-ssh7",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "22/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "10033"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/ce12c269b161208935484bad326540476b3cf9231b645958f87687fe75fe05ed-init/diff:/var/lib/docker/overlay2/47247c7d6bf6a8765ce55e2518634f85f350105f316e7d4d6cb3a21905002c79/diff:/var/lib/docker/overlay2/7ab655513706c0ec3eacd773e884bea49746ccc17107c6e8fe96d55bae9b3382/diff",
                "MergedDir": "/var/lib/docker/overlay2/ce12c269b161208935484bad326540476b3cf9231b645958f87687fe75fe05ed/merged",
                "UpperDir": "/var/lib/docker/overlay2/ce12c269b161208935484bad326540476b3cf9231b645958f87687fe75fe05ed/diff",
                "WorkDir": "/var/lib/docker/overlay2/ce12c269b161208935484bad326540476b3cf9231b645958f87687fe75fe05ed/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "7925700221a0",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "22/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/usr/sbin/sshd",
                "-D"
            ],
            "Image": "local/centos7-ssh4:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20201113",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS",
                "org.opencontainers.image.created": "2020-11-13 00:00:00+00:00",
                "org.opencontainers.image.licenses": "GPL-2.0-only",
                "org.opencontainers.image.title": "CentOS Base Image",
                "org.opencontainers.image.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "808ebaa5dbc8cd40da1cac16d1301eb6635254df49003696448809a203ccebf6",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "22/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "10033"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/808ebaa5dbc8",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "ba4bde820bf94c5c28678cb5ffbd6e43063fb6eaa2c59b318b5fe4e8eee4ca34",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "57750f3d34e10b086af448b9a668aa62d747b970ed436ec93177a6c176a822e4",
                    "EndpointID": "ba4bde820bf94c5c28678cb5ffbd6e43063fb6eaa2c59b318b5fe4e8eee4ca34",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]
senrsl@senrsl-ubuntu:~$

18,创建镜像

18.1)使用commit

如上面对sshd的玩法

在容器中,做完修改后,docker commit -m="描述" -a "作者" 容器id  新镜像保存位置local/centos新:版本名

18.2)使用dockerfile


19,给镜像打新tag

senrsl@senrsl-ubuntu:~$ docker images
REPOSITORY                 TAG       IMAGE ID       CREATED          SIZE
local/centos7-ssh4         latest    4fbed876e3d7   2 hours ago      318MB
centos                     7         8652b9f0cb4c   4 months ago     204MB
senrsl@senrsl-ubuntu:~$ docker tag 4fbed876e3d7 local/centos7-ssh4-n
senrsl@senrsl-ubuntu:~$ docker images
REPOSITORY                 TAG       IMAGE ID       CREATED          SIZE
local/centos7-ssh4-n       latest    4fbed876e3d7   2 hours ago      318MB
local/centos7-ssh4         latest    4fbed876e3d7   2 hours ago      318MB
centos                     7         8652b9f0cb4c   4 months ago     204MB
senrsl@senrsl-ubuntu:~$

继续水 2021年04月06日17:38:34

x,pm卸载gms

franklin:/ # pm uninstall com.google.android.googlequicksearchbox
Failure [DELETE_FAILED_INTERNAL_ERROR]

franklin:/ # pm list package -s google                                                                                                                
package:com.google.android.googlequicksearchbox
package:com.google.android.webview
package:com.google.android.gms
franklin:/ # pm path com.google.android.googlequicksearchbox
package:/system/priv-app/Velvet/Velvet.apk
franklin:/ # rm /system/priv-app/V                                                                                                                    
Velvet/      VpnDialogs/
franklin:/ # rm -rf /system/priv-app/Velvet                                                                                                           
franklin:/ # pm path com.google.android.gms                                                                                                           
package:/system/priv-app/PrebuiltGmsCorePi/PrebuiltGmsCorePi.apk
franklin:/ # rm -rf /system/priv-app/Pr                                                                                                               
PrebuiltGmsCorePi/   Provision/           ProxyHandler/
franklin:/ # rm -rf /system/priv-app/PrebuiltGmsCorePi                                                                                                
franklin:/ #


--
senRsl
2021年03月12日16:43:00

没有评论 :

发表评论