0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Docker Machine のコマンドメモ

Posted at

#DockerMachineのコマンド

項目 バージョン
docker-machine v0.12.2
VirtualBox 6.1.4

##DockerMachineの作成から停止まで

###作成
作成するマシン名は「test-box」とする。

$ docker-machine create --driver virtualbox test-box

###一覧確認

$ docker-machine ls
NAME          ACTIVE   DRIVER       STATE     URL                         SWARM                DOCKER      ERRORS 
test-box      -        virtualbox   Running   tcp://192.168.99.110:2376                        v19.03.12       

###アクティブにする

$ eval $(docker-machine env test-box)
$ docker-machine ls
NAME          ACTIVE   DRIVER       STATE     URL                         SWARM                DOCKER      ERRORS 
test-box      *        virtualbox   Running   tcp://192.168.99.110:2376                        v19.03.12      

###SSHで接続してexitでログアウト

$ docker-machine ssh test-box
   ( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net

docker@test-box:~$ exit
logout

###IPアドレス確認

$ docker-machine ip test-box
192.168.99.110

###アクティブを解除

$ docker-machine ls
NAME          ACTIVE   DRIVER       STATE     URL                         SWARM                DOCKER      ERRORS 
test-box      *        virtualbox   Running   tcp://192.168.99.110:2376                        v19.03.12 
$ eval $(docker-machine env -u)
$ docker-machine ls
NAME          ACTIVE   DRIVER       STATE     URL                         SWARM                DOCKER      ERRORS 
test-box      -        virtualbox   Running   tcp://192.168.99.110:2376                        v19.03.12 

###Dockerマシンを停止

$ docker-machine stop test-box
Stopping "test-box"...
Machine "test-box" was stopped.

##DockerMachine上でコンテナを実行

###DockerMachine上でdockerコマンドを実行
DockerMachineがアクティブな状態でdockerコマンドを実行すると、そのコマンドはアクティブなDockerMachineのホスト上で実行していることになる。
以下はアクティブなtest-boxというDockerMachineホスト上で、nginxコンテナを実行している。
実行後に、test-boxホストのIPアドレスのポート番号8000にブラウザからアクセスする(192.168.99.110:8000)と「Welcome to nginx!」の画面が表示される。

$ docker-machine ls
NAME          ACTIVE   DRIVER       STATE     URL                         SWARM                DOCKER      ERRORS 
test-box      *        virtualbox   Running   tcp://192.168.99.110:2376                        v19.03.12 
$ docker run -d -p 8000:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
852e50cd189d: Pull complete 
a29b129f4109: Pull complete 
b3ddf1fa5595: Pull complete 
c5df295936d3: Pull complete 
232bf38931fc: Pull complete 
Digest: sha256:c3a1592d2b6d275bef4087573355827b200b00ffc2d9849890a4f3aa2128c4ae
Status: Downloaded newer image for nginx:latest
054e2f9af619f68c12dfa964772dcb9902ebfeed9c5805122dd4a967b87b88f7

###作成したコンテナの確認

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
054e2f9af619        nginx               "/docker-entrypoint.…"   2 hours ago         Up 9 minutes        0.0.0.0:8000->80/tcp   boring_chatelet

###DockerMachineホスト上のコンテナのシェルに入る

$ docker exec -it 054e2f9af619 bash                              
root@054e2f9af619:/# 
0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?