본문 바로가기

[DevOps]/[Kubernetes]

[Qwiklabs] Kubernetes in Google Cloud [ Introduction to Docker ]

728x90

[ Introduction to Docker ]

www.docker.com

 

Empowering App Development for Developers | Docker

Learn how Docker helps developers bring their ideas to life by conquering the complexity of app development.

www.docker.com

시작은 Docker이다. 잘 사용하면 굉장히 편리하다고들 하지만, 나는 아직도 컴퓨터를 옮기거나 새로 작업환경을 구축할 때 마다 처음부터 끝까지 설치하고는 한다. 그러면 하루가 끝나있다. GCP에서는 docker를 어떻게 다루는 지 살펴보자.

  • How to build, run, and debug Docker containers.
  • How to pull Docker images from Docker Hub and Google Container Registry.
  • How to push Docker images to Google Container Registry.

여기서 배우는 내용은 docker container를 빌드,실행,디버그 하는 방법과, docker hub 혹은 container registry에서 어떻게 가져오는 지를 배운다. 마지막에는 간단한 배포과정도 있다.

먼저, 클라우드 쉘에서 다음과 같은 명령어를 실행시켜보자.

docker run hello-world

  실행하면 다음과 같은 내용이 뜬다. 명령어를 실행시키면, docker 데몬은 로컬레퍼지토리에서 hello - world라는 이미지파일을 찾는다. 하지만 따로 생성해두지 않았으므로, 로컬 레포지토리에 있을리가 없다. 이런 상황에서 데몬은 Docker hub라는 public registry에서 이미지를 당겨와 실행시켜준다. 

docker images

위 명령어를 통해 레퍼지토리를 볼 수 있다. image id는 당겨온 이미지의 이름이라고 할 수 있는데 SHA256 hash format으로 되어있다. 실행은 'docker run hello-world' 로 실행시킬 수 있다.

docker ps # docker ps -a는 이미 실행시켰던 container들도 전부 볼 수 있다.

 

Build)

dockerfile)

  • The initial line specifies the base parent image, which in this case is the official Docker image for node version 6.
  • In the second, we set the working (current) directory of the container.
  • In the third, we add the current directory's contents (indicated by the "." ) into the container.
  • Then we expose the container's port so it can accept connections on that port and finally run the node command to start the application.

jsfile)

  • This is a simple HTTP server that listens on port 80 and returns "Hello World."

 

이러면 build 과정에서 필요한 기본적인 파일들은 완성이 된 것이다. 다음 명령어를 통해 docker를 빌드한다.

docker build -t node-app:0.1 .

-t는 tag를 붙이는 것인데, name:tag형식이다. 다시말해 이름이 node-app, tag가 0.1인 것이다. 테그를 설정하지 않으면 최신값으로 디폴트 되므로 사용하는 것을 권장한다고 한다. 마지막에 .은 현재 디렉토리를 뜻하는 것으로, dockerfile이 위치한 디렉토리에서 실행해야 한다.

아래 명령어를 통해 만들어진 docker image들을 확인할 수 있다.

docker images

이때 node는 base image이고, node-app은 내가 빌드한 이미지 이다. 빌드한 이미지인 node-app를 제거하지 않고, 본 이미지인 node(base-image)를 삭제할 수 는 없다.

 

Run)

docker run -p 4000:80 --name my-app node-app:0.1

다음 명령어로 앱을 실행시켜 볼 수 있다.  삭제 명령어는 다음과 같다. 

docker stop my-app && docker rm my-app

 

Publish)

이제 완성된 도커 이미지를 Google Container Registry (gcr)에 대포해보기로 한다. 그리고 이어서 컨테이너와 이미지 파일들을 전부 지우고 나서 다시 gcr에서 컨테이너를 다시 당겨오도록 해 보겠다. 이 과정을 보면 도커를 사용하는 것이 굉장히 편리하다는 것을 알 수 있다. 

publish를 위해 알아야 되는 정보는 다음과 같다.

  • [hostname]= gcr.io
  • [project-id]= your project's ID
  • [image]= your image name
  • [tag]= any string tag of your choice. If unspecified, it defaults to "latest".

project id는 cli화면에서도 찾을 수 있지만, 다음 명령어로도 받아올 수 있다.

gcloud config list project

아래 명령어는 테그한 컨테이너를 gcr로 올리는 과정들인데 먼저 컨테이너를 테그하고, 이어서 push하면 된다. 마치 git과 비슷한....

docker tag node-app:0.2 gcr.io/[project-id]/node-app:0.2

 

docker push gcr.io/[project-id]/node-app:0.2

gcr로 올린 container는 Tools > Container Registry나  http://gcr.io/[project-id]/node-app에서 다음과 같이 확인할 수 있다.

 

이전의 내용들을 삭제해주자.

# Stop and remove all containers:
docker stop $(docker ps -q)
docker rm $(docker ps -aq)

# You have to remove the child images (of node:6) before you remove the node image. Replace [project-id].
docker rmi node-app:0.2 gcr.io/[project-id]/node-app node-app:0.1
docker rmi node:6
docker rmi $(docker images -aq) # remove remaining images
docker images

다음과 같이 전부 삭제된 것을 볼 수 있다.

그리고 나서 다음 명령어로 컨테이너를 다시 간단하게 gcr에서 끌어와서 실행시킬 수 있다!

docker pull gcr.io/[project-id]/node-app:0.2
docker run -p 4000:80 -d gcr.io/[project-id]/node-app:0.2
curl http://localhost:4000

 

 

728x90