Ubuntu 18.04 LTSにDockerをインストールしよう!
Dockerとは?
Dockerとは、Linux上で動くコンテナ管理システムです。コンテナ(実行するためのかたまり)の中には、実行に必要なアプリやファイルがぎっしり詰まっています。そこで完結できないものは、コンテナ同士を接続して、互いに使えるようにします。
サーバーを作りたいけど、ホストをいじりたくない、、、という人向けです。
インストールしよう!
パッケージからインストールしよう
UbuntuリポジトリにはDockerのパッケージが用意されているので、これを使って簡単にインストールできます。
まず、パッケージのアップデートから行います。
sudo apt update
これが完了したら、docker.ioパッケージをインストールします。Ubuntuには、dockerというパッケージがありますが、それは、「KDE3/GNOME2 docklet アプリケーション用システムトレイ」です。間違えてインストールしないようにしてください。
sudo apt install docker.io
標準ユーザにも権限を付与しよう
dockerは以上の操作でインストールされましたが、今現在では、dockerを実行するのにsudoを使って管理者権限で実行しなければならない状態です。sudoなしで実行するとこうなります。
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied
あらあら、権限ないよーて起こられていますね。流石に、これでは不便です。そのために、自分のユーザをdockerグループに追加します。以下のコマンドを実行します。
sudo gpasswd -a 自分のユーザ名 docker
これで、PCを再起動すると標準ユーザでもdockerを操作できます。
動作確認をしよう!
Dockerをせっかくインストールしたので、コンテナをすぐ(←それは私だけ?)動かしたいところです。イメージ配布(イメージとは、コンテナの元となるもの)の中心、Docker Hubには、hello-worldイメージが公開されていますので、これを使いましょう。
細かいオプション等は気にせず以下のコマンドを実行してください。
docker pull hello-world docker run --rm 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/