docker镜像和容器的简单操作练习

目录

1.安装docker服务,配置镜像加速器

2.下载系统镜像(ubuntu和centos)

3.基于下载的镜像创建两个容器(一个为名字全拼一个为名字首字母)

4.容器启动、停止和重启

5.查看正在运行的容器和所有容器

6.两种方法退出容器

交互模式下exit

交互模式下“ctrl+d”

7.连接到运行的容器

8.查看容器或镜像的内部信息

9.如何查看所有镜像


1.安装docker服务,配置镜像加速器

​
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2   #安装必要工具
 
[root@localhost~]#yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo   #新增源
 
[root@localhost ~]# sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo  #修改源文件
 
[root@localhost ~]# yum makecache fast  #更新
 
[root@localhost ~]# yum -y install docker-ce  #安装docker-ce
 
[root@localhost ~]# yum list docker-ce.x86_64 --showduplicates | sort -r  #可以通过这行命令找到自己想要安装的具体版本,默认是安装最新版本
 
[root@localhost ~]# systemctl start docker
 
[root@localhost ~]# docker version  #查看docker版本
Client: Docker Engine - Community
 Version:           24.0.5
 API version:       1.43
 Go version:        go1.20.6
 Git commit:        ced0996
 Built:             Fri Jul 21 20:39:02 2023
 OS/Arch:           linux/amd64
 Context:           default
Server: Docker Engine - Community
 Engine:
  Version:          24.0.5
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.6
  Git commit:       a61e2b4
  Built:            Fri Jul 21 20:38:05 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.22
  GitCommit:        8165feabfdfe38c65b599c4993d227328c231fca
 runc:
  Version:          1.1.8
  GitCommit:        v1.1.8-0-g82f18fe
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0


[root@localhost ~]# cd /etc/docker/
[root@localhost docker]# ll
total 0
[root@localhost docker]# vim daemon.json
{
 
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
 
}
#使用的是中国科技大学的镜像加速器,也可以选择阿里云的
[root@localhost docker]# systemctl daemon-reload  #重载配置并重启
[root@localhost docker]# systemctl restart docker

2.下载系统镜像(ubuntu和centos)

[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
b237fe92c417: Pull complete 
Digest: sha256:ec050c32e4a6085b423d36ecd025c0d3ff00c38ab93a3d71a460ff1c44fa6d77
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

3.基于下载的镜像创建两个容器(一个为名字全拼一个为名字首字母)

[root@localhost ~]# docker run -it --name sulibao -d -P ubuntu
b7d1e332cdad2844250054c4fc5c78856139af2117464843251d04974f0ec8d9
[root@localhost ~]# docker run -it --name slb -d -P centos
d51febc7bc7cc7673e5f539d3ed535fffbd1c72aed29ac838dfc59ce99380511
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
d51febc7bc7c   centos    "/bin/bash"   27 seconds ago   Up 26 seconds             slb
b7d1e332cdad   ubuntu    "/bin/bash"   44 seconds ago   Up 43 seconds             sulibao

4.容器启动、停止和重启

[root@localhost ~]# docker stop slb 
slb
[root@localhost ~]# docker stop sulibao
sulibao
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker restart slb
slb
[root@localhost ~]# docker restart sulibao
sulibao
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS         PORTS     NAMES
d51febc7bc7c   centos    "/bin/bash"   About a minute ago   Up 6 seconds             slb
b7d1e332cdad   ubuntu    "/bin/bash"   About a minute ago   Up 2 seconds             sulibao

5.查看正在运行的容器和所有容器

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
d51febc7bc7c   centos    "/bin/bash"   6 minutes ago   Up 5 minutes             slb
b7d1e332cdad   ubuntu    "/bin/bash"   6 minutes ago   Up 5 minutes             sulibao

6.两种方法退出容器

交互模式下exit

[root@localhost ~]# docker exec -it slb /bin/sh
sh-4.4# ls 
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
sh-4.4# exit
exit
[root@localhost ~]# 

交互模式下“ctrl+d”

[root@localhost ~]# docker exec -it sulibao /bin/sh
# ls
bin   dev  home  lib32	libx32	mnt  proc  run	 srv  tmp  var
boot  etc  lib	 lib64	media	opt  root  sbin  sys  usr
# 
[root@localhost ~]# 

7.连接到运行的容器

[root@localhost ~]# docker exec -it sulibao /bin/sh
# ls
bin   dev  home  lib32	libx32	mnt  proc  run	 srv  tmp  var
boot  etc  lib	 lib64	media	opt  root  sbin  sys  usr

8.查看容器或镜像的内部信息

[root@localhost ~]# docker inspect slb
[root@localhost ~]# docker inspect ubuntu:latest

9.如何查看所有镜像

[root@localhost ~]# docker images -a
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
ubuntu       latest    01f29b872827   2 weeks ago     77.8MB
centos       latest    5d0da3dc9764   23 months ago   231MB

你可能感兴趣的:(Linux,#,docker,docker,容器,运维,docker镜像,Linux)