安装docker和centos 7镜像记录

文章详细介绍了如何在CentOS上安装Docker,包括添加阿里云镜像源、安装、启动和设置自动启动。接着,作者分享了如何创建和管理容器,如基于centos镜像创建容器,映射端口,并提到了容器提交为镜像以及镜像的导出与导入。此外,还提到了一些实用技巧,如修改端口映射和目录映射的方法。
摘要由CSDN通过智能技术生成

最近项目要求用容器部署,所以需要将docker的用法搞清楚,在操作过程中,积累了一些操作方法和技巧,作为笔记,为后面使用做个参考。
首先安装docker需要给centos增加源(参考https://www.runoob.com/docker/centos-docker-install.html):
sudo yum-config-manager
–add-repo
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安装:
yum install -y docker-ce
启动docker
systemctl start docker
设置自动启动
systemctl enable docker

注意:docker也有通用的二进制文件可以下载(https://download.docker.com/linux/static/stable/)后拷贝到linux的/usr/sbin中即可使用(注意必须是/usr/sbin,因为运行时调用程序用了绝对路径)。不过此种方式需要自己手动编写docker.service文件并加入到systemctl系统中让其自动运行。

设置安装源

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": ["https://akchsmlh.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

使用docker部署项目时,必须基于一个基础镜像,我比较熟悉centos,所以首先安装centos7的基础镜像:
docker pull centos:7

看已安装镜像
docker images
基于镜像创建一个容器并运行,映射sshd端口号

基于镜像创建一个容器并运行,映射sshd端口号
docker run -itd --name centos7 -v /web:/web -p 222:22 -p280:80 --privileged centos:7
/usr/sbin/init
进入容器,然后剩余操作就跟普通的linux操作一致了。
docker exec -it mycentos bash
进入容器后,安装所需服务软件,并部署好文件。如果要将容器提交为镜像,然后导出给其他地方部署用,建议先停止所有服务,将挂载的目录中的文件移除出去留下一个空目录再提交、导出镜像,这样镜像中的/web目录就是个空目录,方便导入镜像后创建容器去挂载外面的web目录(web目录随镜像一起发布)。

停止容器
dockert stop mycentos

更名容器
docker rename mycentos centos7

容器自动运行
docker update --restart=always centos7

容器提交为镜像
docker commit centos7 mycentos:7

导出镜像
docker save -o centos7.tar mycentos:7

压缩镜像
gzip centos7.tar

二合一,导出并压缩镜像
save mycentos:7 | gzip > mycentos.tar.gz

解压镜像
gunzip centos7.tar.gz

导入镜像
docker load -i centos7.tar

删除镜像
docker rmi 镜像ID或名称(必须先删除此镜像创建的容器)
删除容器
docker rm 容器ID或名称(必须先停止容器)

使用技巧:在docker中运行的centos7也支持rc.local,不过/etc/profile不会在进入shell交互时自动执行,需要在/etc/profile.d/xxx.sh中设置自己需要部署的环境变量。

如果容器已经运行了,想要修改端口映射、目录映射,需要停止容器、停止dockder服务,然后修改config.v2.json和hostconfig.json。建议将两个json下载下来格式化json(编辑器或网上有在线格式化工具)再编辑,编辑后上传覆盖,再启动docker服务,再运行docker容器(自动运行的不必)。
我这里贴出来我用到的配置,供参考:

{
    "StreamConfig": {},
    "State": {
        "Running": false,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "RemovalInProgress": false,
        "Dead": false,
        "Pid": 0,
        "ExitCode": 137,
        "Error": "",
        "StartedAt": "2023-06-01T00:36:48.859893235Z",
        "FinishedAt": "2023-06-01T01:35:25.176121314Z",
        "Health": null
    },
    "ID": "eb8b8ee5ebdc83423d31a12e3c8ce2c4821515f7b90c698644ba29532bed2b9d",
    "Created": "2023-06-01T00:36:48.066835963Z",
    "Managed": false,
    "Path": "/usr/sbin/init",
    "Args": [],
    "Config": {
        "Hostname": "eb8b8ee5ebdc",
        "Domainname": "",
        "User": "",
        "AttachStdin": false,
        "AttachStdout": false,
        "AttachStderr": false,
        "ExposedPorts": {
            "22/tcp": {},
            "3306/tcp": {},
            "443/tcp": {},
            "6379/tcp": {},
            "6388/tcp": {},
            "80/tcp": {}
        },
        "Tty": true,
        "OpenStdin": true,
        "StdinOnce": false,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "Cmd": [
            "/usr/sbin/init"
        ],
        "Image": "centos:7",
        "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"
        }
    },
    "Image": "sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9",
    "ImageManifest": null,
    "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "1abe44a740831ab353f3090c6405fdf40cba5edba5be653044e7dfc7e380f420",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Networks": {
            "bridge": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": null,
                "NetworkID": "bd10cbaf7d62ec6a785d70623eebb39d372e380e24af593bd4ae5079a67763ab",
                "EndpointID": "",
                "Gateway": "",
                "IPAddress": "",
                "IPPrefixLen": 0,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "",
                "DriverOpts": null,
                "IPAMOperational": false
            }
        },
        "Service": null,
        "Ports": null,
        "SandboxKey": "/var/run/docker/netns/1abe44a74083",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "IsAnonymousEndpoint": false,
        "HasSwarmEndpoint": false
    },
    "LogPath": "/var/lib/docker/containers/eb8b8ee5ebdc83423d31a12e3c8ce2c4821515f7b90c698644ba29532bed2b9d/eb8b8ee5ebdc83423d31a12e3c8ce2c4821515f7b90c698644ba29532bed2b9d-json.log",
    "Name": "/centos7",
    "Driver": "overlay2",
    "OS": "linux",
    "RestartCount": 0,
    "HasBeenStartedBefore": true,
    "HasBeenManuallyStopped": true,
    "MountPoints": {
        "/ftp": {
            "Source": "/ftp",
            "Destination": "/ftp",
            "RW": true,
            "Name": "",
            "Driver": "",
            "Type": "bind",
            "Propagation": "rprivate",
            "Spec": {
                "Type": "bind",
                "Source": "/ftp",
                "Target": "/ftp"
            },
            "SkipMountpointCreation": false
        }
    },
    "SecretReferences": null,
    "ConfigReferences": null,
    "MountLabel": "",
    "ProcessLabel": "",
    "AppArmorProfile": "unconfined",
    "SeccompProfile": "",
    "NoNewPrivileges": false,
    "HostnamePath": "/var/lib/docker/containers/eb8b8ee5ebdc83423d31a12e3c8ce2c4821515f7b90c698644ba29532bed2b9d/hostname",
    "HostsPath": "/var/lib/docker/containers/eb8b8ee5ebdc83423d31a12e3c8ce2c4821515f7b90c698644ba29532bed2b9d/hosts",
    "ShmPath": "",
    "ResolvConfPath": "/var/lib/docker/containers/eb8b8ee5ebdc83423d31a12e3c8ce2c4821515f7b90c698644ba29532bed2b9d/resolv.conf",
    "LocalLogCacheMeta": {
        "HaveNotifyEnabled": false
    }
}
{
    "Binds": [
        "/ftp:/ftp"
    ],
    "ContainerIDFile": "",
    "LogConfig": {
        "Type": "json-file",
        "Config": {}
    },
    "NetworkMode": "default",
    "PortBindings": {
        "22/tcp": [
            {
                "HostIp": "",
                "HostPort": "222"
            }
        ],
        "3306/tcp": [
            {
                "HostIp": "",
                "HostPort": "3306"
            }
        ],
        "443/tcp": [
            {
                "HostIp": "",
                "HostPort": "443"
            }
        ],
        "6379/tcp": [
            {
                "HostIp": "",
                "HostPort": "6379"
            }
        ],
        "6388/tcp": [
            {
                "HostIp": "",
                "HostPort": "6388"
            }
        ],
        "80/tcp": [
            {
                "HostIp": "",
                "HostPort": "80"
            }
        ]
    },
    "RestartPolicy": {
        "Name": "always",
        "MaximumRetryCount": 0
    },
    "AutoRemove": false,
    "VolumeDriver": "",
    "VolumesFrom": null,
    "ConsoleSize": [
        43,
        155
    ],
    "CapAdd": null,
    "CapDrop": null,
    "CgroupnsMode": "host",
    "Dns": [],
    "DnsOptions": [],
    "DnsSearch": [],
    "ExtraHosts": null,
    "GroupAdd": null,
    "IpcMode": "private",
    "Cgroup": "",
    "Links": null,
    "OomScoreAdj": 0,
    "PidMode": "",
    "Privileged": true,
    "PublishAllPorts": false,
    "ReadonlyRootfs": false,
    "SecurityOpt": [
        "label=disable"
    ],
    "UTSMode": "",
    "UsernsMode": "",
    "ShmSize": 67108864,
    "Runtime": "runc",
    "Isolation": "",
    "CpuShares": 0,
    "Memory": 0,
    "NanoCpus": 0,
    "CgroupParent": "",
    "BlkioWeight": 0,
    "BlkioWeightDevice": [],
    "BlkioDeviceReadBps": [],
    "BlkioDeviceWriteBps": [],
    "BlkioDeviceReadIOps": [],
    "BlkioDeviceWriteIOps": [],
    "CpuPeriod": 0,
    "CpuQuota": 0,
    "CpuRealtimePeriod": 0,
    "CpuRealtimeRuntime": 0,
    "CpusetCpus": "",
    "CpusetMems": "",
    "Devices": [],
    "DeviceCgroupRules": null,
    "DeviceRequests": null,
    "MemoryReservation": 0,
    "MemorySwap": 0,
    "MemorySwappiness": null,
    "OomKillDisable": false,
    "PidsLimit": null,
    "Ulimits": null,
    "CpuCount": 0,
    "CpuPercent": 0,
    "IOMaximumIOps": 0,
    "IOMaximumBandwidth": 0,
    "MaskedPaths": null,
    "ReadonlyPaths": null
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值