1
3

「Linuxのしくみ 増強改訂版」のコードを実行するため、DockerでUbuntuの仮想環境を構築する方法

Last updated at Posted at 2024-01-11

はじめに

「Linuxのしくみ 増強改訂版」はプログラムによる実験を行う際に、物理マシン上にインストールしたUbuntu20.24上で動かすことを想定しています。しかし、私はmacOSを使用しているので、Ubuntuの仮想環境を構築して学習を進めることにしました。その際に、Virtual Boxをインストールして仮想マシンを利用しようとしたのですが、上手くいかなかったという記事を見たので、今回はDockerでUbuntuの仮想環境を構築していきます。

GitHubのリポジトリ

このリポジトリに仮想環境を構築する際に必要な、ソースコードやDockerfileがあります。また、GitHubのREADMEにも本記事と同じ手順が記されているので、ご覧ください。

*書籍のサンプルコード(linux-in-practice-2nd)を使用しています。

手順1

Desktopに移動し、本リモートリポジトリをgit cloneする。本リポジトリのフォルダやファイルがhands-on-linuxフォルダとしてクローンされます。 今回の例では、Desktop以下にフォルダを作成していますが、どこでも大丈夫です。

cd Desktop
git clone https://github.com/zakzackr/hands-on-linux.git

手順2

Dockerfileが存在するhands-on-linuxフォルダに移動します。その後、2行目でDockerfileからhands-on-linuxというdocker imageを作成し、3行目でhands-on-linux(docker image)からコンテナを実行しています。

cd hands-on-linux
docker image build -t hands-on-linux .
docker run --rm -it hands-on-linux /bin/bash

手順2までを実行すると、下記のようにlinux-in-practiceフォルダ内に「Linuxのしくみ」を学習する際に必要なソースコードが表示されます。

root@8138f14ec0a3:/# cd linux-in-practice 
root@8138f14ec0a3:/linux-in-practice# ls
01-operating-system-overview  02-process-management-1  03-process-scheduler  04-memory-management  05-process-management-2  07-filesystem  08-storage-hierarchy  09-block-layer  10-virtualization  12-cgroups  LICENSE  README.md

試しにp.6のhello.goを実行してみる

root@8138f14ec0a3:/linux-in-practice# cd 01-operating-system-overview
root@8138f14ec0a3:/linux-in-practice/01-operating-system-overview# go build hello.go
root@8138f14ec0a3:/linux-in-practice/01-operating-system-overview# ./hello
hello world
root@8138f14ec0a3:/linux-in-practice/01-operating-system-overview# strace -o hello.log ./hello
hello world
root@8138f14ec0a3:/linux-in-practice/01-operating-system-overview# cat hello.log
...
write(1, "hello world\n", 12)           = 12
...

おわりに

書籍と同じ結果が得られました!今後学習を進めていく中でうまく動作しない部分があれば、適宜更新していきたいと思います。

1
3
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
1
3