aws 배포 연습. 1 docker

홍태경·2021년 5월 23일
0

CI/CD

목록 보기
2/2
  1. 도커
  • ubuntu docker를 설치 후 가지고 놀아보자.

Server: gunicorn/20.0.4
Vary: Origin
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

update 와 upgrade 차이

sudo apt-get update: 업데이트할 패키지들을 파악합니다.

sudo apt-get upgrade: 업데이트할 패키지들과 현재 보유하고 있는 패키지들을 비교하며 업데이트를 수행합니다.

sudo apt-get dist-upgrade: 의존성 검사를 하면서 위의 upgrade에서 수행하지 못한 업데이트를 수행합니다. (잘 사용하지는 않습니다.)

sudo apt update
sudo apt upgrade

ubuntu docker 다운로드

sudo apt update

sudo apt install apt-transport-https ca-certificates curl software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

sudo apt update
apt-cache policy docker-ce
sudo apt install docker-ce

3. EC2 서버 및 RDS 생성 해오기

  • 기존에 생성하신분은 DATABASE의 호스트 주소 , 사용자명, 비밀번호 를 확인해오세요. 실습에 필요합니다.
  • 기존에 미생성하신분은 RDS를 생성해주세요.
  • EC2에 도커설치는 미리 해놓으셔도 안해놓으셔도 상관없습니다. 실습때 진행예정. =======================================================================================================================

파라미터 그룹을 먼저 생성

파라미터 그룹 생성 =

데이터베이스에 한글뿐만 아닌 , (이모티콘) 도 넣고 싶다

character_set 모두 utf8mb로 수정

utf8 도 이모티콘이 들어가지만 mysql에서 바이트를? 잘못 설정하여 개량판으로 utf8mb로 나왔다 한다 패스.

다시 검색어에 colla 이라 검색 후

collation_connection utf8mb4 general_ci
collation_server utf8mb4 unicode_ci

rds 생성

버전은 mysql 5.7.26

db 이름 wecode
root
dkagh1234.
dkagh1234.

도커 이미지를 생성

도커 이미지를 생성 하면 임시 컨테이너가 생성되며 그것으로 도커 이미지가 나온다

형식은 이렇다

바탕화면에 도커 폴더 생성 후 dockerfile 이라는 파일을 생성한다.

그 안에

FROM 

RUN

cmd 

로 넣는데 일단 이해는 되지 않는다.

이제 터미널을 가서 해당 디렉토리로 이동 후

sudo docker build ./을 하면

Step에 따라 임시 컨테이너가 생성 되고 그것을 바탕으로 Step 2
1c28eed96a5f 라는 이미지가 생성 된 것을 확인 할 수 있다

(base) hongtae@user:~/바탕화면/dockerfile-folder$ sudo docker build ./
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM alpine
---> 6dbb9cc54074
Step 2/2 : CMD [ "echo","hello" ]
---> Running in 9332375a52d9
Removing intermediate container 9332375a52d9
---> 1c28eed96a5f
Successfully built 1c28eed96a5f

생성 된 이미지 컨테이너 생성 후 실행

sudo docker run 1c28eed96a5f
hello

(base) hongtae@user:~/바탕화면/dockerfile-folder$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93f7b1afd424 1c28eed96a5f "echo hello" About a minute ago Exited (0) About a minute ago pensive_moore

도커 컨테이너 이름 정해주기.

컨테이너를 생성 후 나오는 긴 이름을 항상 ps 를 통해서 봐야할까? 내가 지정한 이름으로 수정 할 수 없을까?

(base) hongtae@user:~/바탕화면/dockerfile-folder$ sudo docker build -t gusxoqkqh1/hello:latest ./
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM alpine
---> 6dbb9cc54074
Step 2/2 : CMD [ "echo","hello" ]
---> Using cache
---> 1c28eed96a5f
Successfully built 1c28eed96a5f
Successfully tagged gusxoqkqh1/hello:latest

(base) hongtae@user:~/바탕화면/dockerfile-folder$ sudo docker run -it gusxoqkqh1/hello
hello

로컬에서 도커 이미지 생성

이미지를 만들어라

sudo docker build -t gusxoqkqh1/docker_train:0.1 .
sudo docker run python:3
sudo docker ps -a
sudo docker images

컨테이너를 생성해라.

status 값을 보면 알 수 있다.
도커 == 프로세스
시작과 끝이 있다.

sudo docker run -d -p 8000:8000 --name docker_train gusxoqkqh1/docker_train:0.1

-d 데몬(백그라운드)로 돌리면서 실행 되는것이 안꺼지게 하는것.
-p 포트포워딩 : 외부에서 8000번으로 내 공인망으로 들어올 시
내 피시에 있는 도커 런서버 8000번이랑 매치 시켜 접속 시켜줘라.
-- name or -n : 명을 지워줘라.
실행할 이미지 명

4aea6299b5187de144c39ceea46d162c6bec31449785085ba91fd9c089919640

(docker_train) hongtae@user:~/dev/docker-training$ sudo docker ps ( 실행중인 컨테이너 확인)

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4aea6299b518 gusxoqkqh1/docker_train:0.1 "gunicorn --bind 0.0…" 10 seconds ago Up 9 seconds 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp docker_train

실행되고 있는 도커이미지에 접속 하려면

sudo docker exec -it docker_train /bin/bash

지훈멘토님이 만들어놓으신 엔드포인트가 있으셔서 테스트

(docker_train) hongtae@user:~/dev/docker-training$ http -v localhost:8000/basic

GET /basic HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:8000
User-Agent: HTTPie/2.3.0



HTTP/1.1 200 OK
Connection: close
Content-Length: 333
Content-Type: application/json
Date: Mon, 24 May 2021 11:04:17 GMT
Referrer-Policy: same-origin
Server: gunicorn/20.0.4
Vary: Origin
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

[
    {
        "created_at": "2021-05-24T10:28:08.805",
        "id": 1,
        "name": "??? ??? ?? ??? ??"
    },
    {
        "created_at": "2021-05-24T10:28:08.817",
        "id": 2,
        "name": "??? ??? ?? ????."
    },
    {
        "created_at": "2021-05-24T10:28:08.823",
        "id": 3,
        "name": "?? ?? ???? ??? http://docker.com ??"
    },
    {
        "created_at": "2021-05-24T10:28:08.833",
        "id": 4,
        "name": "??? ???!!"
    }
]

name에 ? 나오는것은 Rds 파라메타에 utf-8mb4를 안해줘서 그렇다

나의 rds 에 있는 디비 확인

mysql -h wecode-docker.c5e5d6grvspp.ap-northeast-2.rds.amazonaws.com -u root -p

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| docker |
| innodb |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.01 sec)

mysql> use docker;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------+
| Tables_in_docker |
+---------------------+
| basics |
| django_content_type |
| django_migrations |
| django_session |
+---------------------+
4 rows in set (0.00 sec)

mysql> desc basics;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| created_at | datetime(6) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> select * from basics
-> ;
+----+-------------------------------------+----------------------------+
| id | name | created_at |
+----+-------------------------------------+----------------------------+
| 1 | ??? ??? ?? ??? ?? | 2021-05-24 10:28:08.805974 |
| 2 | ??? ??? ?? ????. | 2021-05-24 10:28:08.817140 |
| 3 | ?? ?? ???? ??? http://docker.com ?? | 2021-05-24 10:28:08.823729 |
| 4 | ??? ???!! | 2021-05-24 10:28:08.833574 |
+----+-------------------------------------+----------------------------+
4 rows in set (0.01 sec)

aws 에 배포하기

도커 허브에 내 이미지를 올려보기.

(docker_train) hongtae@user:~/dev/docker-training$ sudo docker login

Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
도커 허브에 받는 아이디
Username: gusxoqkqh1
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

(docker_train) hongtae@user:~/dev/docker-training$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4aea6299b518 gusxoqkqh1/docker_train:0.1 "gunicorn --bind 0.0…" 15 minutes ago Up 15 minutes 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp docker_train

(docker_train) hongtae@user:~/dev/docker-training$ sudo docker push gusxoqkqh1/docker_train:0.1
The push refers to repository [docker.io/gusxoqkqh1/docker_train]
5f8652461892: Pushed
8e70624f3762: Pushed
0c0e8da8b4a7: Pushed
0c8d2e0c0785: Pushed
88ac49c4037d: Mounted from library/python
001ade22e15c: Mounted from library/python
97e852d9107e: Mounted from library/python
1591bf7ec708: Mounted from library/python
dd3097cd7909: Mounted from library/python
685934357c89: Mounted from library/python
ccb9b68523fd: Mounted from library/python
00bcea93703b: Mounted from library/python
688e187d6c79: Mounted from library/python
0.1: digest: sha256:6a235ac8cc60e130b83dc9a42ca8ac3f6d07514846e82e37cbb5f6452cc004c2 size: 3052

푸쉬가 잘 되었나 도커 허브 확인

도커 푸시 전 도커 허브

도커 푸쉬 후 도커 허브

rds 에서 해보려면

ec2 접근해서 도커 install 하고 도커 이미지 pull 이미지명

잘 되면 run 하면 된다.

ubuntu..는 한줄한줄 넣어줘야 한다 하아..

ssh -i x.pem ubuntu@ip

  • sudo apt update

  • sudo apt install apt-transport-https ca-certificates curl software-properties-common

-curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

aws ec2에 도커 설치 완료

ubuntu@ip-172-31-1-69:~$ docker

Usage: docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
--config string Location of client config files (default "/home/ubuntu/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context
set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/home/ubuntu/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/home/ubuntu/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/ubuntu/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit

Management Commands:
app Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx
Build with BuildKit (Docker Inc., v0.5.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.7.0)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes

Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes

도커 로그인

ubuntu@ip-172-31-1-69:~$ sudo docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: gusxoqkqh1
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

받아온 이미지 확인 후 컨테이너 만들기

ubuntu@ip-172-31-1-69:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ubuntu@ip-172-31-1-69:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ubuntu@ip-172-31-1-69:~$ sudo docker pull gusxoqkqh1/docker_train:0.1

0.1: Pulling from gusxoqkqh1/docker_train
d960726af2be: Pull complete
e8d62473a22d: Pull complete
8962bc0fad55: Pull complete
65d943ee54c1: Pull complete
532f6f723709: Pull complete
1334e0fe2851: Pull complete
062ada600c9e: Pull complete
aec2e3a89371: Pull complete
5c6566073cac: Pull complete
e66ef42cfce5: Pull complete
c005c15c4283: Pull complete
43faf29b599a: Pull complete
65dc0390a9a1: Pull complete
Digest: sha256:6a235ac8cc60e130b83dc9a42ca8ac3f6d07514846e82e37cbb5f6452cc004c2
Status: Downloaded newer image for gusxoqkqh1/docker_train:0.1
docker.io/gusxoqkqh1/docker_train:0.1
ubuntu@ip-172-31-1-69:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

ubuntu@ip-172-31-1-69:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

ubuntu@ip-172-31-1-69:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gusxoqkqh1/docker_train 0.1 1d33d794499f 49 minutes ago 933MB

aws 서버에서 런 서버
sudo docker run -d -p 8000:8000 --name docker_train gusxoqkqh1/docker_train:0.1

이미 데몬으로 돌아가는 컨테이너는 찾기가 너무 힘들다 걍 remove로 강제 제거 시킬 수 있다.

sudo docker run -d -p 8000:8000 --name docker_train gusxoqkqh1/docker_train:0.1
docker: Error response from daemon: Conflict. The container name "/docker_train" is already in use by container "313f36525d86e0a2e01f0997b8b704bb6684b8f8bd89211c38ec4e630d269c90". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
ubuntu@ip-172-31-1-69:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ubuntu@ip-172-31-1-69:~$ sudo docker remove 313f36525d86e0a2e01f0997b8b704bb6684b8f8bd89211c38ec4e630d269c90
docker: 'remove' is not a docker command.
See 'docker --help'
ubuntu@ip-172-31-1-69:~$ docker rm 313f36525d86e0a2e01f0997b8b704bb6684b8f8bd89211c38ec4e630d269c90
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Delete http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/313f36525d86e0a2e01f0997b8b704bb6684b8f8bd89211c38ec4e630d269c90: dial unix /var/run/docker.sock: connect: permission denied
ubuntu@ip-172-31-1-69:~$ sudo docker rm 313f36525d86e0a2e01f0997b8b704bb6684b8f8bd89211c38ec4e630d269c90
313f36525d86e0a2e01f0997b8b704bb6684b8f8bd89211c38ec4e630d269c90

ubuntu@ip-172-31-1-69:~$ sudo docker run -d -p 8000:8000 --name docker_train gusxoqkqh1/docker_train:0.1
b0b58bfcc5ad47a22bff783adac0fe09c848a2d817bdccab7731f4e88077722b

udo docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0b58bfcc5ad gusxoqkqh1/docker_train:0.1 "gunicorn --bind 0.0…" 22 seconds ago Up 21 seconds 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp docker_train

aws 에서 돌고 있는 것은 받아오지 못한다..왜지..?

(base) hongtae@user:~$ http -v 3.36.26.98:8000/basic
GET /basic HTTP/1.1
Accept: /
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: 3.36.26.98:8000
User-Agent: HTTPie/2.3.0

http: error: ConnectionError: HTTPConnectionPool(host='3.36.26.98', port=8000): Max retries exceeded with url: /basic (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0eea1564a8>: Failed to establish a new connection: [Errno 110] Connection timed out',)) while doing a GET request to URL: http://3.36.26.98:8000/basic

HTTPConnectionPool(host='3.36.26.98', port=8000): Max retries exceeded with url: /basic (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0eea1564a8>: Failed to establish a new connection: [Errno 110] Connection timed out',))

ec2 보안그룹 8000 설정

(base) hongtae@user:~$ http -v 3.36.26.98:8000/basic
GET /basic HTTP/1.1
Accept: /
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: 3.36.26.98:8000
User-Agent: HTTPie/2.3.0

HTTP/1.1 200 OK
Connection: close
Content-Length: 333
Content-Type: application/json
Date: Mon, 24 May 2021 12:49:37 GMT
Referrer-Policy: same-origin
Server: gunicorn/20.0.4
Vary: Origin
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

[
{
"created_at": "2021-05-24T10:28:08.805",
"id": 1,
"name": "??? ??? ?? ??? ??"
},
{
"created_at": "2021-05-24T10:28:08.817",
"id": 2,
"name": "??? ??? ?? ????."
},
{
"created_at": "2021-05-24T10:28:08.823",
"id": 3,
"name": "?? ?? ???? ??? http://docker.com ??"
},
{
"created_at": "2021-05-24T10:28:08.833",
"id": 4,
"name": "??? ???!!"
}
]

profile
나의 에고를 인정하고 사랑하자

0개의 댓글