Thursday 9 June 2022

Docker Container| Convert an Application Deployment into a Stack


sudo docker node ls

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION

kj7q08sfk4ojxq9cq9ezejk78     ip-172-31-0-113     Ready               Active                                  19.03.12

64z9yjpcsg3wu0yivbxfjkasd     ip-172-31-8-58      Ready               Active                                  19.03.12

o4adqybqntezkuluy709304os *   ip-172-31-15-10     Ready               Active              Leader              19.03.12


sudo docker node update --availability drain ip-172-31-0-113

ip-172-31-0-113


sudo docker node update --availability drain ip-172-31-8-58

ip-172-31-8-58


sudo docker node ls

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION

kj7q08sfk4ojxq9cq9ezejk78     ip-172-31-0-113     Ready               Drain                                   19.03.12

64z9yjpcsg3wu0yivbxfjkasd     ip-172-31-8-58      Ready               Drain                                   19.03.12

o4adqybqntezkuluy709304os *   ip-172-31-15-10     Ready               Active              Leader              19.03.12


sudo docker service create --name registry --publish published=5000,target=5000 registry:2

zfm3gl51q2n1kaq8d2e2fyqn4

overall progress: 1 out of 1 tasks 

1/1: running   [==================================================>] 

verify: Service converged 


sudo docker service ls

ID                  NAME                  MODE                REPLICAS            IMAGE                           PORTS

zfm3gl51q2n1        registry              replicated          1/1                 registry:2                      *:5000->5000/tcp


curl http://localhost:5000/v2/

{}


mkdir stackdemo && cd stackdemo

ls

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


cat app.py 

from flask import Flask

from redis import Redis

app = Flask(__name__)

redis = Redis(host='redis', port=6379)

@app.route('/')

def hello():

    count = redis.incr('hits')

    return 'Hello World! I have been seen {} times.\n'.format(count)

if __name__ == "__main__":

    app.run(host="0.0.0.0", port=8000, debug=True) 


cat requirements.txt 

flask

redis


cat Dockerfile 

FROM python:3.4-alpine

ADD . /code

WORKDIR /code

RUN pip install -r requirements.txt

CMD ["python", "app.py"]


cat docker-compose.yml 

version: "3.3"

services:

  web:

    image: 127.0.0.1:5000/stackdemo

    build: .

    ports:

      - "8000:8000"

  redis:

    image: redis:alpine


sudo curl -L "https://github.com/docker/compose/releases/download/1.29.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

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

100 12.1M  100 12.1M    0     0  6963k      0  0:00:01  0:00:01 --:--:-- 10.4M


sudo chmod +x /usr/local/bin/docker-compose

docker-compose --version

docker-compose version 1.29.1, build c34c88b2


sudo docker-compose up -d

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 "stackdemo_default" with the default driver

Building web

Sending build context to Docker daemon   5.12kB

Step 1/5 : FROM python:3.4-alpine

3.4-alpine: Pulling from library/python

8e402f1a9c57: Pull complete 

cda9ba2397ef: Pull complete 

aafecf9bbbfd: Pull complete 

bc2e7e266629: Pull complete 

e1977129b756: Pull complete 

Digest: sha256:c210b660e2ea553a7afa23b41a6ed112f85dbce25cbcb567c75dfe05342a4c4b

Status: Downloaded newer image for python:3.4-alpine

 ---> c06adcf62f6e

Step 2/5 : ADD . /code

 ---> 1bc3f7ccf727

Step 3/5 : WORKDIR /code

 ---> Running in 6b9a22b89015

Removing intermediate container 6b9a22b89015

 ---> 4c0bc5770538

Step 4/5 : RUN pip install -r requirements.txt

 ---> Running in 8a7d7770125a

<..>

Removing intermediate container 8a7d7770125a

 ---> 5bf437ad4a63

Step 5/5 : CMD ["python", "app.py"]

 ---> Running in d182530cfeea

Removing intermediate container d182530cfeea

 ---> e551384be1ef

Successfully built e551384be1ef

Successfully tagged 127.0.0.1:5000/stackdemo: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`.

Pulling redis (redis:alpine)...

alpine: Pulling from library/redis

2408cc74d12b: Already exists

e90389148883: Pull complete

c6c08b6ea4d5: Pull complete

665852243e1b: Pull complete

be2f822ab555: Pull complete

f663420da992: Pull complete

Digest: sha256:6bb3ff43923274d6d0f601b5efd5a92b724ac8af2214a8a6864bf5fb9f82df8e

Status: Downloaded newer image for redis:alpine

Creating stackdemo_redis_1 ... done

Starting stackdemo_web_1 ... done


sudo docker-compose ps

      Name                     Command               State           Ports         

-----------------------------------------------------------------------------------

stackdemo_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp              

stackdemo_web_1     python app.py                    Up      0.0.0.0:8000->8000/tcp


curl http://localhost:8000

Hello World! I have been seen 1 times.


sudo docker-compose down --volumes

Stopping stackdemo_web_1   ... done

Stopping stackdemo_redis_1 ... done

Removing stackdemo_web_1   ... done

Removing stackdemo_redis_1 ... done

Removing network stackdemo_default


sudo docker-compose push

Pushing web (127.0.0.1:5000/stackdemo:latest)...

The push refers to repository [127.0.0.1:5000/stackdemo]

1be3f680d745: Pushed

73fa649c0258: Pushed

62de8bcc470a: Pushed

58026b9b6bf1: Pushed

fbe16fc07f0d: Pushed

aabe8fddede5: Pushed

bcf2f368fe23: Pushed

latest: digest: sha256:cd9c62b1ba9d171853f1a3f714cb712cabd8f3a9682f53d9c895a1929637fd2f size: 1786


sudo docker stack deploy --compose-file docker-compose.yml stackdemo

Ignoring unsupported options: build

Creating network stackdemo_default

Creating service stackdemo_web

Creating service stackdemo_redis


sudo docker stack services stackdemo

ID                  NAME                MODE                REPLICAS            IMAGE                             PORTS

uwpl7myiasq0        stackdemo_web       replicated          1/1                 127.0.0.1:5000/stackdemo:latest   *:8000->8000/tcp

wq6j5lpm14wv        stackdemo_redis     replicated          1/1                 redis:alpine               


curl http://172.31.15.10:8000

Hello World! I have been seen 4 times.


cat /etc/hosts

172.31.15.10 localhost


curl http://localhost:8000

Hello World! I have been seen 5 times.


sudo docker service ls

ID                  NAME                MODE                REPLICAS            IMAGE                             PORTS

zfm3gl51q2n1        registry            replicated          1/1                 registry:2                        *:5000->5000/tcp

wq6j5lpm14wv        stackdemo_redis     replicated          1/1                 redis:alpine                      

uwpl7myiasq0        stackdemo_web       replicated          1/1                 127.0.0.1:5000/stackdemo:latest   *:8000->8000/tcp


sudo docker node ls

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION

kj7q08sfk4ojxq9cq9ezejk78     ip-172-31-0-113     Ready               Drain                                   19.03.12

64z9yjpcsg3wu0yivbxfjkasd     ip-172-31-8-58      Ready               Drain                                   19.03.12

o4adqybqntezkuluy709304os *   ip-172-31-15-10     Ready               Active              Leader              19.03.12


sudo docker node update --availability active ip-172-31-0-113

ip-172-31-0-113


sudo docker node update --availability active ip-172-31-8-58

ip-172-31-8-58


sudo docker node ls

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION

kj7q08sfk4ojxq9cq9ezejk78     ip-172-31-0-113     Ready               Active                                  19.03.12

64z9yjpcsg3wu0yivbxfjkasd     ip-172-31-8-58      Ready               Active                                  19.03.12

o4adqybqntezkuluy709304os *   ip-172-31-15-10     Ready               Active              Leader              19.03.12


sudo docker stack rm stackdemo

Removing service stackdemo_redis

Removing service stackdemo_web

Removing network stackdemo_default


sudo docker service rm zfm3gl51q2n1

zfm3gl51q2n1


No comments:

Post a Comment