【自动化部署学习】【离线】docker安装

离线docker及docker基础命令

一、安装docker:

下载地址:https://download.docker.com/linux/static/stable/x86_64/

选择合适的版本:本次选择( docker-19.03.10 )

上传并解压文件:

[root@docker home]# tar -zxvf docker-19.03.10.tgz 
docker/
docker/ctr
docker/docker
docker/containerd
docker/docker-proxy
docker/runc
docker/docker-init
docker/containerd-shim
docker/dockerd

解压的docker文件夹全部移动至/usr/bin目录

cp -p docker/*  /usr/bin

将docker注册为系统服务

在/usr/lib/systemd/system/目录下,创建docker.service文件

vi /usr/lib/systemd/system/docker.service

复制内容到docker.service中,并指定docker的工作地址

[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target docker.socket
[Service]
Type=notify
EnvironmentFile=-/run/flannel/docker
WorkingDirectory=/usr/local/bin
ExecStart=/usr/bin/dockerd \
                -H tcp://0.0.0.0:4243 \
                -H unix:///var/run/docker.sock \
                --selinux-enabled=false \
                --log-opt max-size=1g \
                --graph=/data/docker_data
#【重点】通过--graph=/data/docker_data 指定docker的工作地址
ExecReload=/bin/kill -s HUP $MAINPID
#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
#Uncomment TasksMax if your systemd version supports it.
#Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
#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
Restart=on-failure
[Install]
WantedBy=multi-user.target

重新加载配置文件

systemctl daemon-reload

启动docker

systemctl start docker

查看docker启动状态

systemctl status docker

查看docker版本

docker version

[root@docker software]# docker version
Client: Docker Engine - Community
 Version:           19.03.10
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        9424aea
 Built:             Thu May 28 22:11:50 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.10
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       9424aea
  Built:            Thu May 28 22:18:15 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

查看docker信息,确定工作路径生效

docker info

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

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 19.03.10
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
 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: 7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-957.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 16
 Total Memory: 7.743GiB
 Name: docker
 ID: 2HBM:PGND:ATDR:SABK:XWLW:7FAW:NH5J:LVPQ:MFNQ:PCEK:EDIZ:B7FK
 Docker Root Dir: /data/docker_data    #【这里是docker的工作地址】
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine

WARNING: API is accessible on http://0.0.0.0:4243 without encryption.
         Access to the remote API is equivalent to root access on the host. Refer
         to the 'Docker daemon attack surface' section in the documentation for
         more information: https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface

设置docker开机自启

systemctl enable docker

二、docker基础命令

1、搜索镜像 (OFFICIAL:代表官方发布)

docker search [镜像名称]

#例如:
[root@centos7 conf]# docker ps
NAME                                DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
redis                               Redis is an open source key-value store that…   12285     [OK]       
redislabs/redisearch                Redis With the RedisSearch module pre-loaded…   57                   
redislabs/redisinsight              RedisInsight - The GUI for Redis                 91                   
redislabs/rebloom                   A probablistic datatypes module for Redis        24                   [OK]
redislabs/redis                     Clustered in-memory database engine compatib…   38                   
redis/redis-stack-server            redis-stack-server installs a Redis server w…   54                   
redislabs/rejson                    RedisJSON - Enhanced JSON data type processi…   53                   
redis/redis-stack                   redis-stack installs a Redis server with add…   63                   
redislabs/redisgraph                A graph database module for Redis                26                   [OK]
redislabs/redismod                  An automated build of redismod - latest Redi…   41                   [OK]
redislabs/redistimeseries           A time series database module for Redis          12                   
redislabs/operator                                                                   7                    
redislabs/operator-internal         This repository contains pre-released versio…   1                    
redislabs/redis-py                                                                   5                    

2、拉取镜像

docker pull [镜像名称]:[镜像版本]

3、查看镜像

docker images
docker images -a

#例如:
[root@centos7 conf]# docker images -a
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
redis        latest    7614ae9453d1   20 months ago   113MB
mongo        latest    dfda7a2cf273   20 months ago   693MB

查看镜像详细信息

详细信息中包含了镜像的版本,下载地址,配置路径等信息。

docker inspect [镜像ID]

4、删除镜像

docker rmi [镜像ID]

#例如:
[root@centos7 conf]# docker rmi dfda7a2cf273

5、启动容器

docker run -it \
--name redis_6379 \
--privileged \
-p 6379:6379 \
-v /home/software/redis/6379/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /home/software/redis/6379/data/:/data \
-v /home/software/redis/6379/log/redis.log:/var/log/redis.log \
-d redis

docker run [镜像名称] 配置说明:

参数说明
-it允许进入容器内部,使用“docker exec -it [容器名称] bash” 进入容器
–name指定容器名称
–privileged当值为true时,指定容器权限为root权限
–p指定容器内镜像的映射端口,例如:“6379:6379” ,
前面的6379为主机的端口,后面的6379为容器内部的端口
–v指定容器内镜像文件的挂载地址,例如:“/home/software/redis/6379/conf/redis.conf:/usr/local/etc/redis/redis.conf”
“:”前面的地址代表的是主机的文件路径,“:”后面的地址代表的是容器内部的文件路径
–d指定容器为后台运行

☆处理docker run 权限问题:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"write /proc/self/attr/keycreate: permission denied\"": unknown.

关闭电脑的selinux

修改/etc/selinux/config文件中的SELINUX=“” 为 disabled ,然后重启。
或者使用命令

sed -i 's/enforcing/disabled/'  /etc/selinux/config
setenforce  0

6、查看容器

docker ps
docker ps -a

#例如:
[root@centos7 conf]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                   CREATED          STATUS          PORTS                                           NAMES
4471b5c7a853   mongo     "docker-entrypoint.s…"   10 minutes ago   Up 10 minutes   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   mongodb_27017
cfe1599ab134   redis     "docker-entrypoint.s…"   28 minutes ago   Up 28 minutes   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp       redis_6379

7、查看容器元数据(详情查看)

#语法格式
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
-f	指定返回值的模板文件
-s	如果类型为容器,则显示文件总大小
--type	返回指定类型的JSON


#获取容器 IP
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER_ID
#获取IP地址
docker inspect tomcat7 | grep IPAddress
#获取日志路径
docker inspect --format='{{.LogPath}}' $CONTAINER_ID

[root@localhost ~]# docker inspect  124735ca52a8

6、查看容器日志

docker logs [容器名称]

#例如:
[root@docker conf]# docker logs redis_6379
1:C 15 Sep 2023 03:43:09.006 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:C 15 Sep 2023 03:43:09.006 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 15 Sep 2023 03:43:09.006 * Redis version=7.2.1, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 15 Sep 2023 03:43:09.006 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 15 Sep 2023 03:43:09.007 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 7.2.1 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1:M 15 Sep 2023 03:43:09.007 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 15 Sep 2023 03:43:09.009 * Server initialized
1:M 15 Sep 2023 03:43:09.009 * Ready to accept connections tcp

7、容器启停

#启动容器
docker start [容器ID、容器名称]
#重启容器
docker restart [容器ID、容器名称]
#停止容器
docker stop [容器ID、容器名称]

8、删除容器

#删除容器,需要先停止容器才能删除
docker rm [容器ID、容器名称]

#强制删除容器
docker rm -f [容器ID、容器名称]

9、进入容器

docker exec -it [容器名称] bash

#例如:
docker exec -it nginx_test bash

10、复制容器文件到宿主机

docker cp [容器名称]:[容器内路径]  [宿主机路径]

#例如:
docker cp nginx_test:/etc/nginx/nginx.conf  /data/software/nginx/81/conf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值