docker環境で、redhatを触れるように構築してみた

rhelを気軽に触れる環境が欲しかったんだ

docker環境で、redhatを触れるように構築してみた
目次

TL;DR


本記事でわかることは以下の通りです。

☆ dockerでrhel9の環境を構築する方法
☆ rhelの初期構築
☆ githubにsshで接続する方法

 

環境


  • macOS : Monterey v12
  • docker : 20.10.16
  • OS : Rhel9

 

1. docker イメージの取得


まずは、dockerイメージの検索
今回はrhel9で起動します

user@host:~$ docker search registry.access.redhat.com/ubi | grep ubi9 
ubi9/go-toolset           rhcc_registry.access.redhat.com_ubi9/go-tool…   0                    
ubi9-beta/ubi             Provides the latest release of Red Hat Unive…   0                    
ubi9/ubi                  rhcc_registry.access.redhat.com_ubi9/ubi        0                    
ubi9                      rhcc_registry.access.redhat.com_ubi9            0                    
ubi9-beta/ubi-minimal     Provides the latest release of the Minimal R…   0                    
ubi9/ubi-init             rhcc_registry.access.redhat.com_ubi9/ubi-init   0                    
ubi9-init                 rhcc_registry.access.redhat.com_ubi9-init       0                    
ubi9-micro                rhcc_registry.access.redhat.com_ubi9-micro      0                    
ubi9-beta/ubi-init        Provides the latest release of the Red Hat U…   0                    
ubi9-beta/ubi-micro       Provides the latest release of Micro Univers…   0                    
ubi9/ubi-minimal          rhcc_registry.access.redhat.com_ubi9/ubi-min…   0                    
ubi9/ubi-micro            rhcc_registry.access.redhat.com_ubi9/ubi-mic…   0                    
ubi9-minimal              rhcc_registry.access.redhat.com_ubi9-minimal    0                    
ubi9-beta/toolbox         Toolbox containerized shell image based in U…   0                    
ubi9/toolbox              rhcc_registry.access.redhat.com_ubi9/toolbox    0   

 

イメージの取得

user@host:~$ docker pull registry.access.redhat.com/ubi9/ubi
user@host:~$ docker images | grep ubi9
registry.access.redhat.com/ubi9/ubi                               latest                                                  46720ac964ac   2 months ago    211MB

 

2. コンテナの起動


起動

docker run -it -d --name rhel9 \
--privileged \
registry.access.redhat.com/ubi9/ubi:latest \
/sbin/init

 

コンテナに入る

docker exec -it rhel9 /bin/bash

 

3. OSの設定


 


useradd -u 1000 username
passwd username
usermod -aG wheel username
dnf install -y sudo
dnf install -y git
dnf install iproute -y
dnf install iputils -y
dnf install wget -y

# locale
cat /etc/locale.conf 
localectl status
localectl list-locales
ls /usr/share/i18n/locales/ | grep ja
localectl list-locales | grep -i ja
dnf install glibc-all-langpacks
localectl list-locales | grep -i ja
localectl set-locale LANG=ja_JP.UTF-8
cat /etc/locale.conf

# timezone
cp -ip /etc/localtime /etc/localtime.org
ln -sf  /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
cat /etc/localtime
export LANGUAGE=en_US # 文字化け回避

# python
dnf install python3-pip -y
ln -n /usr/bin/python3 /usr/bin/python

vi .bashrc
# 以下を追加
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

 

4. githubにsshで接続する


キーペアの生成


ssh-keygen -t rsa 

username@hostname:~/.ssh$ ll
total 20
drwx------ 1 username username 4096 Jun  1 12:30 ./
drwxr-x--- 1 username username 4096 Jun  1 11:35 ../
-rw-rw-r-- 1 username username    0 Jun  1 11:36 config
-rw------- 1 username username 2602 Jun  1 12:30 id_rsa_github
-rw-r--r-- 1 username username  571 Jun  1 12:30 id_rsa_github.pub

 

githubに公開鍵を登録する(公式ドキュメントを参考に) https://docs.github.com/ja/authentication/connecting-to-github-with-ssh

 

接続確認

username@hostname:~/.ssh$ ssh -T git@github.com
The authenticity of host 'github.com (13.114.40.48)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
git@github.com: Permission denied (publickey).

 

うまくいかないので、確認↓

username@hostname:~/.ssh$ git config --list
user.name=username
user.email=username@gmail.com
username@hostname:~/.ssh$ eval "$(ssh-agent -s)"
Agent pid 543
username@hostname:~/.ssh$
username@hostname:~/.ssh$ ssh-add -l
The agent has no identities.
username@hostname:~/.ssh$
username@hostname:~/.ssh$ ssh-add id_rsa_github
Identity added: id_rsa_github (username@hostname)
username@hostname:~/.ssh$ 
username@hostname:~/.ssh$ ssh-add -l
3072 SHA256:rr6kLmYIS63LXto1xX3DIJPx601fI2heQeHmhdOZ3yc username@hostname (RSA)
username@hostname:~/.ssh$ 
username@hostname:~/.ssh$ 
username@hostname:~/.ssh$ ssh -T git@github.com
Hi ***! You've successfully authenticated, but GitHub does not provide shell access.
username@hostname:~/.ssh$

 

5. Dockerカスタマイズ


コンテナイメージの更新 次回から更新したイメージでコンテナを起動する

# update image
docker stop rhel9 //起動中のコンテナを停止
docker commit rhel9 org_rhel9 //イメージを更新

  更新したイメージで起動
ポートフォワードの設定やディスクマウントの設定を追加

docker run -it -d --name rhel9 \
-p 2222:22 \
-p 8080:80 \
-p 5050:5000 \
-p 19999:19999 \
-p 1313:1313 \
--ip 172.17.0.1 \
-h hostname \
-v /Users/hoge/work/:/home/hoge/work/ \
--privileged \
org_rhel9:latest \
/sbin/init

 

ログイン時のユーザとディレクトリを指定する

docker exec -it -u 1000 -w /home/username rhel9 /bin/bash

 

まとめ


これでMacでも快適なRhel環境を構築できました。