LoginSignup
2
1

More than 1 year has passed since last update.

CentOS8に「docker-compose」をインストールする方法

Posted at

CentOS8docker-compose をインストールしたので、その手順を紹介します。

1. 環境

  • OS:CentOS Linux release 8.5.2111
  • Docker:Docker version 20.10.14, build a224086
[root@centos8 work1]# cat /etc/redhat-release
CentOS Linux release 8.5.2111
[root@centos8 work1]# docker -v
Docker version 20.10.14, build a224086
[root@centos8 work1]#

2. インストール手順

docker-compose をインストールするには、事前に Docker をインストールしておく必要があります。
Docker のインストール方法は「CentOS8にDockerをインストールする方法」を参照してください。

 

以下のコマンドで、docker-compose をダウンロードします。
curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

実行結果
[root@centos8 ~]# curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   664  100   664    0     0   2243      0 --:--:-- --:--:-- --:--:--  2243
100 8648k  100 8648k    0     0  5025k      0  0:00:01  0:00:01 --:--:-- 17.3M
[root@centos8 ~]#

 

ダウンロードした「/usr/local/bin/docker-compose」に、以下のコマンドで実行権限を付与します。
chmod +x /usr/local/bin/docker-compose

実行結果
[root@centos8 ~]# chmod +x /usr/local/bin/docker-compose
[root@centos8 ~]#

docker-compose -vのコマンドで、インストールされたdocker-compose のバージョンを確認します。

実行結果
[root@centos8 ~]# docker-compose -v
docker-compose version 1.16.1, build 6d1ac21
[root@centos8 ~]#

3. 動作確認

docker-compose の動作確認のため、「Docker Compose - とほほのWWW入門」に紹介されている、チュートリアルを実行してみました。

ワークディレクトリを作成し、ワークディレクトリに移動します。
mkdir work1
cd work1

実行結果
[root@centos8 ~]# pwd
/root
[root@centos8 ~]# mkdir work1
[root@centos8 ~]# cd work1

 

作成したワークディレクトリ(/root/work1)配下に、以下の4ファイルを作成します。

  • app.py
  • requirements.txt
  • Dockerfile
  • docker-compose.yml

内容はそれぞれ以下の通りです。

/root/work1/app.py
from flask import Flask
from redis import Redis

app = Flask(__name__)
redis = Redis(host='redis', port=6379)

@app.route('/')
def hello():
    redis.incr('hits')
    return 'Hello World! I have been seen %s times.\n' % redis.get('hits').decode()

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)
/root/work1/requirements.txt
flask
redis
/root/work1/Dockerfile
FROM python:3.6
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD python app.py
/root/work1/docker-compose.yml
version: '2'
services:
  web:
    build: .
    ports:
     - "5000:5000"
    volumes:
     - .:/code
    depends_on:
     - redis
  redis:
    image: redis
ファイル確認
[root@centos8 work1]# pwd
/root/work1
[root@centos8 work1]# ls -l
合計 16
-rw-r--r-- 1 root root  96  3月 26 17:46 Dockerfile
-rw-r--r-- 1 root root 315  3月 26 17:45 app.py
-rw-r--r-- 1 root root 156  3月 26 17:46 docker-compose.yml
-rw-r--r-- 1 root root  12  3月 26 17:45 requirements.txt
[root@centos8 work1]#

 

事前に Dockerを起動しておきます。
systemcrl start dcker

実行結果
[root@centos8 work1]# systemctl start docker
[root@centos8 work1]#

 

以下の docker-compose のコマンドで、使用するコンテナを起動します。
docker-compose up -d

実行結果
実行結果
[root@centos8 work1]# docker-compose up -d
Creating network "work1_default" with the default driver
Pulling redis (redis:latest)...
latest: Pulling from library/redis
ae13dd578326: Pull complete
e6f25d21ebb3: Pull complete
601cc6106ba1: Pull complete
5b8be2fd806e: Pull complete
950c3791111a: Pull complete
567b7ad78092: Pull complete
Digest: sha256:771c20f2507b0e6b59a857abc3be09bb6764bb024db35f379ada9fe62ad932c5
Status: Downloaded newer image for redis:latest
Building web
Step 1/5 : FROM python:3.6
3.6: Pulling from library/python
0e29546d541c: Pull complete
9b829c73b52b: Pull complete
cb5b7ae36172: Pull complete
6494e4811622: Pull complete
6f9f74896dfa: Pull complete
5e3b1213efc5: Pull complete
9fddfdc56334: Pull complete
404f02044bac: Pull complete
c4f42be2be53: Pull complete
Digest: sha256:f8652afaf88c25f0d22354d547d892591067aa4026a7fa9a6819df9f300af6fc
Status: Downloaded newer image for python:3.6
 ---> 54260638d07c
Step 2/5 : ADD . /code
 ---> a4d9270f0a92
Step 3/5 : WORKDIR /code
 ---> Running in 49a192ba04f6
Removing intermediate container 49a192ba04f6
 ---> a79afefccc97
Step 4/5 : RUN pip install -r requirements.txt
 ---> Running in b7708247066d
Collecting flask
  Downloading Flask-2.0.3-py3-none-any.whl (95 kB)
Collecting redis
  Downloading redis-4.2.0-py3-none-any.whl (225 kB)
Collecting itsdangerous>=2.0
  Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Collecting Jinja2>=3.0
  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
Collecting Werkzeug>=2.0
  Downloading Werkzeug-2.0.3-py3-none-any.whl (289 kB)
Collecting click>=7.1.2
  Downloading click-8.0.4-py3-none-any.whl (97 kB)
Collecting typing-extensions
  Downloading typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Collecting async-timeout>=4.0.2
  Downloading async_timeout-4.0.2-py3-none-any.whl (5.8 kB)
Collecting packaging>=20.4
  Downloading packaging-21.3-py3-none-any.whl (40 kB)
Collecting deprecated>=1.2.3
  Downloading Deprecated-1.2.13-py2.py3-none-any.whl (9.6 kB)
Collecting importlib-metadata>=1.0
  Downloading importlib_metadata-4.8.3-py3-none-any.whl (17 kB)
Collecting wrapt<2,>=1.10
  Downloading wrapt-1.14.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (74 kB)
Collecting zipp>=0.5
  Downloading zipp-3.6.0-py3-none-any.whl (5.3 kB)
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)
Collecting pyparsing!=3.0.5,>=2.0.2
  Downloading pyparsing-3.0.7-py3-none-any.whl (98 kB)
Collecting dataclasses
  Downloading dataclasses-0.8-py3-none-any.whl (19 kB)
Installing collected packages: zipp, typing-extensions, wrapt, pyparsing, MarkupSafe, importlib-metadata, dataclasses, Werkzeug, packaging, Jinja2, itsdangerous, deprecated, click, async-timeout, redis, flask
Successfully installed Jinja2-3.0.3 MarkupSafe-2.0.1 Werkzeug-2.0.3 async-timeout-4.0.2 click-8.0.4 dataclasses-0.8 deprecated-1.2.13 flask-2.0.3 importlib-metadata-4.8.3 itsdangerous-2.0.1 packaging-21.3 pyparsing-3.0.7 redis-4.2.0 typing-extensions-4.1.1 wrapt-1.14.0 zipp-3.6.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container b7708247066d
 ---> 04577ccd9c5e
Step 5/5 : CMD python app.py
 ---> Running in af82aa21e7de
Removing intermediate container af82aa21e7de
 ---> d397ba8eaec3
Successfully built d397ba8eaec3
Successfully tagged work1_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating work1_redis_1 ...
Creating work1_redis_1 ... done
Creating work1_web_1 ...
Creating work1_web_1 ... done

 

以下のコマンドで docker の状態を確認します。
docker ps
docker ps -a
docker images
docker network ls
docker volume ls

実行結果
[root@centos8 work1]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                       NAMES
87e7d488f9fa   work1_web   "/bin/sh -c 'python …"   2 minutes ago   Up 2 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   work1_web_1
f41dc67963db   redis       "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   6379/tcp                                    work1_redis_1
[root@centos8 work1]# docker ps -a
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                       NAMES
87e7d488f9fa   work1_web   "/bin/sh -c 'python …"   2 minutes ago   Up 2 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   work1_web_1
f41dc67963db   redis       "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   6379/tcp                                    work1_redis_1
[root@centos8 work1]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
work1_web    latest    d397ba8eaec3   2 minutes ago   916MB
redis        latest    87c26977fd90   8 days ago      113MB
python       3.6       54260638d07c   3 months ago    902MB
[root@centos8 work1]# docker network ls
NETWORK ID     NAME            DRIVER    SCOPE
9fb67fd1621b   bridge          bridge    local
6357fa5f6a07   host            host      local
a144f0d6a794   none            null      local
12816e60b028   work1_default   bridge    local
[root@centos8 work1]# docker volume ls
DRIVER    VOLUME NAME
local     7661007c9b943a9c229e556753bb84029c368c167d6203a7642bc95fabb8614c
[root@centos8 work1]#

 

curlコマンドでhttp://localhost:5000/にアクセスし、動作を確認します。
curl http://localhost:5000/

実行結果
[root@centos8 work1]# curl http://localhost:5000/
Hello World! I have been seen 1 times.
[root@centos8 work1]# curl http://localhost:5000/
Hello World! I have been seen 2 times.
[root@centos8 work1]# curl http://localhost:5000/
Hello World! I have been seen 3 times.
[root@centos8 work1]# curl http://localhost:5000/
Hello World! I have been seen 4 times.
[root@centos8 work1]# curl http://localhost:5000/
Hello World! I have been seen 5 times.
[root@centos8 work1]#

Docker Compose - とほほのWWW入門」の チュートリアル の通り、上手く動作しました。

 

以下のコマンドでdocker-compose up -dで起動したコンテナを終了させます。
docker-compose down

実行結果
[root@centos8 work1]# docker-compose down
Stopping work1_web_1   ... done
Stopping work1_redis_1 ... done
Removing work1_web_1   ... done
Removing work1_redis_1 ... done
Removing network work1_default
[root@centos8 work1]#

 

再度、以下のコマンドで docker の状態を確認します。
docker ps
docker ps -a
docker images
docker network ls
docker volume ls

実行結果
[root@centos8 work1]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@centos8 work1]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@centos8 work1]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
work1_web    latest    d397ba8eaec3   6 minutes ago   916MB
redis        latest    87c26977fd90   8 days ago      113MB
python       3.6       54260638d07c   3 months ago    902MB
[root@centos8 work1]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
9fb67fd1621b   bridge    bridge    local
6357fa5f6a07   host      host      local
a144f0d6a794   none      null      local
[root@centos8 work1]# docker volume ls
DRIVER    VOLUME NAME
local     7661007c9b943a9c229e556753bb84029c368c167d6203a7642bc95fabb8614c
[root@centos8 work1]#

docker ps -aの結果をみると、コンテナが終了して、さらに削除されていることがわかります。
docker imagesの結果をみると、イメージは残っています。

参考

Docker Compose - とほほのWWW入門


以上

2
1
1

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