Docker Compose

λ©€ν‹° μ»¨ν…Œμ΄λ„ˆ ν™˜κ²½

  1. 데λͺ¨ μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ†ŒμŠ€μ½”λ“œ λ‹€μš΄λ‘œλ“œ

    git clone https://github.com/youngwjung/to-do.git && cd to-do
  2. μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ†ŒμŠ€ μ½”λ“œ 및 README 리뷰

  3. Dockerfile 생성

    cat <<EOF | tee frontend/Dockerfile
    FROM nginx:alpine
    COPY . /usr/share/nginx/html
    EOF
  4. ν”„λ‘ νŠΈμ—”λ“œ μ»¨ν…Œμ΄λ„ˆ 이미지 생성

    docker image build -t frontend frontend/
  5. ν”„λ‘ νŠΈμ—”λ“œ μ»¨ν…Œμ΄λ„ˆ 생성

    docker container run --name frontend -d -p 8080:80 frontend
  6. μ›ΉλΈŒλΌμš°μ €μ—μ„œ μ»¨ν…Œμ΄λ„ˆλ‘œ μ ‘κ·Όλ˜λŠ”μ§€ 확인 - μ•„λž˜ λͺ…λ Ήμ–΄λ‘œ μ£Όμ†Œ 확인 κ°€λŠ₯

    echo "$(curl -s ifconfig.io):8080"
  7. Dockerfile λ³€κ²½

    cat <<'EOF' | tee frontend/Dockerfile
    FROM nginx:alpine
    COPY . /usr/share/nginx/html
    ARG API_ENDPOINT
    RUN sed -i 's/localhost:9000/'"$API_ENDPOINT"'/g' /usr/share/nginx/html/js/main.js
    EOF
  8. ν”„λ‘ νŠΈμ—”λ“œ μ»¨ν…Œμ΄λ„ˆ 이미지 μž¬μƒμ„±

    docker image build --no-cache -t frontend \
    --build-arg API_ENDPOINT=$(curl -s ifconfig.io):9000 \
    frontend/
  9. ν”„λ‘ νŠΈμ—”λ“œ μ»¨ν…Œμ΄λ„ˆ μž¬μƒμ„±

    {
        docker container rm frontend --force
        docker container run --name frontend -d -p 8080:80 frontend
    }
  10. μ›Ή λΈŒλΌμš°μ €μ—μ„œ λ‘œλ”©λœ νŽ˜μ΄μ§€ λ¦¬ν”„λ ˆμ‹œ

  11. Dockerfile 생성

    cat <<EOF | tee api/Dockerfile
    FROM golang:1.20-alpine as builder
    WORKDIR /app
    COPY go.mod go.sum ./
    RUN go mod download
    COPY *.go ./
    RUN go build -o /api
    
    FROM alpine
    RUN mkdir /app
    WORKDIR /app
    COPY --from=builder /api /app/api
    EXPOSE 9000
    CMD ["/app/api"]
    EOF
  12. API μ»¨ν…Œμ΄λ„ˆ 이미지 생성

    docker image build -t api api/
  13. API μ»¨ν…Œμ΄λ„ˆ 생성

    docker container run --name api -d -p 9000:9000 api
  14. API μ»¨ν…Œμ΄λ„ˆ μƒνƒœ 확인

    docker container ls -a
  15. API μ»¨ν…Œμ΄λ„ˆ 둜그 확인

    docker logs api
  16. MySQL μ»¨ν…Œμ΄λ„ˆ 생성

    docker container run --name mysql -d -p 3306:3306 \
    -e MYSQL_ROOT_PASSWORD=asdf1234 mysql:5.7
  17. λ°μ΄ν„°λ² μ΄μŠ€ μŠ€ν‚€λ§ˆ 생성

    mysql -h 127.0.0.1 -u root -pasdf1234 < database/schema.sql
  18. API μ»¨ν…Œμ΄λ„ˆ μž¬μƒμ„±

    {
        docker container rm api --force
        docker container run --name api -d -p 9000:9000 \
        -e DATABASE_HOST=$(docker container inspect mysql --format '{{.NetworkSettings.IPAddress}}') \
        -e DATABASE_PASSWORD=asdf1234 \
        api
    }
  19. API μ»¨ν…Œμ΄λ„ˆ 둜그 확인

    docker logs api
  20. Redis μ»¨ν…Œμ΄λ„ˆ 생성

    docker container run --name redis -d redis
  21. API μ»¨ν…Œμ΄λ„ˆ μž¬μƒμ„±

    {
        docker container rm api --force
        docker container run --name api -d -p 9000:9000 \
        -e DATABASE_HOST=$(docker container inspect mysql --format '{{.NetworkSettings.IPAddress}}') \
        -e DATABASE_PASSWORD=asdf1234 \
        -e REDIS_HOST=$(docker container inspect redis --format '{{.NetworkSettings.IPAddress}}') \
        api
    }
  22. 싀행쀑인 μ»¨ν…Œμ΄λ„ˆ λͺ©λ‘ 확인

    docker container ls
  23. λ³΄μ•ˆκ·Έλ£Ήμ— μΈλ°”μš΄λ“œ κ·œμΉ™ μΆ”κ°€

    aws ec2 authorize-security-group-ingress \
    --group-id $(aws ec2 describe-security-groups \
    --filters Name=group-name,Values=$(curl -s http://169.254.169.254/latest/meta-data/security-groups) \
    --query "SecurityGroups[0].GroupId" --output text) \
    --protocol tcp \
    --port 9000 \
    --cidr 0.0.0.0/0 \
    --output text
  24. μ›Ή λΈŒλΌμš°μ €μ—μ„œ λ‘œλ”©λœ νŽ˜μ΄μ§€ λ¦¬ν”„λ ˆμ‹œ

  25. λ¦¬μ†ŒμŠ€ 정리

    {
        docker container rm $(docker container ls -aq) --force
        docker image prune --all --force
        docker volume prune --force
        cd ~/environment
    }

Docker Compose

  1. Docker Compose μ„€μΉ˜

    {
        sudo mkdir -p /usr/local/lib/docker/cli-plugins
        sudo curl -SL https://github.com/docker/compose/releases/download/v2.23.0/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins/docker-compose
        sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
    }
  2. Docker Compose μ„€μΉ˜ 확인

    docker compose version
  3. μ›Œλ“œν”„λ ˆμŠ€λ₯Ό ꡬ성할 Compose 파일 생성

    mkdir wordpress && cd wordpress && \
    cat <<EOF | tee docker-compose.yml
    services:
      wordpress:
        image: wordpress
    EOF
  4. Compose νŒŒμΌμ— λͺ…μ‹œλœ κ΅¬μ„±λŒ€λ‘œ μ»¨ν…Œμ΄λ„ˆ ν™˜κ²½ 생성

    docker compose up -d
  5. 싀행쀑인 Compose ν”„λ‘œμ νŠΈ λͺ©λ‘ 확인

    docker compose ls
  6. Compose 파일 μˆ˜μ •

    cat <<EOF | tee docker-compose.yml
    name: blog
    services:
      wordpress:
        image: wordpress
    EOF
  7. μˆ˜μ • 사항 반영

    docker compose up -d
  8. 싀행쀑인 Compose ν”„λ‘œμ νŠΈ λͺ©λ‘ 확인

    docker compose ls
  9. μˆ˜μ •ν•˜κΈ°μ „ λ²„μ „μ˜ Compose 파일둜 μƒμ„±λœ λ¦¬μ†ŒμŠ€λ₯Ό μˆ˜λ™μœΌλ‘œ μ‚­μ œ

    {
        docker container rm wordpress-wordpress-1 --force
        docker network rm wordpress_default
    }
  10. Composeλ₯Ό ν†΅ν•΄μ„œ 싀행쀑인 μ»¨ν…Œμ΄λ„ˆ λͺ©λ‘ 확인

    docker compose ps
  11. Compose 파일 μˆ˜μ •

    cat <<EOF | tee docker-compose.yml
    name: blog
    services:
      wordpress:
        image: wordpress
        container_name: wordpress
    EOF
  12. μˆ˜μ • 사항 반영

    docker compose up -d
  13. Composeλ₯Ό ν†΅ν•΄μ„œ 싀행쀑인 μ»¨ν…Œμ΄λ„ˆ λͺ©λ‘ 확인

    docker compose ps
  14. 싀행쀑인 λͺ¨λ“  μ»¨ν…Œμ΄λ„ˆ λͺ©λ‘ 확인

    docker container ls
  15. NGINX μ»¨ν…Œμ΄λ„ˆ 생성

    docker run --name nginx -d nginx
  16. 싀행쀑인 λͺ¨λ“  μ»¨ν…Œμ΄λ„ˆ λͺ©λ‘ 확인

    docker container ls
  17. Composeλ₯Ό ν†΅ν•΄μ„œ 싀행쀑인 μ»¨ν…Œμ΄λ„ˆ λͺ©λ‘ 확인

    docker compose ps
  18. μƒˆλ‘œμš΄ 터미널을 μ—΄κ³  Composeλ₯Ό ν†΅ν•΄μ„œ 싀행쀑인 μ»¨ν…Œμ΄λ„ˆ λͺ©λ‘ 확인

    docker compose ps
  19. 싀행쀑인 Compose ν”„λ‘œμ νŠΈ λͺ©λ‘ 확인

    docker compose ls
  20. μƒμ„±λœ λͺ¨λ“  μ»¨ν…Œμ΄λ„ˆμ— λΆ€μ—¬λœ Label 확인

    docker container inspect \
    $(docker container ps -aq) --format '{{.Name}} - {{json .Config.Labels}}'
  21. κΈ°μ‘΄ ν„°λ―Έλ„λ‘œ μ΄λ™ν•΄μ„œ NGINX μ‚­μ œ

    docker container rm nginx --force
  22. Compose 파일 μˆ˜μ •

    cat <<EOF | tee docker-compose.yml
    name: blog
    services:
      wordpress:
        image: wordpress
        container_name: wordpress
        ports:
        - 8080:80
    EOF
  23. μˆ˜μ • 사항 반영

    docker compose up -d
  24. μ›ΉλΈŒλΌμš°μ €μ—μ„œ ν¬νŠΈν¬μ›Œλ”©ν•œ 포트λ₯Ό ν†΅ν•΄μ„œ μ»¨ν…Œμ΄λ„ˆλ‘œ μ ‘κ·Όλ˜λŠ”μ§€ 확인 - μ•„λž˜ λͺ…λ Ήμ–΄λ‘œ μ£Όμ†Œ 확인 κ°€λŠ₯

    echo "$(curl -s ifconfig.io):8080"
  25. Compose 파일 μˆ˜μ •

    cat <<EOF | tee docker-compose.yml
    name: blog
    services:
      wordpress:
        image: wordpress
        container_name: wordpress
        ports:
        - 8080:80
        environment:
        - WORDPRESS_DB_HOST=mysql
        - WORDPRESS_DB_USER=wordpress
        - WORDPRESS_DB_PASSWORD=wordpress
        - WORDPRESS_DB_NAME=wordpress
        depends_on:
        - mysql
      mysql:
        image: mysql
        volumes:
        - mysql-data:/var/lib/mysql
        environment:
        - MYSQL_ROOT_PASSWORD=asdf1234
        - MYSQL_USER=wordpress
        - MYSQL_PASSWORD=wordpress
        - MYSQL_DATABASE=wordpress
    volumes:
      mysql-data:
    EOF
  26. μˆ˜μ • 사항 반영

    docker compose up -d
  27. μ›ΉλΈŒλΌμš°μ €μ—μ„œ μ›Œλ“œν”„λ ˆμŠ€ μ‚¬μ΄νŠΈ μž¬μ ‘μ† - μ•„λž˜ λͺ…λ Ήμ–΄λ‘œ μ£Όμ†Œ 확인 κ°€λŠ₯

    echo "$(curl -s ifconfig.io):8080"
  28. Compose 파일 μˆ˜μ •

    cat <<EOF | tee docker-compose.yml
    name: blog
    services:
      wordpress:
        image: wordpress
        container_name: wordpress
        volumes:
        - plugins:/var/www/html/wp-content/plugins
        ports:
        - 8080:80
        environment:
        - WORDPRESS_DB_HOST=mysql
        - WORDPRESS_DB_USER=wordpress
        - WORDPRESS_DB_PASSWORD=wordpress
        - WORDPRESS_DB_NAME=wordpress
        depends_on:
        - mysql
        - wordpress-init
      mysql:
        image: mysql
        volumes:
        - mysql-data:/var/lib/mysql
        environment:
        - MYSQL_ROOT_PASSWORD=asdf1234
        - MYSQL_USER=wordpress
        - MYSQL_PASSWORD=wordpress
        - MYSQL_DATABASE=wordpress
      wordpress-init:
        image: alpine
        volumes:
        - plugins:/plugins
        command:
        - "sh"
        - "-c"
        - "wget -O https://downloads.wordpress.org/plugin/classic-editor.1.6.3.zip && unzip -o classic-editor.1.6.3.zip -d /plugins"
    volumes:
      mysql-data:
      plugins:
    EOF
  29. μˆ˜μ • 사항 반영

    docker compose up -d
  30. ν”ŒλŸ¬κ·ΈμΈμ΄ μΆ”κ°€ λ˜μ—ˆλŠ”μ§€ 확인

  31. wp-cliλ₯Ό ν†΅ν•΄μ„œ μ›Œλ“œν”„λ ˆμŠ€ ν…Œλ§ˆ μ„€μΉ˜ μ‹œλ„

    docker compose exec wordpress wp theme install blocksy --activate --allow-root
  32. Dockerfile 생성

    cat <<EOF | tee Dockerfile
    FROM wordpress
    RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
        chmod +x wp-cli.phar && mv wp-cli.phar /usr/local/bin/wp
    EOF
  33. Compose 파일 μˆ˜μ •

    cat <<EOF | tee docker-compose.yml
    name: blog
    services:
      wordpress:
        build: .
        container_name: wordpress
        volumes:
        - plugins:/var/www/html/wp-content/plugins
        ports:
        - 8080:80
        environment:
        - WORDPRESS_DB_HOST=mysql
        - WORDPRESS_DB_USER=wordpress
        - WORDPRESS_DB_PASSWORD=wordpress
        - WORDPRESS_DB_NAME=wordpress
        depends_on:
        - mysql
        - wordpress-init
      mysql:
        image: mysql
        volumes:
        - mysql-data:/var/lib/mysql
        environment:
        - MYSQL_ROOT_PASSWORD=asdf1234
        - MYSQL_USER=wordpress
        - MYSQL_PASSWORD=wordpress
        - MYSQL_DATABASE=wordpress
      wordpress-init:
        image: alpine
        volumes:
        - plugins:/plugins
        command:
        - "sh"
        - "-c"
        - "wget -O https://downloads.wordpress.org/plugin/classic-editor.1.6.3.zip && unzip -o classic-editor.1.6.3.zip -d /plugins"
    volumes:
      mysql-data:
      plugins:
    EOF
  34. μˆ˜μ • 사항 반영

    docker compose up -d
  35. μ›Ή λΈŒλΌμš°μ €μ—μ„œ μœ„λ“œν”„λ ˆμŠ€ 메인 νŽ˜μ΄μ§€λ‘œ μ΄λ™ν•΄μ„œ ν…Œλ§ˆ 확인

  36. wp-cliλ₯Ό ν†΅ν•΄μ„œ μ›Œλ“œν”„λ ˆμŠ€ ν…Œλ§ˆ μ„€μΉ˜

    docker compose exec wordpress wp theme install blocksy --activate --allow-root
  37. μ›Ή λΈŒλΌμš°μ €μ—μ„œ μœ„λ“œν”„λ ˆμŠ€ 메인 νŽ˜μ΄μ§€λ‘œ μ΄λ™ν•΄μ„œ ν…Œλ§ˆ 확인

  38. wordpress μ»¨ν…Œμ΄λ„ˆμ—μ„œ 싀행쀑인 ν”„λ‘œμ„ΈμŠ€ 확인

    docker compose top
  39. wordpress μ»¨ν…Œμ΄λ„ˆμ—μ„œ μ—λŸ¬κ°€ λ°œμƒν–ˆλ‹€κ³  κ°€μ •ν•˜κ³  싀행쀑인 ν”„λ‘œμ„ΈμŠ€ μ’…λ£Œ

    docker compose exec wordpress pkill apache2
  40. μ›Œλ“œν”„λ ˆμŠ€ μ‚¬μ΄νŠΈμ— 접속 λ˜λŠ”μ§€ 확인

  41. Compose ν”„λ‘œμ νŠΈ μƒνƒœ 확인

    docker compose ls
  42. wordpress μ»¨ν…Œμ΄λ„ˆκ°€ 싀행쀑인지 확인

    docker compose ps -a
  43. Compose 파일 μˆ˜μ •

    cat <<EOF | tee docker-compose.yml
    name: blog
    services:
      wordpress:
        build: .
        container_name: wordpress
        restart: always
        volumes:
        - plugins:/var/www/html/wp-content/plugins
        ports:
        - 8080:80
        environment:
        - WORDPRESS_DB_HOST=mysql
        - WORDPRESS_DB_USER=wordpress
        - WORDPRESS_DB_PASSWORD=wordpress
        - WORDPRESS_DB_NAME=wordpress
        depends_on:
        - wordpress-init
      mysql:
        image: mysql
        volumes:
        - mysql-data:/var/lib/mysql
        environment:
        - MYSQL_ROOT_PASSWORD=asdf1234
        - MYSQL_USER=wordpress
        - MYSQL_PASSWORD=wordpress
        - MYSQL_DATABASE=wordpress
      wordpress-init:
        image: alpine
        volumes:
        - plugins:/plugins
        command:
        - "sh"
        - "-c"
        - "wget -O https://downloads.wordpress.org/plugin/classic-editor.1.6.3.zip && unzip -o classic-editor.1.6.3.zip -d /plugins"
    volumes:
      mysql-data:
      plugins:
    EOF
  44. Compose ν”„λ‘œμ νŠΈ μž¬μ‹€ν–‰

    docker compose up -d
  45. μ›Œλ“œν”„λ ˆμŠ€ μ‚¬μ΄νŠΈμ— 접속 λ˜λŠ”μ§€ 확인

  46. wordpress μ»¨ν…Œμ΄λ„ˆμ—μ„œ μ—λŸ¬κ°€ λ°œμƒν–ˆλ‹€κ³  κ°€μ •ν•˜κ³  싀행쀑인 ν”„λ‘œμ„ΈμŠ€ μ’…λ£Œ

    docker compose exec wordpress pkill apache2
  47. μ›Œλ“œν”„λ ˆμŠ€ μ‚¬μ΄νŠΈμ— 접속 λ˜λŠ”μ§€ 확인

  48. wordpress μ»¨ν…Œμ΄λ„ˆκ°€ 싀행쀑인지 확인

    docker compose ps
  49. wordpress μ»¨ν…Œμ΄λ„ˆκ°€ μ‚­μ œλ˜λ©΄ λ‹€μ‹œ 생성 λ κΉŒμš”?

    docker container rm wordpress --force
  50. wordpress μ»¨ν…Œμ΄λ„ˆκ°€ 싀행쀑인지 확인

    docker compose ps -a
  51. Compose ν”„λ‘œμ νŠΈ μ‚­μ œ

    docker compose down
  52. Compose에 λͺ…μ‹œν–ˆλ˜ Volume듀이 남아 μžˆλŠ”μ§€ 확인

    docker volume ls
  53. Compose ν”„λ‘œμ νŠΈ 생성

    docker compose up -d
  54. μ›ΉλΈŒλΌμš°μ €μ—μ„œ μ›Œλ“œν”„λ ˆμŠ€ μ‚¬μ΄νŠΈ μž¬μ ‘μ† - μ•„λž˜ λͺ…λ Ήμ–΄λ‘œ μ£Όμ†Œ 확인 κ°€λŠ₯

    echo "$(curl -s ifconfig.io):8080"
  55. Compose ν”„λ‘œμ νŠΈ μ‚­μ œ

    docker compose down --volumes
  56. λ©€ν‹° μ»¨ν…Œμ΄λ„ˆ ν™˜κ²½μ—μ„œ κ°œλ³„λ‘œ μƒμ„±ν–ˆλ˜ μ• ν”Œλ¦¬μΌ€μ΄μ…˜λ“€μ„ Docker Composeλ₯Ό ν†΅ν•΄μ„œ ꡬ성

  57. λ¦¬μ†ŒμŠ€ 정리

    {
        docker compose down --volumes
        docker container rm $(docker container ls -aq) --force
        docker image prune --all --force
        docker volume prune --force
        docker system prune --all --force
        cd ~/environment
    }

Last updated