그냥저냥

컨테이너 hello-world 흉내내보기! 본문

인프라

컨테이너 hello-world 흉내내보기!

sync86 2026. 10. 2. 10:01
728x90
반응형

우선은 hello-world를 C언어로 코드 작성해봅니다.

중요한 것은 코드 내용이 아니라, 빌드할 때가 더 중요하니 어떻게 코드를 작성하느냐는 아무렴 괜찮습니다.

#include <stdio.h>

int main(int argc, char *argv[])
{
  printf("Hello from Docker!\n");
  printf("This message shows that your installation appears to be working correctly.\n");
  printf("\n");
  printf("To generate this message, Docker took the following steps:\n");
  printf(" 1. The Docker client contacted the Docker daemon.\n");
  printf(" 2. The Docker daemon pulled the \"hello-world\" image from the Docker Hub.\n");
  printf("    (amd64)\n");
  printf(" 3. The Docker daemon created a new container from that image which runs the\n");
  printf("    executable that produces the output you are currently reading.\n");
  printf(" 4. The Docker daemon streamed that output to the Docker client, which sent it\n");
  printf("    to your terminal.\n");
  printf("\n");
  printf("To try something more ambitious, you can run an Ubuntu container with:\n");
  printf(" $ docker run -it ubuntu bash\n");
  printf("\n");
  printf("Share images, automate workflows, and more with a free Docker ID:\n");
  printf(" https://hub.docker.com/\n");
  printf("\n");
  printf("For more examples and ideas, visit:\n");
  printf(" https://docs.docker.com/get-started/\n");
  return 0;
}

 

추후 비어있는 컨테이너에 딱 이 파일만으로 실행을 해야 합니다.

이러한 이유로 컴파일 할 때 -static 옵션을 사용하여 정적으로 라이브러리를 포함시켜야 합니다.

참고로 이 옵션을 사용하지 않으면 libc 동적 라이브러리와 링크 하므로 주의해야 합니다.

$ gcc -static ./hello.c

 

워낙 간단한 코드라 빌드할 때 딱히 오류날 일은 거의 없...을테지만, 오류가 있다면 고쳐야겠죠?

참고로 컴파일 후 출력 파일명은 지정하지 않았는데, 지정하지 않으면 현재 디렉토리에서 a.out이라는 파일을 확인할 수 있습니다.

아래와 같이 ./a.out 파일을 실행해봅니다.

$ ./a.out 
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

 

실행은 잘되는 것 같아요.

 

그 후 Dockerfile을 만들어야 해요.

전 touch 명령어로 먼저 생성하고 편집하는 것을 선호합니다.

이후 내용을 수정은 주로 vi를 주로 쓰긴 하지만, 주로 사용하는 에디터를 사용해도 됩니다.

# Dockerfile
FROM scratch
COPY ./a.out /
CMD [ "/a.out" ]

 

Dockerfile 파일 작성을 완료하였다면, 컨테이너 이미지를 빌드 과정을 거쳐야 합니다.

$ docker build -t hello-world:latest .

 

마지막으로 만들어진 이미지를 실행하면 결과는 아래와 같습니다.

$ docker run hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

이렇게 해서 Docker Hub에서 제공하는 hello-world 따라하기를 해보았습니다.

이번 포스팅은 이전 포스팅에서 "컨테이너는 서비스(또는 소프트웨어)를 패키징하는 방식이다."라고 주장의 예제입니다.

물론 이 단순한 예제로는 납득이 어렵겠지만, 다음 1~2개의 포스팅을 더 작성해서 보여드리고자 합니다.

 

원래는 중간에 명령어 실행한 후 확인하는 과정을 넣어야 하지만, 생략하였습니다.

728x90
반응형