当前位置:   article > 正文

RHEL 9.3安装docker及简单示例_rhel9 安装docker

rhel9 安装docker

环境

  • RHEL 9.3
  • Docker Community 24.0.7

准备

  • 注册系统:
sudo subscription-manager register
  • 1

根据提示,输入username和password,这是在redhat网站上注册的用户名和密码。

  • 升级系统:
sudo yum update
  • 1

安装步骤

  • 删除旧docker版本:
sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
  • 1
  • 删除 /var/lib/docker/ 里的数据。

注:本例中,之前没有安装过docker,也并没有 /var/lib/docker/ 目录。

  • 安装 yum-utils
sudo yum install -y yum-utils
  • 1
  • 添加 docker-ce repo:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  • 1
  • 查看repolist:
[ding@192 ~]$ sudo yum repolist
Updating Subscription Management repositories.
repo id                                                                                                    repo name
docker-ce-stable                                                                                           Docker CE Stable - x86_64
rhel-9-for-x86_64-appstream-rpms                                                                           Red Hat Enterprise Linux 9 for x86_64 - AppStream (RPMs)
rhel-9-for-x86_64-baseos-rpms                                                                              Red Hat Enterprise Linux 9 for x86_64 - BaseOS (RPMs)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 安装docker:
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • 1
  • 启动docker:
sudo systemctl start docker
  • 1
  • Enable docker service(开机启动):
sudo systemctl enable docker.service
  • 1
  • 查看docker版本:
[ding@192 ~]$ sudo docker version
Client: Docker Engine - Community
 Version:           24.0.7
 API version:       1.43
 Go version:        go1.20.10
 Git commit:        afdd53b
 Built:             Thu Oct 26 09:09:13 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.10
  Git commit:       311b9ff
  Built:            Thu Oct 26 09:07:45 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.26
  GitCommit:        3dd1e886e55dd695541fdcd67420c2888645a495
 runc:
  Version:          1.1.10
  GitCommit:        v1.1.10-0-g18a0cb0
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 查看docker compose版本:
[ding@192 ~]$ docker compose version
Docker Compose version v2.21.0
  • 1
  • 2
  • 给非root用户赋权
sudo groupadd docker
  • 1

注:本例中,提示“groupadd: group ‘docker’ already exists”。

sudo usermod -aG docker $USER
  • 1

重新打开命令行窗口。(注:本例中使用的是虚拟机,则需要重启虚拟机)

验证:

[ding@192 ~]$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
  • 1
  • 2

docker示例

hello-world

  • 运行hello-world:
[ding@192 ~]$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete 
Digest: sha256:ac69084025c660510933cca701f615283cdbb3aa0963188770b54c31c8962493
Status: Downloaded newer image for hello-world:latest

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/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 查看运行的容器:
[ding@192 ~]$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
  • 1
  • 2

并没有任何进程,这是因为helloworld进程已经结束了。

  • 查看所有容器(包括非运行状态的):
[ding@192 ~]$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED              STATUS                          PORTS     NAMES
6b8bee697866   hello-world   "/hello"   About a minute ago   Exited (0) About a minute ago             recursing_sutherland
  • 1
  • 2
  • 3
  • 删除容器:
docker rm 6b8bee697866
  • 1
  • 查看image:
[ding@192 ~]$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d2c94e258dcb   7 months ago   13.3kB
  • 1
  • 2
  • 3
  • 删除image:
docker rmi hello-world
  • 1

welcome-to-docker

  • 搜索image:
[ding@192 ~]$ docker search welcome-to-docker
NAME                                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker/welcome-to-docker                   Docker image for new users getting started w…   16                   
......
  • 1
  • 2
  • 3
  • 4
  • 运行 welcome-to-docker
docker run -p 8080:80 docker/welcome-to-docker
  • 1

打开浏览器,访问 http://localhost:8080 ,如下:

在这里插入图片描述

按下“Ctrl + C”停止进程,或者在另一个终端窗口用 docker stop xxx 命令停止进程。最后用 docker rmdocker rmi 清理容器和镜像。

Build image

新建目录 welcome-to-docker

在该目录下,运行

wget https://github.com/docker/welcome-to-docker/archive/refs/heads/main.zip
  • 1

解压:

unzip main.zip
  • 1

进入 welcome-to-docker-main 目录,查看内容:

[ding@192 welcome-to-docker-main]$ ls
Dockerfile  MAINTAINERS.md  package.json  package-lock.json  public  README.md  src
  • 1
  • 2

其中,最重要的就是 Dockerfile

使用 docker build 命令构建image:

docker build -t welcome-to-docker .
  • 1

在下面这一步会卡住:

=> [6/6] RUN npm install     && npm install -g serve     && npm run build     && rm -fr node_modules
  • 1

我等了半小时,还一直hang着,只好Ctrl + C了。

这应该是因为npm的server连不上。

打开 Dockerfile 文件,对应的代码为:

# Install node packages, install serve, build the app, and remove dependencies at the end
RUN npm install \
    && npm install -g serve \
    && npm run build \
    && rm -fr node_modules
  • 1
  • 2
  • 3
  • 4
  • 5

解决办法为,设置国内镜像: npm config set registry=https://registry.npmmirror.com

# Install node packages, install serve, build the app, and remove dependencies at the end
RUN npm config set registry=https://registry.npmmirror.com \
    && npm install \
    && npm install -g serve \
    && npm run build \
    && rm -fr node_modules
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

保存修改,再次build:

docker build -t welcome-to-docker .
  • 1

这次等了大约一分钟就完成了。

查看image:

[ding@192 welcome-to-docker-main]$ docker images                                                                                                                                                                                             
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE                                                                                                                                                                           
welcome-to-docker   latest    4ee26f3f44fc   25 seconds ago   190MB
  • 1
  • 2
  • 3

运行 welcome-to-docker

docker run -p 8080:3000 welcome-to-docker
  • 1

打开浏览器,访问 http://localhost:8080 ,可以成功访问页面。

注: Dockerfile 文件里是 EXPOSE 3000 ,所以使用 -p 8080:3000

multi-container

新建目录 multi-container

在该目录下,运行:

wget https://github.com/docker/multi-container-app/archive/refs/heads/main.zip
  • 1

解压:

unzip main.zip
  • 1

进入 multi-container-app-main 目录,查看内容:

[ding@192 multi-container-app-main]$ ls
app  compose.yaml  README.md
  • 1
  • 2

运行 docker compose up -d

[ding@192 multi-container-app-main]$ docker compose up -d
validating /home/ding/Downloads/docker/multi-container/multi-container-app-main/compose.yaml: services.todo-app Additional property develop is not allowed
  • 1
  • 2

报错说: services.todo-app Additional property develop is not allowed 。我测试了一下,是因为compose的版本太低:

[ding@192 multi-container-app-main]$ docker compose version
Docker Compose version v2.21.0
  • 1
  • 2

需要将其升级到 v2.23.3 。方法为:

DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
  • 1

注: :- 表示设置缺省值。所以这句话意思是,如果没有设置变量 DOCKER_CONFIG ,就将其设置为 $HOME/.docker ,本例中为 /home/ding/.docker

创建 cli-plugins 目录:

mkdir -p $DOCKER_CONFIG/cli-plugins
  • 1

下载 docker-compose

curl -SL https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
  • 1

注:本例中,指定了版本 v2.23.3 。可以到 https://github.com/docker/compose/releases 去看一下当前的最新版本是什么。

添加可运行属性:

chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
  • 1

再次查看compose版本:

[ding@192 ~]$ docker compose version
Docker Compose version v2.23.3
  • 1
  • 2

可见,已经升级到 v2.23.3 了。

不过,只有当前用户使用的是 v2.23.3 版本,其他用户比如root,使用的仍然是 v2.21.0 版本。所以,最好将其放置于 /usr/local/lib/docker/cli-plugins/ 目录下(如果没有该目录则用root权限创建),则所有用户都使用该版本。

现在,再次运行 docker compose up -d ,这次成功了。

查看docker容器:

[ding@192 multi-container-app-main]$ docker ps
CONTAINER ID   IMAGE                               COMMAND                  CREATED              STATUS              PORTS                                                                                      NAMES
c881e67bef98   multi-container-app-main-todo-app   "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3000->3000/tcp, :::3000->3000/tcp, 0.0.0.0:35729->35729/tcp, :::35729->35729/tcp   multi-container-app-main-todo-app-1
68596e594af8   mongo:6                             "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp                                              multi-container-app-main-todo-database-1
  • 1
  • 2
  • 3
  • 4

打开浏览器,访问 http://localhost:3000 ,如下:

在这里插入图片描述

添加几个task,如下:

在这里插入图片描述

新开一个浏览器窗口,也可以看到这些task。

如果不删除容器( docker rm ),仅仅 docker stop 后再重新启动容器,数据也还在。

但是,这些数据没有持久化。如果删除容器,然后再启动新的容器,数据就没了。

容器数据持久化

接着上面的例子。停止所有容器并删除容器。

编辑 compose.yaml 文件:

services:
  todo-app:
    build:
      context: ./app
    depends_on:
      - todo-database
    environment:
      NODE_ENV: production
    ports:
      - 3000:3000
      - 35729:35729
    develop:
      watch:
        - path: ./app/package.json
          action: rebuild
        - path: ./app
          target: /usr/src/app
          action: sync

  todo-database:
    image: mongo:6
    #volumes: 
    #  - database:/data/db # 把database volume mount到 /data/db
    ports:
      - 27017:27017

#volumes:
  #database: # 定义名为database的volume
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

有几行被注释掉了,将其反注释一下,保存。

运行容器:

[ding@192 multi-container-app-main]$ docker compose up -d
[+] Running 3/3
 ✔ Volume "multi-container-app-main_database"          Created                                                                                                                                                                          0.0s 
 ✔ Container multi-container-app-main-todo-database-1  Started                                                                                                                                                                          0.0s 
 ✔ Container multi-container-app-main-todo-app-1       Started 
  • 1
  • 2
  • 3
  • 4
  • 5

注:可以看到输出结果里有一行是关于创建volume的。不过创建volume是一次性的,所以这一行输出结果只在第一次运行时有,后面再次运行时就没了。

打开浏览器,访问 http://localhost:3000 ,输入一些task数据,然后停止容器,删除容器,重新运行容器,再次访问 http://localhost:3000 ,可以看到task数据还在,说明数据被持久化了。

关于volume

可用 docker volume ls 查看volume:

[root@192 ~]# docker volume ls
DRIVER    VOLUME NAME
local     multi-container-app-main_database
  • 1
  • 2
  • 3

然后用 docker volume inspect 命令查看某个volume:

[root@192 ~]# docker volume inspect multi-container-app-main_database
[
    {
        "CreatedAt": "2023-12-26T08:18:16+08:00",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "multi-container-app-main",
            "com.docker.compose.version": "2.23.3",
            "com.docker.compose.volume": "database"
        },
        "Mountpoint": "/var/lib/docker/volumes/multi-container-app-main_database/_data",
        "Name": "multi-container-app-main_database",
        "Options": null,
        "Scope": "local"
    }
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

可见挂载点为 /var/lib/docker/volumes/multi-container-app-main_database/_data

可以看一下里面的内容:

[root@192 ~]# ls /var/lib/docker/volumes/multi-container-app-main_database/_data
collection-0--6348594005971772566.wt  collection-7--6348594005971772566.wt  index-3--6348594005971772566.wt  index-8--6348594005971772566.wt  mongod.lock    WiredTiger       WiredTiger.turtle
collection-2--6348594005971772566.wt  diagnostic.data                       index-5--6348594005971772566.wt  journal                          sizeStorer.wt  WiredTigerHS.wt  WiredTiger.wt
collection-4--6348594005971772566.wt  index-1--6348594005971772566.wt       index-6--6348594005971772566.wt  _mdb_catalog.wt                  storage.bson   WiredTiger.lock
  • 1
  • 2
  • 3
  • 4

这和进入mongo容器里( docker exec -it xxx sh ),然后 ls /data/db 看到的内容是一致的。

参考

  • https://docs.docker.com/engine/install/centos/
  • https://docs.docker.com/compose/install/
  • https://docs.docker.com/storage/volumes/
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/576191
推荐阅读
相关标签
  

闽ICP备14008679号