Docker は、ソフトウェア開発者がコンテナ化されたアプリケーションを構築、展開、および管理できるようにするオープンソースプラットフォームです。 これは、アプリケーションが、任意のオペレーティングシステムで実行するために必要なソースコード、ライブラリ、およびその他の依存関係の組み合わせであるコンテナにパッケージ化されていることを意味します。
さらに、dockerを使用すると、コンテナーでの作業がより簡単に、よりスムーズに、より安全になります。
このチュートリアルでは、Ubuntu 20.04にDockerをインストールするすべての手順を実行し、Dockerコンテナー/イメージを操作する際の基本的な概念を学習します。
Dockerをインストールする
の公式ソフトウェアリポジトリ Ubuntu 20.04、dockerインストールパッケージはデフォルトで利用可能です。 ただし、これは最新バージョンではありません。 公式のDockerリポジトリから最新バージョンのDockerをインストールすることをお勧めします。
まず、次のコマンドを実行してシステムに新しいリポジトリを追加するために、現在のパッケージリストを更新し、前提条件のパッケージをインストールする必要があります。
$ sudo apt update
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent
次に、あなたはしなければなりません GPGキーをインポートします Dockerリポジトリの:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
それで、 Dockerリポジトリを追加します システム上のAPTソースへ:
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
次のコマンドを実行して、新しく追加されたリポジトリを更新します。
$ sudo apt update
あなたが行くことを確認してください Dockerをインストールする Ubuntuリポジトリの代わりにそのリポジトリから:
$ apt-cache policy docker-ce
出力は次のようになります。
docker-ce:
Installed: (none)
Candidate: 5:19.03.11~3-0~ubuntu-focal
Version table:
5:19.03.11~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
5:19.03.10~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
5:19.03.9~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
上記の出力は、Ubuntu20.04のDockerリポジトリからの「docker-ce」がシステムにインストールされることを示しています。
最後に、インストールします docker-this (コミュニティ版):
$ sudo apt install docker-ce
そうするには 確認 dockerが正常にインストールされ、正しく実行されているかどうかに関係なく、次のコマンドを実行します。
$ sudo systemctl status docker
出力には、Dockerサービスがアクティブ(実行中)であることが示されます。
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2020-06-17 15:29:48 +07; 8min ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 136640 (dockerd)
Tasks: 17
Memory: 50.8M
CGroup: /system.slice/docker.service
└─136640 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
...
これで、Ubuntuマシンにdockerが正常にインストールされました。 このチュートリアルの次のセクションでは、dockerコマンドの使用方法について詳しく説明します。
せずにdockerを実行する sudo 特権
デフォルトでは、dockerコマンドをで実行する必要があります sudo 特権またはdockerグループのユーザーによる。 せずにdockerコマンドを実行しようとした場合 sudo または、Dockerグループに属していないユーザーの権限がある場合は、次のようなエラーが発生します。
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.
回避策は、ユーザーを「docker」グループに追加することです。
$ sudo usermod -aG docker <user-name>
現在のセッションからログアウトし、再度ログインしてメンバーシップを適用します。 次のコマンドを実行して、ユーザーが「docker」グループに属していることを確認します。
$ groups
ubuntu sudo docker
今後は、 ‘なしでdockerコマンドを実行できます。sudo’。
Dockerコマンド
このセクションでは、 Dockerを使用する 指図。 詳細に入る前に、「docker」コマンドの構文を見てみましょう。
$ docker [options] [sub-commands] [arguments]
dockerで使用可能なすべてのサブコマンドを一覧表示するには、次のコマンドを実行します。
$ docker
多くのオプションがあり、サブコマンドと引数を覚えておく必要があります。 このチュートリアルの次のセクションで、それらのいくつかを詳しく見ていきましょう。
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
Dockerイメージ
Dockerイメージは、ソースコード、ライブラリ、およびアプリケーションがコンテナーとして実行するために必要なすべての依存関係で構成されます。 Dockerイメージは最初から作成できますが、コンテナーレジストリからこれらのイメージをプルダウンできます。 レジストリは、画像を保存し、一意の名前を使用してダウンロードできる場所です。 デフォルトでは、dockerのコンテナレジストリはDockerHubです。
から画像をプルできるかどうかを確認するには DockerHub、次のコマンドを実行できます。
$ docker run hello-world
次の出力でわかるように、dockerは「hello-world」イメージをローカルで見つけることができず、DockerHubからイメージをプルしました。 イメージがダウンロードされた後、dockerはそのイメージからコンテナーを作成します。 コンテナ内のアプリケーションが実行され、次のメッセージが表示されます。
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
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
...
Docker Hubでイメージを検索するには、「docker」コマンドと「search」サブコマンドを使用できます。 ‘alpine’画像を検索する場合は、次のコマンドを実行します。
$ docker search alpine

ご覧のとおり、出力には、名前が「alpine」であるすべての画像のリストが返されます。
さらに、コマンド ‘を実行してDockerイメージをダウンロードできます。Dockerイメージプル‘画像の名前を続けます。 にとって example:
$ docker image pull alpine
インターネットの速度によっては、ダウンロードに1分かかる場合があります。
Using default tag: latest
latest: Pulling from library/alpine
df20fa9351a1: Pull complete
Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
さらに、すべてのダウンロードイメージを一覧表示する場合は、次のコマンドを実行します。
$ docker image ls
出力には、リポジトリ、タグ、画像ID、作成時間、ファイルサイズなどの画像の詳細が表示されます。
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest a24bb4013296 2 weeks ago 5.57MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
時々、あなたはする必要があります Dockerを削除する 画像の場合、次のコマンドを実行できます。
$ docker image rm <image-name>
また
$ docker image rm <image-id>
Dockerコンテナ
コンテナは、Dockerイメージの実行中のインスタンスです。 コンテナは、単一のアプリケーションまたはプロセスのランタイムを実行します。 ‘docker container’サブコマンドを実行して、コンテナーを操作し、コンテナーを開始/停止または削除できます。
にとって example、「Ubuntu」イメージに基づいてDockerコンテナを起動するには、次のコマンドを実行します。
ubuntu@linoxide:~$ docker container run ubuntu
Docker HubからUbuntuの新しいイメージをダウンロードし、Ubuntuコンテナーを起動すると、起動直後にUbuntuコンテナーが停止しているため、システムコマンドプロンプトが返されます。
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
a4a2a29f9ba4: Pull complete
127c9761dcba: Pull complete
d13bf203e905: Pull complete
4039240d2e0b: Pull complete
Digest: sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce
Status: Downloaded newer image for ubuntu:latest
ubuntu@linoxide:~$
コマンドラインからUbuntuコンテナを操作するには、次のコマンドでコンテナを起動します。 -it
オプション:
$ docker container run -it ubuntu
これで、コンテナに入り、そのインタラクティブシェルにアクセスできました。
root@45cc68d4395e:/#
これまでのところ、Ubuntuシステムですでに多くのコンテナを実行しています。 すべてのアクティブな(実行中の)コンテナーをリストするには、次のコマンドを実行します。
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
結果として空のリストが返されるため、実行中のコンテナはありません。 アクティブと非アクティブのすべてのコンテナを一覧表示する場合は、次のコマンドを実行します。
$ docker container ls -a
出力は次のようになります。
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45cc68d4395e ubuntu "/bin/bash" 14 minutes ago Exited (0) 11 minutes ago stoic_dhawan
fc91e50d223e ubuntu "/bin/bash" 26 minutes ago Exited (0) 26 minutes ago sleepy_ritchie
ce0d81682913 alpine "/bin/bash" 26 minutes ago Created trusting_kowalevski
4291477f5ac2 alpine "/bin/sh" 27 minutes ago Exited (130) 27 minutes ago priceless_wozniak
86c0fd44a4f4 alpine "/bin/sh" 28 minutes ago Exited (0) 28 minutes ago objective_agnesi
3fdcaa196fca hello-world "/hello" 31 minutes ago Exited (0) 31 minutes ago eloquent_rhodes
コンテナを削除/削除する場合は、次のコマンドを実行します。
$ docker container rm <container-id>
結論
このチュートリアルでは、Ubuntu 20.04にDockerをインストールする方法と、例を通じてイメージやコンテナーを操作する方法を学びました。 これらはDockerの概念の基本であり、通常は開発者が使用します。
読んでいただきありがとうございます。以下のコメントセクションに提案を残してください。