[Docker] 도커로 Centos 다루기

식빵·2022년 1월 11일
1
post-thumbnail

최근 회사에서 사용중인 Centos 서버의 세팅법이 굉장히 복잡하다.
그래서 나만의 가상 환경에서 Centos 서버를 띄우고, 그 안에서 세팅하는 연습을 하고 싶다는 생각이 들었다.

고민 끝에 docker를 사용해서 Centos를 띄우고, 그 안에서 세팅하는 연습을 해보기로 했다.
VirtualBox 을 사용하는 방법도 있지만, 이왕하는 거 개발자들이 필수템이라 불리는 docker로 해보는 게 좋을 거 같아서 그랬다.

이번 글은 Docker 로 Centos OS 를 띄우는 것 정도만 하겠다.
복잡한 세팅들은 추후에 잘 되면 이어서 작성해보겠다.

참고 : 테스트 환경

  • OS : Window 10 Pro 사용 중!
  • Terminal Program : Window PowerShell
  • 도커는 이미 깔려있다고 가정합니다!

Docker로 Centos 설치 및 실행

1. docker search centos : 도커 이미지 검색


2. docker pull centos : 도커 이미지 다운로드


3. docker images -a : 현재 갖고 있는 이미지 조회


4. docker run --name centos-test --hostname centos-test -it -d centos :

  • 도커 이미지를 통해서 도커 컨테이너 생성 후 실행
  • --name
    • container 이름 설정
    • 나중에 stop, start, exec 명령어와 함께 사용
    • 지정 안하면 자동으로 이름을 생성. docker ps -a 를 통해서 이름 확인 가능
  • --hostname : container 의 os hostname 설정
  • --it : bash 가 실행되기 위한 interactive + pseudo tty 를 할당. 필수!
  • --d : detach mode 로 실행(= 백그라운드 실행)

참고: -d 만 하면 되지 않나? 왜 --it가 필요할까?

centos 이미지에는 기본 COMMAND 가 있고, container 가 실행될 때 이 COMMAND 가 실행된다. centos의 경우에는 /bin/bash 이다. 이 bash 가 실행되기 위해서는 "터미널"이 필요하다. 그래서 --it 옵션을 통해서 interactive 한 pseudo tty를 할당하는 것이다.

이렇게 안 하면 run을 해서 container가 생성되어도 바로 stop 상태가 된다.
이후에 다시 docker start 해도 같은 결과를 낳는다. 그러니 꼭 이 옵션은 지정한다.

그리고 꼭 위와 같은 경우가 아니더라도, 이후에 container에 bash로 접속하기 위해서는 이 설정을 켜줘야 한다. bash는 결국 terminal 이 있어야 동작하는 프로그램이라는 것을 잊지 말자.

이 참고글은 스택오버플로우을 참고했다.


5. docker ps -a : 실행 혹은 정지된 모든 docker container list를 조회

  • 방금 실행한 docker run에 의해 container 실행 상태(STATUS : Up x minutes)인 것을 확인할 수 있다.

6. docker exec -it centos-test /bin/bash : centos-test container의 bash 접속

  • 주의! container 가 실행되고 있는 상태에서 exec 명령어를 써야 한다.

TIP : bash 접속 상태에서 docker stop 하지 않으면서 빠져나오기

  • ctrl + p ==> ctrl + q 를 연달아 입력
  • docker run 명령어 옵션에 -d (= detach)를 줬기 때문에 bash에 exit 입력해도 가능함




Centos 필요한 패키지 설치

여기서부터는 도커가 아닌 Centos 명령어와 관련된 내용이다.
가볍게 보고 넘어가자.

1. yum update -y : 현재 갖고 있는 패키지 최신화

2. yum install ncurses wget vim net-tools -y : 필요한 패키지들 설치

3. yum list installed | grep -E "wget|ncurses|vim|net-tools" : 정상 설치되었는지 확인


콘솔 화면




Docker Container 정지, 재시작

1. docker stop centos-test : docker container 를 중지

2. docker ps -a 를 통해서 현재 중지가 되었는지 확인해주면 좋다.


3. docker start centos-test : docker container (재)시작


bash 접근을 하려면 이전과 같이 docker exec -it centos-test /bin/bash 를 실행한다.

참고로 이렇게 bash 에 접근하고 나서 exit 또는 [ctrl + p] ==> [ctrl + q] 를 입력하면 이전과 같이 docker container 가 멈추지 않고 백그라운드에서 돌면서 bash 에서 빠져나올 수 있다.




작업 내용 Docker Image로 저장

1. docker ps -a : 먼저 Container 의 Name 또는 ID 를 알기 위해서 명령어 수행

2. docker commit {Container Name 또는 Container ID} {원하는 Image Repository 이름 지정} : 이미지 생성

3. docker images : 이미지가 정상 생성되었는지 확인한다.

이후에 세팅법을 하다가 망치면 지금 만든 image를 run시키면 된다.
ex) docker run --hostname test1 --name test1 -it -d dailycode/centos_image




command 한번에 보기

window powershell
docker search centos
docker pull centos
docker images -a
docker run --name centos-test --hostname centos-test -it -d centos


centos bash
[root@centos-test /]# yum update -y
[root@centos-test /]# yum install ncurses wget vim net-tools -y
[root@centos-test /]# yum list installed | grep -E "wget|ncurses|vim|net-tools"
[root@centos-test /]# exit


window powershell
docker ps -a
docker exec -it centos-test /bin/bash
docker stop centos-test
docker start centos-test
docker exec -it centos-test /bin/bash


centos bash
[root@centos-test /]# exit


window powershell
docker commit container-test dailycode/centos_image





참고

  1. "docker run -it" vs "docker run -itd"
  2. terminal vs shell
profile
백엔드를 계속 배우고 있는 개발자입니다 😊

1개의 댓글

comment-user-thumbnail
2023년 12월 19일

정말 감사드립니다.

답글 달기

관련 채용 정보