0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[自分用メモ]DockerFileでAnsible入りコンテナを構築

Last updated at Posted at 2025-02-09

Dockerイメージの作成手順

  1. rootユーザに移行する

  2. Dockerfileを格納するディレクトリを作成

    mkdir /home/vboxuser/dockerFiles/ansibleDockerFile
    
  3. 作成したディレクトリに移動

    cd /home/vboxuser/dockerFiles/ansibleDockerFile
    
  4. Makefileを作成

    vim Makefile
    

    以下の内容を記述

    # Rocky Linux 9のベースイメージを取得
    pull-image:
            docker pull rockylinux:9
    
    # Ansibleコンテナのイメージをビルド
    build-ansible-container-image: pull-image
            docker build -t ansible-container-image .
    
  5. Dockerfileを作成

    vim Dockerfile
    

    以下の内容を記述

     FROM rockylinux:9
     
     # EPEL リポジトリを追加
     RUN dnf install -y epel-release
     
     # dnfリポジトリをクリーンアップ
     RUN dnf clean all
     
     # dnfリポジトリをアップデート
     RUN dnf update -y
     
     # 必要パッケージをインストール
     RUN dnf install -y iproute iputils
     
     # Ansibleをインストール
     RUN dnf install -y ansible
    

    最新のAnsible(Ansible Core 2.18)をインストールするには、pipxコマンドを使用する必要があるかも。公式ドキュメント:https://releases.ansible.com/

  6. Makefileを実行

    make build-ansible-container-image
    
  7. 作成したイメージの確認

    docker images
    

    以下のように、作成したDockerイメージが表示される

    [root@vbox ansibleDockerFile]# docker images
    REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
    ansible_rocky9   latest    d97694f8ebc0   30 minutes ago   902MB
    rockylinux       9         9cc24f05f309   14 months ago    176MB
    

Dockerコンテナのステータス確認

  1. コンテナを起動する

    Rocky Linux 9 の Docker イメージは、デフォルトで /bin/bash を実行するようにはなっていないため、コンテナ起動時に何も実行するコマンドを指定しないと、すぐに終了する。

    そのため、以下のコマンドでコンテナを起動する

    docker run -d --name <任意のコンテナ名> ansible-container-image sleep infinity
    

    sleep infinity は、コンテナ内で無限にスリープし続けるコマンドである。これによりコンテナが起動し続け、docker ps で確認できるようになる。
    以下はコマンド例

    docker run -d --name ansible-container ansible-container-image sleep infinity
    
  2. コンテナの起動状況を確認

    以下コマンドを実行

    docker ps
    

    結果、以下のようにansible-containerが表示されれば、「ansible-container」という名前のコンテナの構築に成功している

    CONTAINER ID   IMAGE                     COMMAND            CREATED         STATUS         PORTS     NAMES
    44876981f384   ansible-container-image   "sleep infinity"   7 seconds ago   Up 7 seconds             ansible-container
    
  3. コンテナにログインする

    docker exec -it ansible-container bash
    
  4. Ansibleがインストールされているか確認

    ansible --version
    

    以下のようにAnsibleバージョンが表示されればOK

    ansible [core 2.14.17]
      config file = /etc/ansible/ansible.cfg
      configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
      ansible python module location = /usr/lib/python3.9/site-packages/ansible
      ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
      executable location = /usr/bin/ansible
      python version = 3.9.21 (main, Dec  5 2024, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-2)] (/usr/bin/python3)
      jinja version = 3.1.2
      libyaml = True
    
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?