Docker镜像制作及使用

一、准备

1.1 确保网络可用

[root@chpdirector84 ~]# ping baidu.com

[root@chpdirector84 ~]# ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=8.81 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=8.71 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=8.25 ms
^C
--- baidu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2008ms
rtt min/avg/max/mdev = 8.259/8.596/8.817/0.253 ms

1.2 防火墙已关闭

[root@chpdirector84 ~]# systemctl status firewalld

[root@chpdirector84 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

[root@chpdirector84 ~]# cat /etc/sysconfig/selinux

[root@chpdirector84 ~]# cat /etc/sysconfig/selinux 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Lev

二、服务器1:192.168.0.200

2.1 安装DOCKER

2.1.1 使用官方安装脚本自动安装

[root@chpdirector84 ~]# curl -fsSL https://get.docker.com | bash -s docker –mirror Aliyun

[root@chpdirector84 ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
# Executing docker install script, commit: 7cae5f8b0decc17d6571f9f52eb840fbc13b2737
+ sh -c 'yum install -y -q yum-utils'
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.

…………………………………………..
WARNING: Access to the remote API on a privileged Docker daemon is equivalent
         to root access on the host. Refer to the 'Docker daemon attack surface'
         documentation for details: https://docs.docker.com/go/attack-surface/

================================================================================

查看安装版本:[root@chpdirector84 ~]# docker version

[root@chpdirector84 ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.6
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        370c289
 Built:             Fri Apr  9 22:45:33 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

2.1.2 启动、停止、自启动、查看状态

查询状态:systemctl status docker
启动:systemctl start docker
停止:systemctl stop docker
重启:systemctl restart docker
开机启动:systemctl enable docker

[root@chpdirector84 ~]# systemctl status docker

[root@chpdirector84 ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: https://docs.docker.com

[root@chpdirector84 ~]# systemctl start docker

[root@chpdirector84 ~]# systemctl enable docker

[root@chpdirector84 ~]# systemctl start docker
[root@chpdirector84 ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@chpdirector84 ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-05-19 14:08:01 CST; 9s ago
     Docs: https://docs.docker.com
 Main PID: 2537 (dockerd)
   CGroup: /system.slice/docker.service
           └─2537 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
……………………

2.2 镜像

2.2.1 Pull下载CENTOS镜像

访问 CentOS 镜像库地址:https://www.runoob.com/docker/docker-install-centos.html

选择需要安装的版本,此处选择centos7.2.1511

使用pull命令进行安装:

[root@chpdirector84 ~]# docker pull centos:centos7.2.1511

[root@chpdirector84 ~]# docker pull centos:centos7.2.1511
centos7.2.1511: Pulling from library/centos
a8c7037c15e9: Pull complete 
Digest: sha256:50cca1e74da4b6a4eb4ade029c8fdd4ee8564776801914d9bd89df8c6344add0
Status: Downloaded newer image for centos:centos7.2.1511
docker.io/library/centos:centos7.2.1511

查看镜像:[root@chpdirector84 ~]# docker images

[root@chpdirector84 ~]# docker images
REPOSITORY   TAG              IMAGE ID       CREATED       SIZE
centos       centos7.2.1511   9aec5c5fe4ba   2 years ago   195MB

2.2.2 重命名镜像

新建一个名字:[root@chpdirector84 ~]# docker tag 9aec5c5fe4ba centos72:latest

[root@chpdirector84 ~]# docker images

删除之前的:[root@chpdirector84 ~]# docker rmi centos:centos7.2.1511

[root@chpdirector84 ~]# docker tag 9aec5c5fe4ba centos72:latest
[root@chpdirector84 ~]# docker images
REPOSITORY   TAG              IMAGE ID       CREATED       SIZE
centos72     latest           9aec5c5fe4ba   2 years ago   195MB
centos       centos7.2.1511   9aec5c5fe4ba   2 years ago   195MB
[root@chpdirector84 ~]# docker rmi centos:centos7.2.1511
Untagged: centos:centos7.2.1511
Untagged: centos@sha256:50cca1e74da4b6a4eb4ade029c8fdd4ee8564776801914d9bd89df8c6344add0
[root@chpdirector84 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos72     latest    9aec5c5fe4ba   2 years ago   195MB

2.3 容器

2.3.1 创建并运行

2.3.1.1 方式1:支持systemctl

支持端口转换,重名名

docker run -itd -p 5888:5888 -p 5822:22  -p 3306:3306 -p 21:21  -p 18083:18083 –privileged –name centos72-ai centos72 /usr/sbin/init

[root@chpdirector84 ~]# docker run -itd -p 5888:5888 -p 5822:22  -p 3306:3306 -p 21:21  -p 18083:18083 --privileged --name centos72-ai centos72 /usr/sbin/init
39e5378a4e384484ee30f6ceb500a13e24a23cbbd3e4f96936a87e228f957014

2.3.1.2 方式2

docker run -i -t –name centos72-run centos72 /bin/bash

2.3.2 查看容器

[root@chpdirector84 ~]# docker ps -a

[root@chpdirector84 ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND            CREATED          STATUS         PORTS                                                                                                                                                                                                           NAMES
39e5378a4e38   centos72   "/usr/sbin/init"   11 seconds ago   Up 9 seconds   0.0.0.0:21->21/tcp, :::21->21/tcp, 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 0.0.0.0:5888->5888/tcp, :::5888->5888/tcp, 0.0.0.0:18083->18083/tcp, :::18083->18083/tcp, 0.0.0.0:5822->22/tcp, :::5822->22/tcp   centos72-ai

2.3.3 停止容器

[root@chpdirector84 ~]# docker stop 39e5378a4e38

[root@chpdirector84 ~]# docker ps -a.

[root@chpdirector84 ~]# docker stop 39e5378a4e38
39e5378a4e38
[root@chpdirector84 ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND            CREATED              STATUS                      PORTS     NAMES
39e5378a4e38   centos72   "/usr/sbin/init"   About a minute ago   Exited (137) 1 second ago             centos72-ai

2.3.4 启动容器

[root@chpdirector84 ~]# docker start 39e5378a4e38

[root@chpdirector84 ~]# docker start 39e5378a4e38
39e5378a4e38
[root@chpdirector84 ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND            CREATED         STATUS              PORTS                                                                                                                                                                                                           NAMES
39e5378a4e38   centos72   "/usr/sbin/init"   2 minutes ago   Up About a minute   0.0.0.0:21->21/tcp, :::21->21/tcp, 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 0.0.0.0:5888->5888/tcp, :::5888->5888/tcp, 0.0.0.0:18083->18083/tcp, :::18083->18083/tcp, 0.0.0.0:5822->22/tcp, :::5822->22/tcp   centos72-ai

2.3.5 重启容器

docker restart 39e5378a4e38  

2.3.6 删除容器

[root@chpdirector84 ~]# docker stop 39e5378a4e38

[root@chpdirector84 ~]# docker rm 39e5378a4e38

[root@chpdirector84 ~]# docker ps -a

[root@chpdirector84 ~]# docker stop 39e5378a4e38
39e5378a4e38
[root@chpdirector84 ~]# docker rm 39e5378a4e38
39e5378a4e38
[root@chpdirector84 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

2.3.7 进入容器

进入容器之前,先得启动:docker start 容器ID

2.3.7.2 方式1:systemctl

[root@chpdirector84 ~]# docker exec -it 60a392cc804e bash

[root@60a392cc804e /]#

2.3.7.1 方式2

进入:docker attach 容器ID

或者:docker start -a -t 容器名/ID(相当于start后attach两步)

2.4 安装服务

2.4.1 设置root密码

[root@60a392cc804e /]# passwd root       

[root@60a392cc804e /]# passwd root          
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

2.4.2 SSHD服务

2.4.2.1 安装

[root@60a392cc804e /]# yum install openssh-server

[root@60a392cc804e /]# yum install openssh-server
Loaded plugins: fastestmirror
……………………………..                                                                                                                                                                      

Dependency Installed:
  fipscheck.x86_64 0:1.4.1-6.el7                   fipscheck-lib.x86_64 0:1.4.1-6.el7                   openssh.x86_64 0:7.4p1-21.el7                   tcp_wrappers-libs.x86_64 0:7.6-77.el7                  

Dependency Updated:
  openssl-libs.x86_64 1:1.0.2k-21.el7_9                                                                                                                                                                        

Complete!

2.4.2.2 启动、自启动

systemctl start httpd

systemctl enable httpd

2.4.2.3 SSH连接容器:5822端口

2.4.3 HTTPD服务

安装,启动,自启动

修改监听端口:[root@60a392cc804e /]# vi /etc/httpd/conf/httpd.conf

测试服务:http://192.168.0.200:5888/

2.5 制作保存镜像

2.5.1 制作

[root@chpdirector84 ~]# docker commit 60a392cc804e ai_bk_20210519

[root@chpdirector84 ~]# docker commit 60a392cc804e ai_bk_20210519
sha256:6dab60b98f9c88355b2b586d1a8681c97f8a31cfeefae14355e9eace4d027965
[root@chpdirector84 ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
ai_bk_20210519   latest    6dab60b98f9c   11 seconds ago   348MB
centos72         latest    9aec5c5fe4ba   2 years ago      195MB

2.5.2 保存

[root@chpdirector84 ~]# docker save -o ai_bk_img ai_bk_20210519

[root@chpdirector84 ~]# docker save -o ai_bk_img ai_bk_20210519
[root@chpdirector84 ~]# ll
total 347360
-rw-------. 1 root root 355691008 May 19 15:03 ai_bk_img
-rw-------. 1 root root      1664 Dec 25 15:31 anaconda-ks.cfg
[root@chpdirector84 ~]#

下载下来后,关闭服务器

三、服务器2:192.168.0.201

3.1 安装、启动DOCKER

[root@chpdirector84 ~]# curl -fsSL https://get.docker.com | bash -s docker –mirror Aliyun

[root@chpdirector84 ~]# systemctl start docker

3.2 上传并导入镜像文件

[root@chpdirector84 ~]# docker load -i ai_bk_img

[root@chpdirector84 ~]# ll
total 347360
-rw-r--r--. 1 root root 355691008 May 19 15:08 ai_bk_img
-rw-------. 1 root root      1664 Dec 25 15:31 anaconda-ks.cfg
[root@chpdirector84 ~]# docker load -i ai_bk_img
a11c91bfd866: Loading layer [==================================================>]    202MB/202MB
26438ad96862: Loading layer [==================================================>]  153.7MB/153.7MB
Loaded image: ai_bk_20210519:latest
[root@chpdirector84 ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
ai_bk_20210519   latest    6dab60b98f9c   10 minutes ago   348MB
[root@chpdirector84 ~]#

3.3 创建并启动容器

[root@chpdirector84 ~]# docker run -itd -p 5888:5888 -p 5822:22  -p 3306:3306 -p 21:21  -p 18083:18083 –privileged –name ai_bk ai_bk_20210519 /usr/sbin/init

[root@chpdirector84 ~]# docker run -itd -p 5888:5888 -p 5822:22  -p 3306:3306 -p 21:21  -p 18083:18083 --privileged --name ai_bk ai_bk_20210519 /usr/sbin/init
d25f6c2a8b5c9f0aae8d3f58529331befd54ed4e8f6ad98043dccffcf965ace0
[root@chpdirector84 ~]#

3.4 试验

http://192.168.0.201:5888/

发表回复