[Docker] docker-compose로 flask, mysql 서버 띄우기

이수진·2022년 1월 31일
0

오늘은 하나 말고 여러개의 컨테이너를 동시에 띄워볼거에요!
flask와 mysql 서버를 동시에 띄워보도록 하겠습니다.

먼저, 제 폴더구조는 다음과 같습니다.

├── FLASK_MYSQL
    ├── docker-compose.yml
    ├── 01_dir
        ├── Dockerfile
        ├── main.py
        ├── .dockerignore

먼저, docker-compose.yml 파일은 다음과 같습니다.

version: "3"

services:
  app: # 컨테이너1 : app
    build:
      context: ./01_dir
      dockerfile: Dockerfile
    links:
      - "db:mysqldb"
    ports:
      - "80:8080"
    container_name: app
    depends_on:
      - db

    db: # 컨테이너2 : db
    image: mysql:5.7
    volumes:
      - ./dbdata:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=mydb
    ports:
      - "3306:3306"
    container_name: db

두 개의 컨테이너 (app컨테이너 그리고 db컨테이너)를 동시에 띄워 볼 것이구요.
flask에 대한 설정과 세팅은 Dockerfile과 main.py에서 작성해두었습니다.

그러면 작성한 docker-compose.yml을 기반으로
"docker-compose up -d" 명령어를 통하여 컨테이너를 생성하고 실행해보겠습니다.

ubuntu@ip-172-31-38-68:~/FLASK_MYSQL$ docker-compose up -d
Building with native build. Learn about native build in Compose here: https://docs.docker.com/go/compose-native-build/
Creating network "flask_mysql_default" with the default driver
Pulling db (mysql:5.7)...
5.7: Pulling from library/mysql
6552179c3509: Pull complete
d69aa66e4482: Pull complete
3b19465b002b: Pull complete
7b0d0cfe99a1: Pull complete
9ccd5a5c8987: Pull complete
2dab00d7d232: Pull complete
64d3afdccd4a: Pull complete
6992e58be0f2: Pull complete
67313986b81d: Pull complete
7c36a23db0a4: Pull complete
d34c396e3198: Pull complete
Digest: sha256:afc453de0d675083ac00d0538521f8a9a67d1cce180d70fab9925ebcc87a0eba
Status: Downloaded newer image for mysql:5.7
Building app
Sending build context to Docker daemon  4.608kB

Step 1/5 : FROM continuumio/miniconda
latest: Pulling from continuumio/miniconda
b8f262c62ec6: Pulling fs layer
fa9712f20293: Pulling fs layer
6ec1e76960c6: Pulling fs layer
b8f262c62ec6: Verifying Checksum
b8f262c62ec6: Download complete
6ec1e76960c6: Verifying Checksum
6ec1e76960c6: Download complete
fa9712f20293: Verifying Checksum
fa9712f20293: Download complete
b8f262c62ec6: Pull complete
fa9712f20293: Pull complete
6ec1e76960c6: Pull complete
Digest: sha256:fee1354ae2435522b9a8a79c5f1c406facc07ec5c44d730d8053600b37c924f0
Status: Downloaded newer image for continuumio/miniconda:latest
 ---> b8ea69b5c41c
Step 2/5 : COPY ./ /app
 ---> 26fb6b1d7c92
Step 3/5 : WORKDIR /app
 ---> Running in b2b9785e8156
Removing intermediate container b2b9785e8156
 ---> d13174dbdf21
Step 4/5 : RUN pip install flask pymysql cryptography
 ---> Running in e2c59e1cec27
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting flask
  Downloading https://files.pythonhosted.org/packages/e8/6d/994208daa354f68fd89a34a8bafbeaab26fda84e7af1e35bdaed02b667e6/Flask-1.1.4-py2.py3-none-any.whl (94kB)
Collecting pymysql
  Downloading https://files.pythonhosted.org/packages/1a/ea/dd9c81e2d85efd03cfbf808736dd055bd9ea1a78aea9968888b1055c3263/PyMySQL-0.10.1-py2.py3-none-any.whl (47kB)
Requirement already satisfied: cryptography in /opt/conda/lib/python2.7/site-packages (2.7)
Collecting click<8.0,>=5.1 (from flask)
  Downloading https://files.pythonhosted.org/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl (82kB)
Collecting itsdangerous<2.0,>=0.24 (from flask)
  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2<3.0,>=2.10.1 (from flask)
  Downloading https://files.pythonhosted.org/packages/7e/c2/1eece8c95ddbc9b1aeb64f5783a9e07a286de42191b7204d67b7496ddf35/Jinja2-2.11.3-py2.py3-none-any.whl (125kB)
Collecting Werkzeug<2.0,>=0.15 (from flask)
  Downloading https://files.pythonhosted.org/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl (298kB)
Requirement already satisfied: enum34; python_version < "3" in /opt/conda/lib/python2.7/site-packages (from cryptography) (1.1.6)
Requirement already satisfied: asn1crypto>=0.21.0 in /opt/conda/lib/python2.7/site-packages (from cryptography) (1.0.1)
Requirement already satisfied: cffi!=1.11.3,>=1.8 in /opt/conda/lib/python2.7/site-packages (from cryptography) (1.12.3)
Requirement already satisfied: six>=1.4.1 in /opt/conda/lib/python2.7/site-packages (from cryptography) (1.12.0)
Requirement already satisfied: ipaddress; python_version < "3" in /opt/conda/lib/python2.7/site-packages (from cryptography) (1.0.22)
Collecting MarkupSafe>=0.23 (from Jinja2<3.0,>=2.10.1->flask)
  Downloading https://files.pythonhosted.org/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Requirement already satisfied: pycparser in /opt/conda/lib/python2.7/site-packages (from cffi!=1.11.3,>=1.8->cryptography) (2.19)
Installing collected packages: click, itsdangerous, MarkupSafe, Jinja2, Werkzeug, flask, pymysql
Successfully installed Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.4 itsdangerous-1.1.0 pymysql-0.10.1
Removing intermediate container e2c59e1cec27
 ---> 61be07f40173
Step 5/5 : CMD ["python", "main.py"]
 ---> Running in 973b435b6ee8
Removing intermediate container 973b435b6ee8
 ---> 51701c0fce0e
Successfully built 51701c0fce0e
Successfully tagged flask_mysql_app:latest
WARNING: Image for service app was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating db ... done
Creating app ... done

터미널 마지막에서 db와 app 컨테이너가 잘 생성되었음을 확인할 수 있습니다.
이어서 만들어진 이미지와 실행되는 컨테이너를 확인하면 다음과 같습니다.

ubuntu@ip-172-31-38-68:~/FLASK_MYSQL$ docker images
REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
flask_mysql_app         latest    51701c0fce0e   10 seconds ago   415MB
mysql                   5.7       0712d5dc1b14   4 days ago       448MB
continuumio/miniconda   latest    b8ea69b5c41c   2 years ago      410MB
ubuntu@ip-172-31-38-68:~/FLASK_MYSQL$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
bf379be8028e   flask_mysql_app   "python main.py"         15 seconds ago   Up 14 seconds   0.0.0.0:80->8080/tcp, :::80->8080/tcp                  app
fe943c8b9693   mysql:5.7         "docker-entrypoint.s…"   16 seconds ago   Up 15 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   db

다음번에는 django와 mysql 컨테이너도 함께 띄워보는것을 연습해보도록 하겠습니다 ㅎㅎ

profile
꾸준히, 열심히, 그리고 잘하자

0개의 댓글