labunix's blog

labunixのラボUnix

debian stretchにdockerを導入する。

■debian stretchにdockerを導入する。

 How to install Docker engine on Debian 9 Stretch Linux
 https://linuxconfig.org/how-to-install-docker-engine-on-debian-9-stretch-linux

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 9.3 (stretch)
Release:	9.3
Codename:	stretch

■dockerリポジトリを登録してインストール。
 プロキシ経由でない場合は、「--keyserver-option」行は不要。

$ sudo apt-get install -y apt-transport-https dirmngr

$ w3m -dump https://apt.dockerproject.org/repo/dists/ | awk '/debian/'
debian-jessie/                              12-Apr-2017 19:11  -
debian-stretch/                             12-Apr-2017 19:11  -
debian-wheezy/                              12-Apr-2017 19:11  -

$ echo deb https://apt.dockerproject.org/repo debian-stretch main | \
    sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 \
    --keyserver-option http-proxy=http://172.31.31.60:8080/ \
    --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
Executing: /tmp/apt-key-gpghome.rJ8CAEWXKO/gpg.1.sh --keyserver hkp://p80.pool.sks-keyservers.net:80 \
  --keyserver-option http-proxy=http://172.31.31.60:8080/ --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
gpg: 鍵F76221572C52609D: 公開鍵"Docker Release Tool (releasedocker) <docker@docker.com>"をインポートしました
gpg:           処理数の合計: 1
gpg:             インポート: 1

$ sudo apt-get install -y docker-engine

$ docker -v
Docker version 17.05.0-ce, build 89658be

$ sudo docker version
Client:
 Version:      17.05.0-ce
 API version:  1.29
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:09:06 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.05.0-ce
 API version:  1.29 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:09:06 2017
 OS/Arch:      linux/amd64
 Experimental: false

■DockerHubからDockerイメージをダウンロード

 Control and configure Docker with systemd
 https://docs.docker.com/engine/admin/systemd/#manually-create-the-systemd-unit-files

$ awk '/proxy/' /etc/default/docker
# If you need Docker to use an HTTP proxy, it can also be specified here.
#export http_proxy="http://127.0.0.1:3128/"

$ sudo sed -i 's%.*http_proxy.*%&\nexport http_proxy="http://172.31.31.60:8080/"%' /etc/default/docker

$ dpkg -L docker-engine | awk '/service$/'
/lib/systemd/system/docker.service

$ dpkg -L docker-engine | awk '/service$/{print "cp "$1" /etc/systemd/system/"}' | sudo sh

$ sudo sed -i 's%\[Service\]%&\nEnvironment="HTTP_PROXY=http://172.31.31.60:8080"%' /etc/systemd/system/docker.service

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker.service 

$ sudo systemctl show --property Environment docker
Environment=HTTP_PROXY=http://172.31.31.60:8080

$ sudo docker search httpd | awk '/NAME|^httpd/'
NAME                                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
httpd                               The Apache HTTP Server Project                  1412      [OK]       

$ sudo docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
f49cf87b52c1: Pull complete 
02ca099fb6cd: Pull complete 
de7acb18da57: Pull complete 
770c8edb393d: Pull complete 
0e252730aeae: Pull complete 
6e6ca341873f: Pull complete 
2daffd0a6144: Pull complete 
Digest: sha256:b5f21641a9d7bbb59dc94fb6a663c43fbf3f56270ce7c7d51801ac74d2e70046
Status: Downloaded newer image for httpd:latest

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               latest              7239615c0645        2 weeks ago         177MB

$ sudo docker run -ti httpd /bin/bash
root@6f32436cde9b:/usr/local/apache2# exit
exit

■コンテナの起動中にホストに戻る

$ sudo docker run -ti httpd /bin/bash
[Ctrl]+p [Ctrl]+q

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
ff553f5901f3        httpd               "/bin/bash"         About a minute ago   Up About a minute   80/tcp              dreamy_pike

$ sudo docker attach ff553f5901f3
root@ff553f5901f3:/usr/local/apache2# exit
exit