docker基础操作

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

1)添加docker-ce 源信息

[root@master ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo

2)修改docker-ce 源

[root@master ~]# sed -i '[email protected]@mirrors.tuna.tsinghua.edu.cn/docker-ce@g' /etc/yum.repos.d/docker-ce.repo

3)更新并安装 Docker-CE

[root@master ~]# yum makecache fast
[root@master ~]# yum -y install docker-ce 
#安装的是默认最新版本

4)配置阿里云镜像加速器

2.下载系统镜像(Ubuntu、 centos)

[root@master ~]# docker pull ubuntu
[root@master ~]# docker pull centos

[root@master ~]# docker images
REPOSITORY   TAG           IMAGE ID       CREATED         SIZE
ubuntu       latest        ba6acccedd29   22 months ago   72.8MB
centos       latest        5d0da3dc9764   23 months ago   231MB
nginx        1.14-alpine   8a2fb25a19f5   4 years ago     16MB

3.基于下载的镜像创建两个容器

[root@master ~]# docker run --name test2 -dit centos:latest /bin/bash
57c15dff4ff8c902e28076789c7ec660e1456c5600a62b5b9e9645cfdbda73e7

[root@master ~]# docker run --name test1 -dit ubuntu:latest /bin/bash
639c42c0d2dbb381d2c6b50bd4a25837bf1d8617d34267e53279caf384c79191

4.容器的启动、 停止及重启操作

#启动
[root@master ~]# docker start test1
test1
[root@master ~]# docker start test2
test2

#停止
[root@master ~]# docker stop test1
test1
[root@master ~]# docker stop test2
test2

#重启
[root@master ~]# docker restart test1
test1
[root@master ~]# docker restart test2
test2

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

#正在运行
[root@master ~]# docker ps
CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS          PORTS     NAMES
639c42c0d2db   ubuntu:latest   "/bin/bash"   24 seconds ago   Up 23 seconds             test1
57c15dff4ff8   centos:latest   "/bin/bash"   38 seconds ago   Up 37 seconds             test2

#所有容器
[root@master ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS          PORTS     NAMES
639c42c0d2db   ubuntu:latest   "/bin/bash"   31 seconds ago   Up 31 seconds             test1
57c15dff4ff8   centos:latest   "/bin/bash"   45 seconds ago   Up 44 seconds             test2

6.怎么退出容器: 两种方法分别实现?

root@a992aedce7c0:/# exit

或

Ctrl + D

7.怎么连接到运行的容器?

[root@master ~]# docker exec -it test1 /bin/bash
root@639c42c0d2db:/# exit
exit
[root@master ~]# docker exec -it test2 /bin/bash
[root@57c15dff4ff8 /]# 

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

#查看容器内部信息
[root@master ~]# docker inspect test1
[
    {
        "Id": "639c42c0d2dbb381d2c6b50bd4a25837bf1d8617d34267e53279caf384c79191",
        "Created": "2023-08-19T03:42:48.361213643Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 8118,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-08-19T03:42:48.527774783Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1",
        "ResolvConfPath": "/var/lib/docker/containers/639c42c0d2dbb381d2c6b50bd4a25837bf1d8617d34267e53279caf384c79191/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/639c42c0d2dbb381d2c6b50bd4a25837bf1d8617d34267e53279caf384c79191/hostname",
        "HostsPath": "/var/lib/docker/containers/639c42c0d2dbb381d2c6b50bd4a25837bf1d8617d34267e53279caf384c79191/hosts",
        "LogPath": "/var/lib/docker/containers/639c42c0d2dbb381d2c6b50bd4a25837bf1d8617d34267e53279caf384c79191/639c42c0d2dbb381d2c6b50bd4a25837bf1d8617d34267e53279caf384c79191-json.log",
#查看镜像内部信息
[root@master ~]# docker inspect centos:latest 
[
    {
        "Id": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "RepoTags": [
            "centos:latest"
        ],
        "RepoDigests": [
            "centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-09-15T18:20:05.184694267Z",
        "Container": "9bf8a9e2ddff4c0d76a587c40239679f29c863a967f23abf7a5babb6c2121bf1",
        "ContainerConfig": {
            "Hostname": "9bf8a9e2ddff",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],

9.如何查看所有镜像?

[root@master ~]# docker images
REPOSITORY   TAG           IMAGE ID       CREATED         SIZE
ubuntu       latest        ba6acccedd29   22 months ago   72.8MB
centos       latest        5d0da3dc9764   23 months ago   231MB
nginx        1.14-alpine   8a2fb25a19f5   4 years ago     16MB

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