LoginSignup
1
5

More than 5 years have passed since last update.

Docker for MacでAmazon Linuxを動かしてみる

Posted at

条件

  • Mac OS Sierra
  • Docker for Macをインストール
  • Docker Hubにサインアップ

Hello World

Docker Hubにログイン

$ docker login

Docker Hubのユーザーネームとパスワードでログインする。

Hello Worldを実行する

まずはお手本通りHello Worldを実行する。

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
78445dd45222: Pull complete 
Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
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.
 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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

Hello Worldの最新イメージがpullされてコンテナとして実行された。(Hello World用のイメージがDockerHubに存在する)

Amazon Linuxを動かす

Amazon Linuxのイメージをpullする。

$ docker pull amazonlinux
Using default tag: latest
latest: Pulling from library/amazonlinux
0793c64286b7: Pull complete 
Digest: sha256:7c781c9234e712f135ee402a13ffd5dbf342a9ff1394c73bc5ae4d9b9078e0f8
Status: Downloaded newer image for amazonlinux:latest


# pullしたイメージを確認

$ docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
amazonlinux            latest              766ebb052d4f        6 weeks ago         162 MB

イメージからコンテナを作成して起動する。

$ docker container run -itd amazonlinux

#起動中のコンテナを確認する
docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
cc02297dc8f1        amazonlinux         "/bin/bash"         About a minute ago   Up About a minute                       peaceful_ritchie

起動したコンテナにアクセスする。

$ docker attach cc02297dc8f1
bash-4.2# 

# exitコマンドを打つとコンテナを停止して終了
bash-4.2# exit
exit

先ほど作成したコンテナを起動して今度は終了させないで抜けてみる。

$ docker start cc02297dc8f1
cc02297dc8f1

# もう一度アクセス
$ docker attach cc02297dc8f1
bash-4.2# 


# Control + p + q

# コンテナが起動中であることを確認する

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
cc02297dc8f1        amazonlinux         "/bin/bash"         19 minutes ago      Up 3 minutes                            peaceful_ritchie

コンテナを終了する。

$ docker stop cc02297dc8f1
cc02297dc8f1
1
5
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
5