美文网首页
Linux作业(9)——docker安装及简单使用

Linux作业(9)——docker安装及简单使用

作者: 羰基生物 | 来源:发表于2020-11-16 10:04 被阅读0次

1、通过 RPM 安装 docker 17.03.0 版本并且配置 docker 阿里加速

docker-ce-17.03:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm

docker-ce-selinux-17.03:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm

下载好rpm包后直接yum安装,装完后将iptables的默认规则清空后启动docker服务,启动第一个容器测试是否能正常拉取镜像。接着配置阿里云镜像加速。

[root@localhost ~]# wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm
[root@localhost ~]# wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm
[root@localhost ~]# yum install -y *
[root@localhost ~]# systemctl enable --now docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /etc/systemd/system/docker.service.
​
[root@localhost ~]# docker version
Client:
 Version:      17.03.0-ce
 API version:  1.26
 Go version:   go1.7.5
 Git commit:   3a232c8
 Built:        Tue Feb 28 08:10:07 2017
 OS/Arch:      linux/amd64
​
Server:
 Version:      17.03.0-ce
 API version:  1.26 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   3a232c8
 Built:        Tue Feb 28 08:10:07 2017
 OS/Arch:      linux/amd64
 Experimental: false
​
[root@localhost ~]# docker run hello-world
​
Hello from Docker!
This message shows that your installation appears to be working correctly.
​
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 (amd64)
 3. The Docker daemon created a new container from that image which runs the
 executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
 to your terminal.
​
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
​
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
​
For more examples and ideas, visit:
 https://docs.docker.com/get-started/
​
​
##针对Docker客户端版本大于 1.10.0 的用户可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器
##阿里云加速,前往http://cr.console.aliyun.com获取自己的加速地址,可以按照阿里云的配置说明配置加速,也可以将加速地址写入service文件中。
###写入service文件,ExecStart后写上加速地址
ExecStart=/usr/bin/dockerd --registry-mirror=https://********.mirror.aliyuncs.com
systemctl daemon-reload
systemctl restart docker
###查看加速是否成功,如下看到加速地址信息则说明加速成功
[root@localhost ~]# ps -ef | grep docker
root       2310      1  0 22:05 ?        00:00:04 /usr/bin/dockerd --registry-mirror=https://5bkin14v.mirror.aliyuncs.com
root       2316   2310  0 22:05 ?        00:00:01 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc
root       2474   1539  0 22:30 pts/0    00:00:00 grep --color=auto docker

2、通过 docker 安装一个 LAPM 架构

  • 拉取lamp镜像
   [root@localhost lib]# docker search lamp
    NAME                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    mattrayner/lamp             A simple LAMP docker image running the pre...   240                  [OK]
    linode/lamp                 LAMP on Ubuntu 14.04.1 LTS Container            178                  
    tutum/lamp                  Out-of-the-box LAMP image (PHP+MySQL)           141                  
    greyltc/lamp                a super secure, up-to-date and lightweight...   101                  [OK]
    fauria/lamp                 Modern, developer friendly LAMP stack. Inc...   93                   [OK]
    ...
    ...
    [root@localhost lib]# docker pull tutum/lamp
    Using default tag: latest
    latest: Pulling from tutum/lamp
    8387d9ff0016: Pull complete 
    3b52deaaf0ed: Pull complete 
    4bd501fad6de: Pull complete 
    a3ed95caeb02: Pull complete 
    ...
    ...
    ...
    Digest: sha256:d332e7e97606ac6407b0ba9ae9e9383c89d7e04c6f4853332e98f7d326408329
    Status: Downloaded newer image for tutum/lamp:latest
    [root@localhost lib]# docker image 
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    hello-world         latest              bf756fb1ae65        10 months ago       13.3 kB
    tutum/lamp          latest              3d49e175ec00        4 years ago         427 MB
    ​
    [root@localhost ~]# docker run --name lamp -d -p 80:80 -p 3306:3306 -v /data/docker/mysql:/var/lib/mysql tutum/lamp
    61f7af5ffddf577b6c99c0a7e21d623bd4f34ed2cb2ba06aa19c80d7971ecd1c
    #--name:指定容器运行时的名字
    #-d:后台方式运行
    #-v:将容器MySQL数据目录映射到宿主机
    #-p:将容器的对应端口映射到宿主机端口

浏览器输入宿主机地址,查看测试页面。

3、写出 docker run 命令的延申指令,如怎么在停止一个 docker 容器的时候自动删除该容器

[root@localhost ~]# docker run --name httpd -p 80:80 -d httpd 
f1a6193014cdd41101e1fbd1a872bd40ed07a7d86177113b3fe07e2fbae3672f
​
[root@localhost ~]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                     PORTS                NAMES
f1a6193014cd        httpd               "httpd-foreground"   11 seconds ago      Up 11 seconds              0.0.0.0:80->80/tcp   httpd
a06faf0304e8        hello-world         "/hello"             7 minutes ago       Exited (0) 7 minutes ago                        condescending_lewin
​
[root@localhost ~]# docker stop f1a6193014cd | xargs docker rm
f1a6193014cd
​
[root@localhost ~]# docker ps -a -q
a06faf0304e8

4、写出 docker run 命令在自动启动 docker 服务时通过什么参数能够启动 docker 中的容器,从而实现容器随着 docker 服务的启动而自动启动

[root@localhost ~]# docker run --name httpd -p 80:80 -d --restart=always httpd
886cb4f143c09f28a328925de7f48555a22d5fbdb166ab2bb4742758476ac424
##增加--restart参数设置容器随宿主机启动而自动启动
[root@localhost ~]# reboot
Connection closing...Socket close.
​
WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Nov 16 08:51:51 2020 from 10.0.0.1
[root@localhost ~]# systemctl status docker.service 
● docker.service - Docker Application Container Engine
 Loaded: loaded (/etc/systemd/system/docker.service; enabled; vendor preset: disabled)
 Active: active (running) since Mon 2020-11-16 09:11:14 CST; 14s ago
 Docs: https://docs.docker.com
 Main PID: 977 (dockerd)
 Tasks: 39
 Memory: 62.3M
 CGroup: /system.slice/docker.service
 ├─ 977 /usr/bin/dockerd --registry-mirror=https://5bkin14v.mirror.aliyuncs.com
 ├─1051 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /va...
 ├─1620 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 80 -container-ip 172.17.0.2 -container-port 80
 └─1626 docker-containerd-shim 886cb4f143c09f28a328925de7f48555a22d5fbdb166ab2bb4742758476ac424 /var/run/docker/libcontainerd/886cb4f143c09f28a3...
[root@localhost ~]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND              CREATED              STATUS              PORTS                NAMES
886cb4f143c0        httpd               "httpd-foreground"   About a minute ago   Up 32 seconds       0.0.0.0:80->80/tcp   httpd

相关文章

网友评论

      本文标题:Linux作业(9)——docker安装及简单使用

      本文链接:https://www.haomeiwen.com/subject/rbuobktx.html