Centos下的纯命令记录Docker学习(2)-配置阿里云镜像加速和开启Docker远程访问(生产环境谨慎开启,不然可能被用于挖矿)

原因:

龟速和加速的区别。

步骤

1)登入阿里云镜像服务中心(需使用账号登入)
地址:https://cr.console.aliyun.com/cn-shenzhen/new
2)获取地址:

image.png

3)按照要求修改OS的Docker配置文件
image.png

在/etc/docker/下查找daemon.json文件,无则创建。并写入以下的内容:


[root@localhost ~]# nano /etc/docker/daemon.json
[root@localhost ~]#

写入的内容:

{
  "registry-mirrors": ["https://xxxxxxxx.mirror.aliyuncs.com"]
}

4 ) 重启daemon

 [root@localhost ~]#systemctl daemon-reload

5 ) 重启docker

[root@localhost ~]# systemctl restart docker

6 ) 查看修改的后docker信息

[root@localhost ~]# docker info
Client:
 Debug Mode: false

Server:
 Containers: 2
  Running: 0
  Paused: 0
  Stopped: 2
 Images: 1
 Server Version: 19.03.13
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 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: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 8fba4e9a7d01810a393d5d25a3621dc101981175
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-514.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 976.5MiB
 Name: localhost.localdomain
 ID: TT6R:HUR2:3AQG:JLBU:CGAO:BG5T:LMC4:FYTX:PRGV:4D56:4TAN:BGFE
 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
 Registry Mirrors:
  https://aiyf7r3a.mirror.aliyuncs.com/
 Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
[root@localhost ~]#

7 ) 测试拉取一个ubuntu:15.10的镜像

[root@localhost ~]# docker pull ubuntu:15.10
15.10: Pulling from library/ubuntu
7dcf5a444392: Pull complete
759aa75f3cee: Pull complete
3fa871dc8a2b: Pull complete
224c42ae46e7: Pull complete
Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3
Status: Downloaded newer image for ubuntu:15.10
docker.io/library/ubuntu:15.10
[root@localhost ~]#

附加其他的对镜像的操作测试处理:

8) 查看本地的镜像


[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        9 months ago        13.3kB
ubuntu              15.10               9b9cb95443b5        4 years ago         137MB
[root@localhost ~]#

9)使用镜像跑一个示例 前台模式的启动

说明:/bin/echo "Hello world": 在启动的容器里执行的命令

[root@localhost ~]# docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world

其他一些容器的基本的操作:
root@0a22083fa318:~# cd ll
bash: cd: ll: No such file or directory
root@0a22083fa318:~# cd ~
root@0a22083fa318:~# ll
total 8
drwx------. 2 root root   37 Jul  6  2016 ./
drwxr-xr-x. 1 root root    6 Oct 19 07:57 ../
-rw-r--r--. 1 root root 3106 Feb 20  2014 .bashrc
-rw-r--r--. 1 root root  140 Feb 20  2014 .profile
root@0a22083fa318:~# cd ..
root@0a22083fa318:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@0a22083fa318:/# eixt
bash: eixt: command not found
root@0a22083fa318:/# exit
exit
[root@localhost ~]# docker run -i -t ubuntu:15.10 /bin/bash
root@a2cc1c83603a:/# exit

10)前台模式的启动 查看的相关的容器情况


[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                        PORTS               NAMES
a2cc1c83603a        ubuntu:15.10        "/bin/bash"              26 seconds ago       Exited (0) 24 seconds ago                         elated_cartwright
0a22083fa318        ubuntu:15.10        "/bin/bash"              About a minute ago   Exited (127) 29 seconds ago                       epic_galileo
e0069b97f043        ubuntu:15.10        "/bin/echo 'Hello wo…"   3 minutes ago        Exited (0) 3 minutes ago                          intelligent_lamport
209fc9dd3324        hello-world         "/hello"                 56 minutes ago       Exited (0) 56 minutes ago                         flamboyant_knuth
d6087f654d9e        hello-world         "/hello"                 2 hours ago          Exited (0) 2 hours ago                            practical_wozniak

11)后台的模式启动的容器实例

[root@localhost ~]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
f1b40b91bb48a2cc832412b9e71860a44cddcd4fa83a82db7b64fcc9c68ad82f
[root@localhost ~]#

12)再次查看容器的情况:


[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
f1b40b91bb48        ubuntu:15.10        "/bin/sh -c 'while t…"   3 minutes ago       Up 3 minutes                            great_tereshkova
[root@localhost ~]#

13)停止容器:

[root@localhost ~]# docker stop f1b40b91bb48
f1b40b91bb48
[root@localhost ~]#

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost ~]#

开启Docker远程访问

Docker远程访问可以用于手续的,直接使用Pycharm等工具连接的到我们的Docker,然后直接的使用工具进行镜像的制作!

PS:该方法千万不要在生产环境,否则100%被挖矿!!!!!!!!!!!!!

步骤

  • 1)编辑docker的宿主机文件/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process

[Install]
WantedBy=multi-user.target


  • 2)修改内容:
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
  • 3)更新修改
[root@localhost web_statistics]# systemctl daemon-reload
    1. 重启docker服务
[root@localhost web_statistics]# systemctl restart docker
    1. 测试访问情况
[root@localhost web_statistics]# curl http://localhost:2375/version
{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"19.03.13","Details":{"ApiVersion":"1.40","Arch":"amd64","BuildTime":"2020-09-16T17:02:21.000000000+00:00","Experimental":"false","GitCommit":"4484c46d9d","GoVersion":"go1.13.15","KernelVersion":"3.10.0-1127.19.1.el7.x86_64","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.3.7","Details":{"GitCommit":"8fba4e9a7d01810a393d5d25a3621dc101981175"}},{"Name":"runc","Version":"1.0.0-rc10","Details":{"GitCommit":"dc9208a3303feef5b3839f4323d9beb36df0a9dd"}},{"Name":"docker-init","Version":"0.18.0","Details":{"GitCommit":"fec3683"}}],"Version":"19.03.13","ApiVersion":"1.40","MinAPIVersion":"1.12","GitCommit":"4484c46d9d","GoVersion":"go1.13.15","Os":"linux","Arch":"amd64","KernelVersion":"3.10.0-1127.19.1.el7.x86_64","BuildTime":"2020-09-16T17:02:21.000000000+00:00"}
[root@localhost web_statistics]#

  • 6)外网访问


    image.png

你可能感兴趣的:(Centos下的纯命令记录Docker学习(2)-配置阿里云镜像加速和开启Docker远程访问(生产环境谨慎开启,不然可能被用于挖矿))