KAEDE Hack blog

kaede0902 技術ブログ お仕事募集中。

Docker compose で postgres + adminer を動かす (stack swarm を削除して)

why

[qiita stack aritcle]

  • 前回間違えて stack.yml 書いて docker stack deploy で swarm で動かしてた
  • 今回はまず compose.yml 作って compose up でうごかす

code

# Use postgres/example user/password credentials
version: '3.1'

services:

  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: example

  adminer:
    image: adminer
    restart: always
    ports:
  • 以前の記事で使っていた stack.yml を名前を変えて転用

https://hub.docker.com/_/postgres

  • ここのコード
docker-compose up                                                                                                
WARNING: The Docker Engine you're using is running in swarm mode.                                                                                
                                                                                                                                                 
Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.           
                                                                                                                                                 
To deploy your application across the swarm, use `docker stack deploy`.                                                                          
                                                                                                                                                 
Creating network "docker-postgres_default" with the default driver 

db_1       | PostgreSQL init process complete; ready for start up.

db_1       | 2020-10-12 10:32:37.843 UTC [1] LOG:  database system is ready to accept connections
  • 同じように adminer が 8080 port で動いた
  • しかし swarm mode になっている
  • プロセスを確認してみる
docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
5b17ccd96941        adminer             "entrypoint.sh docke…"   3 minutes ago       Up 3 minutes        0.0.0.0:8080->8080/tcp   docker-postgres_adminer_1
512f1dced481        postgres            "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        5432/tcp                 docker-postgres_db_1
  • adminer, postgres, が動いているように見える
  • stack をみてみる
docker stack ls
NAME                SERVICES            ORCHESTRATOR
  • stack は動いてない
  • swarm が動いてるのか?
docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
tem7xgdatnbha4bfq7rwdglsp *   docker-desktop      Ready               Active              Leader              19.03.12
  • swarm ls とかは見つからないから node ls した。デスクトップのアプリが動いてる
docker network ls
NETWORK ID          NAME                      DRIVER              SCOPE
9c5a8f439579        bridge                    bridge              local
e84d363bfb37        docker-postgres_default   bridge              local
c782587be9fa        docker_gwbridge           bridge              local
ca04d2ac4141        host                      host                local
zp84rl1ybyb2        ingress                   overlay             swarm
ccb78c451e9e        nextjs-blog_default       bridge              local
87eb957ef9f8        none                      null                local
  • network のリストをみると doker_postgres_default の DB は bridge になってる
  • つまり swarm は動いてない、さっきのはなに?

  • swarm を調べると docker service ls でみるようにしか出てこない

質問

docker-compose up で起動した adminer + postgres の コンテナで

WARNING: The Docker Engine you're using is running in swarm mode.

と出てしまいます

https://docs.docker.com/engine/swarm/manage-nodes/#leave-the-swarm

docker swarm leave --force                                                                                       
Node left the swarm.        
                                                                                                                      docker-compose up                                                                                                
Starting docker-postgres_db_1      ... done                                                                                                      
Starting docker-postgres_adminer_1 ... done                                                                                                      
Attaching to docker-postgres_db_1, docker-postgres_adminer_1
adminer_1  | [Mon Oct 12 10:50:37 2020] PHP 7.4.11 Development Server (http://[::]:8080) started

stack で作ったものは stack rm {name} で消えました!

docker stack rm postgres
Removing service postgres_adminer
Removing service postgres_db
Removing network postgres_default
RYOs-MBP:nextjs-blog kaede$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

もう一度動かす

docker-compose up
Starting docker-postgres_db_1      ... done
Starting docker-postgres_adminer_1 ... done
Attaching to docker-postgres_db_1, docker-postgres_adminer_1
db_1       | 
db_1       | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1       | 
db_1       | 2020-10-29 08:20:28.979 UTC [1] LOG:  starting PostgreSQL 13.0 (Debian 13.0-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1       | 2020-10-29 08:20:28.979 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1       | 2020-10-29 08:20:28.979 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1       | 2020-10-29 08:20:28.983 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1       | 2020-10-29 08:20:28.990 UTC [25] LOG:  database system was shut down at 2020-10-13 16:28:26 UTC
db_1       | 2020-10-29 08:20:29.000 UTC [1] LOG:  database system is ready to accept connections
adminer_1  | [Thu Oct 29 08:20:29 2020] PHP 7.4.11 Development Server (http://[::]:8080) started
  • ちゃんと動いてそう

Screen Shot 2020-10-29 at 17.21.28.png

  • ホストの 8080 で開いた

Screen Shot 2020-10-29 at 17.22.17.png

  • login できた

Screen Shot 2020-10-29 at 17.23.43.png

  • 新しいテーブルも作れた

Next

  • 次は MySQL で最初からやってみる
docker-compose down
Removing docker-postgres_adminer_1 ... done
Removing docker-postgres_db_1      ... done
Removing network docker-postgres_default