Docker容器(一)--安装及简单配置

一、安装Docker

仓库下载地址:https://download.docker.com/

    仓库配置文件:https://download.docker.com/linux/centos/docker-ce.repo

也可以选用清华的镜像

      https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo

下载后使用vim编辑一下把地址替换为清华大学镜像的地址

 vim  docker-ce.repo 
	查找替换	
	:%s@https://download.docker.com/@https://mirrors.tuna.tsinghua.edu.cn/docker-ce/@

安装Docker要求Centos内核版本高于3.10。可以通过uname -r查看当前系统的内核版本,这里使用的是centos7.4版本。

[root@centos7 yum.repos.d]# uname -r
3.10.0-693.el7.x86_64

可以查看所有仓库中所有docker版本,并选择特定版本安装

[root@centos7 yum.repos.d]# yum list docker-ce --showduplicates | sort -r
已加载插件:fastestmirror, langpacks
可安装的软件包
Loading mirror speeds from cached hostfile
docker-ce.x86_64            3:19.03.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.0-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.0-3.el7                     docker-ce-stable
docker-ce.x86_64            18.06.3.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.2.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.1.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.0.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.3.ce-1.el7                    docker-ce-stable
docker-ce.x86_64            17.03.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable

安装Docker,命令:yum install docker-ce-版本号,不选择版本号则默认安装最新版本。

[root@centos7 yum.repos.d]# yum install -y docker-ce

出现报错
在这里插入图片描述
解决办法:
1、下载container-selinux:

wget https://mirrors.aliyun.com/centos/7/extras/x86_64/Packages/container-selinux-2.107-3.el7.noarch.rpm

2、安装container-selinux(使用–nodeps --force跳过依赖检查):

  rpm -ivh container-selinux-2.107-3.el7.noarch.rpm --nodeps --force

安装成功后,启动服务。

[root@centos7 app]# systemctl start docker
[root@centos7 app]# systemctl enable docker

查看版本信息

[root@centos7 app]# docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:27:04 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:25:42 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

二、Docker相关配置文件

Docker组件:

    docker-ce:
       配置文件:/etc/docker/daemon.json

Docker镜像加速

docker cn
阿里云加速器
中国科技大学
例:vim /etc/docker/daemon.json
	{
	     "registry-mirrors"	: ["https://registry.docker-cn.com"]	
	}
    
    
注册阿里云账号,专用加速器地址获得路径:
    https://cr.console.aliyun.com/#/accelerator
[root@centos7 docker]# vim /etc/docker/daemon.json

{
      "registry-mirrors" : ["https://registry.docker-cn.com"]
}

逻辑:
	Containers:容器,运行时的镜像具有生命周期
	Images:镜像、映像
	Registry:Image Repositories 镜像仓库
	容器的状态:
	created:
	runing:
	paused:
	stopped:
	deleted:

三、docker相关操作

1、docker info
查看当前docker的信息

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

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 19.03.8
 Storage Driver: overlay2
  Backing Filesystem: <unknown>
  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-693.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 1.781GiB
 Name: centos7.cwj.com
 ID: RK6H:JQZP:EFEH:ZOTJ:CER3:JF72:BZXT:WYFZ:6QEL:EAQP:SFVX:MKW3
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://registry.docker-cn.com/
 Live Restore Enabled: false

2、docker search
在Docker Hub中搜索镜像

[root@centos7 docker]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13152               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1796                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   771                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   111                                     
bitnami/nginx                      Bitnami nginx Docker Image                      83                                      [OK]
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   70                                      [OK]
alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from sou…   58                                      [OK]
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   57                                      
nginxdemos/hello                   NGINX webserver that serves a simple page co…   48                                      [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        42                                      [OK]
nginx/unit                         NGINX Unit is a dynamic web and application …   37                                      
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         30                                      
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   24                                      [OK]
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   18                                      [OK]
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  14                                      
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13                                      
centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   13                                      
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   12                                      [OK]
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       12                                      
sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats a…   7                                       [OK]
mailu/nginx                        Mailu nginx frontend                            6                                       [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   5                                       [OK]
wodby/nginx                        Generic nginx                                   1                                       [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                                       [OK]
centos/nginx-110-centos7           Platform for running nginx 1.10 or building …   0                                       

3、docker pull 等价于docker image pull
从docker registry中获取镜像

[root@centos7 docker]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
e2334dd9fee4: Pull complete 
Digest: sha256:a8cf7ff6367c2afa2a90acd081b484cbded349a7076e7bdf37a05279f276bc12
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
[root@centos7 docker]# docker pull nginx:1.14-alpine
1.14-alpine: Pulling from library/nginx
bdf0201b3a05: Pull complete 
3d0a573c81ed: Pull complete 
8129faeb2eb6: Pull complete 
3dc99f571daf: Pull complete 
Digest: sha256:485b610fefec7ff6c463ced9623314a04ed67e3945b9c08d7e53a47f6d108dc7
Status: Downloaded newer image for nginx:1.14-alpine
docker.io/library/nginx:1.14-alpine


4、docker image ls
列出当前服务器上的镜像文件。

[root@centos7 docker]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              be5888e67be6        3 weeks ago         1.22MB
nginx               1.14-alpine         8a2fb25a19f5        13 months ago       16MB

5、docker network ls
列出docker本地的网络类型

[root@centos7 app]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
6d86f805cb88        bridge              bridge              local
4c484f1fad68        host                host                local
08e0be372dd3        none                null                local

[root@centos7 app]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:06:c0:3d:08  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

6、docker ps等价于docker containers ls
列出处于运行状态的容器

[root@centos7 ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
c11c79b63651        busybox             "sh"                10 minutes ago      Up 10 minutes                           busy1

7、docker run
在新容器中运行命令,不指定命令则运行容器默认的命令。
运行busybox容器,该容器默认运行一个shell

[root@centos7 app]# docker run --name busy1 -it busybox
/ # 
/ # ls /
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # ps
PID   USER     TIME  COMMAND
    1 root      0:00 sh
    7 root      0:00 ps

在busybox中运行一个web服务

/ # httpd -h
httpd: option requires an argument -- h
BusyBox v1.31.1 (2020-04-14 01:09:51 UTC) multi-call binary.

Usage: httpd [-ifv[v]] [-c CONFFILE] [-p [IP:]PORT] [-u USER[:GRP]] [-r REALM] [-h HOME]
or httpd -d/-e/-m STRING

Listen for incoming HTTP requests

	-i		Inetd mode
	-f		Don't daemonize
	-v[v]		Verbose
	-p [IP:]PORT	Bind to IP:PORT (default *:80)
	-u USER[:GRP]	Set uid/gid after binding to port
	-r REALM	Authentication Realm for Basic Authentication
	-h HOME		Home directory (default .)
	-c FILE		Configuration file (default {/etc,HOME}/httpd.conf)
	-m STRING	MD5 crypt STRING
	-e STRING	HTML encode STRING
	-d STRING	URL decode STRING
/ # mkdir -p /data/html
/ # vi /data/html/index.html
/ # cat /data/html/index.html 
<h1>Busybox httpd server</h1>
/ # httpd -f -h /data/html/

使用docker inspect NAME 查看指定容器的详细信息。

[root@centos7 ~]# docker inspect busy1
...
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "6d86f805cb88b5f2b58935668b9c167eeeec96954de703198ada35fa36ffb0ef",
                    "EndpointID": "7d96146e29bd955afa0c98906c573dbd857fa7e3412f676eae7ba90107fcafd4",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

在容器外部使用curl命令访问测试。

[root@centos7 ~]# curl 172.17.0.2
<h1>Busybox httpd server</h1>

运行nginx容器

[root@centos7 app]# docker run --name web1 -d nginx:1.14-alpine ##-d表示在后台运行容器并打印容器ID
47ae69d98ad0bb55f920c8f2535ab31bce2fcdc51c4545f84ad240c323ccfbaf
[root@centos7 app]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
47ae69d98ad0        nginx:1.14-alpine   "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds        80/tcp              web1

nginx -g daemon off表示nginx进程在容器中运行于前台,在docker中必须有一个运行于前台的进程,否则容器将终止。

[root@centos7 ~]# curl 172.17.0.3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

8、docker start
运行一个或多个处于停止状态的容器

[root@centos7 app]# docker start -i -a busy1
/ # ls
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
/ # 

9、docker stop和docker kill
这两条命令都是用于停止正在运行的容器,docker stop是属于正常终止,相当于发送15信号,docker kill是强制终止,相当于发送9信号。

[root@centos7 ~]# docker stop busy1
busy1
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
c11c79b63651        busybox             "sh"                38 minutes ago      Exited (137) 41 seconds ago                       busy1
[root@centos7 ~]# docker kill busy1
busy1
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
c11c79b63651        busybox             "sh"                39 minutes ago      Exited (137) 5 seconds ago                       busy1

10、删除容器

# [root@centos7 app]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
c11c79b63651        busybox             "sh"                40 minutes ago      Exited (137) 38 seconds ago                       busy1
[root@centos7 app]# docker rm busy1
busy1
[root@centos7 app]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

11、docker exec
在运行的容器中执行命令,如:可以通过运行一个shell进入到运行的web1容器中,并查看其信息。

[root@centos7 ~]# docker exec -it web1 /bin/sh
/ # ls
bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # ps
PID   USER     TIME  COMMAND
    1 root      0:00 nginx: master process nginx -g daemon off;
    6 nginx     0:00 nginx: worker process
    7 root      0:00 /bin/sh
   13 root      0:00 ps

12、docker logs
获取某个容器的日志信息。

[root@centos7 ~]# docker logs web1
172.17.0.1 - - [11/May/2020:13:08:17 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.17.0.1 - - [11/May/2020:13:30:27 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker是一种容器化技术,可在其中运行现代应用程序。Docker容器本身是一种虚拟化技术,它允许用户将应用程序与依赖项捆绑在一起,并以一种轻量级的方式运行在不同的环境中。Docker容器的关键优势在于能够轻松地在不同的环境中部署应用程序,达到开发人员和运维人员的持续整合、持续部署和持续交付。 Docker Compose是一种工具,它允许用户定义和运行多个Docker容器。使用Docker Compose可以轻松地配置容器之间的网络连接和依赖关系,使得多个Docker容器能够协同工作,从而形成一个完整应用程序。 连接本地数据库时,可以使用Docker Compose配置文件定义数据库容器和应用程序容器之间的连接。在Docker Compose配置文件中,可以指定数据库容器要映射到的本地端口,以及应用程序容器如何连接到本地数据库。例如,下面是一个简单Docker Compose配置文件,用于连接一个MySQL数据库和一个PHP应用程序: version: '3.1' services: db: image: mysql:latest ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: example_password php: build: . ports: - "80:80" depends_on: - db environment: DB_HOST: db DB_USER: root DB_PASSWORD: example_password 在上面的Docker Compose配置文件中,我们定义了两个服务:一个MySQL数据库服务和一个PHP应用程序服务。数据库服务的镜像是MySQL的最新版。我们将其映射到本地端口3306,并设置了MySQL的root密码。PHP应用程序服务使用Dockerfile进行构建。我们将其映射到本地端口80,并指定它依赖于数据库服务。然后我们通过环境变量将应用程序容器连接到本地数据库。 总之,Docker容器Docker Compose的使用使得我们能够轻松地集成和部署多个容器,包括本地数据库,使得整个应用程序具有高效性、可靠性和扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值