LoginSignup
1
1

More than 3 years have passed since last update.

Dockerのコマンドまとめ

Last updated at Posted at 2020-10-16

はじめに

Dockerは多くのIT開発企業で導入されている環境構築を楽にする技術です。
Dockerのコマンドをまとめます。

今回のDockerコマンド

  • docker login
  • docker pull
  • docker images
  • docker run
  • docker run --name
  • docker run --rm
  • docker ps -a
  • docker restart
  • docker exec -it (コンテナID)or(コンテナ名) bash
  • docker commit (コンテナID)or(コンテナ名) (新しいimage名)
  • docker tag (source) (target)
  • docker push
  • docker rmi
  • docker stop
  • docker rm
  • docker system prune

詳細

docker login

Dockerにログインするコマンド。
Dockerを使用する場合は最初に行う。

ターミナル
% docker login
Username: => Dockerに登録したユーザー名を入力
Password: => Dockerに登録したパスワードを入力

=> ユーザー名、パスワードが合っていれば、ログインする。(Login Succeededと表示される)

docker pull

Docker imageをdockerhubからホストにダウンロードするコマンド。

ターミナル
例)hello-worldというimageをダウンロードする時

% docker pull hello-world
=> hello-worldがダウンロードされる

=> ユーザー名、パスワードが合っていれば、ログインする。(Login Succeededと表示される)

docker images

ホストの中にあるdocker imageのリストを表示するコマンド。

ターミナル
例)hello-worldというimageをダウンロードした後

% docker images
=> REPOSITORY      TAG      IMAGE ID      CREATED      SIZE
   hello-world     latest   (自動作成のID)  (作成した時)   (データサイズ)

docker run

コンテナを作るコマンド。

ターミナル
例)hello-worldというimageをダウンロードした後

% docker run hello-world
=> hello-worldのコンテナが作成され、実行される。

% docker run -it ubuntu bash
=> ubuntu(OSの名前、dockerhubに存在するdocker image)のコンテナが作成され、bashというプログラムを実行させる。
(bashはshellの名前。ubuntuのデフォルトのコマンドをbashに上書きして実行している。ちなみにubuntuのデフォルトのコマンドはbashなので、今回はdocker -it run ubuntuでも同じ。)

docker run --name

名前をつけてコンテナを作るコマンド。

ターミナル

% docker run --name sample ubuntu
=> sampleという名前のコンテナがubuntuというdocker imageから作成される。

docker run --rm

コンテナを起動した後、すぐにコンテナを削除コマンド。

ターミナル

% docker run --rm hello-world
=> hello-worldというdocker imageからコンテナを作成し、実行した後、すぐにコンテナが削除される。
(一度しか使わないコンテナは残さないために、すぐに削除する。)

docker ps -a

全てのコンテナを表示するコマンド。

ターミナル
% docker ps -a
=> 全てのコンテナが表示される。

% docker ps
=> 実行中のコンテナのみされる。

docker restart

一度実行したコンテナをリスタートするコマンド。

ターミナル

% docker restart (コンテナID)or(コンテナ名)
=> コンテナIDで指定したコンテナがリスタートされる。(STATUSがUPとなる)

docker exec -it (コンテナID)or(コンテナ名) bash

リスタートしたコンテナにプログラムを実行させるコマンド。(bashの部分は適宜、実行したいプログラムに置き換える)

ターミナル

% docker exec -it (コンテナID)or(コンテナ名) bash
=> リスタートしたコンテナの中に入ることができる。(bashにより)

docker commit (コンテナID)or(コンテナ名) (新しいimage名)

更新したコンテナを新しいDocker imageとして保存するコマンド。

ターミナル

% docker commit (コンテナID)or(コンテナ名) (新しいimage名)
=> docker imageが保存され、IDが表示される。

docker tag (source) (target)

docker imageのimage名を変更するコマンド。
docker imageをdockerhubにpushする前に、ホストのimageのリポジトリ名とpush先の自身のdockerhubのリポジトリ名を同じにするために使う。(dockerhubのリポジトリはdockerhubで作成しておく必要がある)

ターミナル

% docker tag (ホストのdocker imageのリポジトリ名:タグ名) (dockerのユーザー名/リポジトリ名)
=> docker imageのリポジトリ名がdockerhubのリポジトリ名と同じになる。(タグ名は指定しなければ、latestとなる。)

docker push

docker imageを自分のリポジトリに作成するコマンド。

ターミナル

% docker push (image名)
=> dockerhubにdocker imageがpushされる。(image名はdockerのユーザー名/リポジトリ名と同じにしておく)

docker rmi

docker imageを削除するコマンド。(image名はdockerのユーザー名/リポジトリ名と同じにしておく)

ターミナル

% docker rmi (image名)or(image id)
=> 指定したdocker imageが削除される

docker stop

実行中のコンテナをストップするコマンド。(STATUSをUP→EXITEDにする)

ターミナル

% docker stop (コンテナ名)or(コンテナID)
=> 指定したコンテナがストップされる。

docker rm

コンテナを削除するコマンド。

ターミナル

% docker rm (コンテナ名)or(コンテナID)
=> 指定したコンテナが削除される

docker system prune

ストップしているコンテナ全てを一括で削除するコマンド。

ターミナル

% docker system prune
=> ストップしているコンテナが全て削除される

参考

Udemy

かめれおん講師 「米国AI開発者がゼロから教えるDocker講座」

https://www.udemy.com/share/103aTRAEAdd1pTTHoC/

有料ですが、初学者の私にも非常に理解しやすかったです。

最後に

本投稿が初学者の復習の一助となればと幸いです。

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