0712-Docker

hyejin·2022년 7월 12일
0

commit 동작중인 컨테이너를 이미지로 생성 가능
build 도커파일 먼저 구성 -> 이미지로

--- 도커 파일 이해

# vi Dockerfile
FROM ubuntu:18.04   //이미지 저장
MAINTAINER johnlee   //작성자 지정
LABEL "name"="webserver"  //라벨 설정
ENV aloha=date
ENV path=/var/www/html
RUN sed -i 's/archive.ubuntu.com/ftp.daumkakao.com/g' /etc/apt/sources.list
RUN apt-get update  //명령어 실행
RUN apt-get install apache2 -y
COPY nihao /var/www/html/nihao   //cp
COPY hello.html $path
ADD aws.tar /var/www/html     //tar를 푸는 느낌
WORKDIR /var/www/html         //cd, exec -it 
RUN echo ohayo >> ohayo.html
VOLUME /var/www/html          //-v /tmt:/var/www/html 와 유사, host 경로 지정 불가, 컨테이너 경로만 지정 가능
EXPOSE 80   //포트, -p 80:80, -p 8080:80, -P 컨테이너 포트
ENTRYPOINT ["apachectl"]  //데몬 실행 ["apachectl", "-D", "FOREGROUND"]
CMD ["-D", "FOREGROUND"]  //데몬 실행(커스터마이징 가능) ["apachectl", "-D", "FOREGROUND"]
[root@localhost test]# mkdir nihao
[root@localhost test]# vi Dockerfile
[root@localhost test]# vi nihao/index.html
[root@localhost test]# cat nihao/index.html
NIHAO
[root@localhost test]# echo "NIHAO" > nihao/index.html
[root@localhost test]# ls
Dockerfile  nihao
[root@localhost test]# echo "HELLO" > hello.html
[root@localhost test]# ls
Dockerfile  hello.html  nihao
[root@localhost test]# ls
aws.tar  Dockerfile  hello.html  nihao
  • build
[root@localhost test]# docker build -t seozzang3/hello:v1.0 .
[root@localhost test]# docker images
REPOSITORY              TAG       IMAGE ID       CREATED         SIZE
seozzang3/hello         v1.0      717b29ee982d   2 minutes ago   203MB
test_commit             v1.0      234da1de60e3   23 hours ago    170MB
seozzang3/test_commit   v1.0      234da1de60e3   23 hours ago    170MB
nginx                   latest    55f4b40fe486   2 weeks ago     142MB
mariadb                 latest    ea81af801379   5 weeks ago     383MB
ubuntu                  18.04     ad080923604a   5 weeks ago     63.1MB
centos                  7         eeb6ee3f44bd   9 months ago    204MB
  • push
[root@localhost test]# docker push seozzang3/hello:v1.0
The push refers to repository [docker.io/seozzang3/hello]
09c9bb31afa9: Pushed
69f4a1168951: Pushed
060f81f2e243: Pushed
c401e3921b92: Pushed
2814d0d604d3: Pushed
d59e062e3c10: Pushed
886c31e9418c: Pushed
95129a5fe07e: Mounted from library/ubuntu
v1.0: digest: sha256:04528c711f5810974b43e0ea95ef4878d2582a702c616d5598cfee3d8c12691f size: 1992

  • run
[root@localhost test]# docker run -d -P --name hello seozzang3/hello:v1.0
880b75bfc2f5142c9758bd12d0a167d763c9480a7fbc09c79716f9464074b137
[root@localhost test]# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                                     NAMES
880b75bfc2f5   seozzang3/hello:v1.0   "apachectl -D FOREGR…"   5 seconds ago   Up 4 seconds   0.0.0.0:49154->80/tcp, :::49154->80/tcp   hello

이미지를 tar파일로 변환

[root@localhost test]# cd ~
[root@localhost ~]# mkdir homepage
[root@localhost ~]# cd homepage/
[root@localhost homepage]# ls
[root@localhost homepage]# mkdir images
[root@localhost homepage]# ls
images
[root@localhost homepage]# ls images/
two-rabbit.jpg
[root@localhost homepage]# tar -cvf test.tar images index.html
images/
images/two-rabbit.jpg
index.html
[root@localhost homepage]# vi Dockerfile
[root@localhost homepage]# docker build -t seozzang3/homepage:v1.0 .
Sending build context to Docker daemon  1.161MB
Step 1/3 : FROM nginx:latest
 ---> 55f4b40fe486
Step 2/3 : ADD test.tar /usr/share/nginx/html
 ---> f20029106e15
Step 3/3 : CMD ["nginx", "-g", "daemon off;"]
 ---> Running in c8d8bb6655a5
Removing intermediate container c8d8bb6655a5
 ---> 2ecc35173f13
Successfully built 2ecc35173f13
Successfully tagged seozzang3/homepage:v1.0
[root@localhost homepage]# docker push seozzang3/homepage:v1.0
The push refers to repository [docker.io/seozzang3/homepage]
b3746a9ecfc4: Pushed
e7344f8a29a3: Mounted from seozzang3/test_commit
44193d3f4ea2: Mounted from seozzang3/test_commit
41451f050aa8: Mounted from seozzang3/test_commit
b2f82de68e0d: Mounted from seozzang3/test_commit
d5b40e80384b: Mounted from seozzang3/test_commit
08249ce7456a: Mounted from seozzang3/test_commit
v1.0: digest: sha256:fc2af9c4182f2ffec8a490da84c826fd1740156864805db126f1e6290bd2636e size: 1780
[root@localhost homepage]# docker run -d -p 9000:80 --name home seozzang3/hello:v1.0
c3c49f6773fe8874894a598f5ca2ad88389695adf37360aa8904cfea0b0c08eb
[root@localhost homepage]# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS          PORTS                                     NAMES
c3c49f6773fe   seozzang3/hello:v1.0   "apachectl -D FOREGR…"   10 seconds ago   Up 8 seconds    0.0.0.0:9000->80/tcp, :::9000->80/tcp     home
eb6324144a3b   seozzang3/hello:v1.0   "apachectl -D FOREGR…"   43 minutes ago   Up 43 minutes   0.0.0.0:49156->80/tcp, :::49156->80/tcp   hello2
2844a3cca194   seozzang3/hello:v1.0   "apachectl -D FOREGR…"   43 minutes ago   Up 43 minutes   0.0.0.0:49155->80/tcp, :::49155->80/tcp   hello1
880b75bfc2f5   seozzang3/hello:v1.0   "apachectl -D FOREGR…"   44 minutes ago   Up 44 minutes   0.0.0.0:49154->80/tcp, :::49154->80/tcp   hello

wordpress 설치

# mkdir wordpress && cd $_
# vi Dockerfile
FROM centos:7
MAINTAINER hyejin@example.com
RUN yum install -y httpd php php-mysql php-gd php-mbstring wget unzip
RUN wget https://ko.wordpress.org/wordpress-4.8.2-ko_KR.zip
WORKDIR /var/www/html
RUN unzip /wordpress-4.8.2-ko_KR.zip
RUN mv wordpress/* .
RUN chown -R apache:apache /var/www
CMD httpd -DFOREGROUND
# docker image build -t seozzang3/wordpress:v1.0 .
# docker push seozzang3/wordpress:v1.0
# docker container run -d -p 88:80 --name wordpress seozzang3/wordpress:v1.0 
- dbserver
# docker run -d -p 3306:3306 --name dbserver \
-e MYSQL_DATABASE=wordpress \
-e MYSQL_USER=wpuser \ 
-e MYSQL_PASSWORD=wppass \
-e MYSQL_ROOT_PASSWORD=password --network test_bridge mariadb
# mkdir wordpress && cd $_
# vi Dockerfile
FROM centos:7
MAINTAINER itssp0405@gmail.com
RUN yum install -y httpd php php-mysql php-gd php-mbstring wget unzip
RUN wget https://ko.wordpress.org/wordpress-4.8.2-ko_KR.zip
WORKDIR /var/www/html
RUN unzip /wordpress-4.8.2-ko_KR.zip
RUN mv wordpress/* .
RUN chown -R apache:apache /var/www
CMD httpd -DFOREGROUND
# docker image build -t seozzang3/wordpress:v1.0 .
# docker push seozzang3/wordpress:v1.0
# docker container run -d -p 88:80 --name wordpress --network test_bridge seozzang3/wordpress:v1.0 

--- 도커 데이터 관리

  • Bind Mount
[root@localhost wordpress]# cd ~
[root@localhost ~]# mkdir volume && cd $_
[root@localhost volume]# mkdir bm01 ; touch bm01/test.txt
[root@localhost volume]# ls bm01
test.txt
[root@localhost volume]# docker run -itd --name bm-test -v /root/volume/bm01:/mnt centos:7
ddd0ec3e1974f00bfeb007cba213205b3862610098280ad081622bbeb4611323
[root@localhost volume]# docker exec bm-test ls /mnt
test.txt
[root@localhost volume]# cd bm01
[root@localhost bm01]# ls
test.txt
[root@localhost bm01]# echo hello > hello.txt
[root@localhost bm01]# ls
hello.txt  test.txt
[root@localhost bm01]# docker exec bm-test ls /mnt
hello.txt
test.txt
[root@localhost bm01]# docker run -itd --name vol-test -v my-vol01:/mnt centos:7
58afe47c9861212c92aae656c4d1ed28e7e47dedc4019621efc29f5e8b03ec37
  • Volume
[root@localhost bm01]# docker run -itd -p 801:80 --name vol-web -v my-vol01:/usr/local/apache2/htdocs:ro httpd:latest

  • 삭제
[root@localhost ~]# docker rm -f vol-web vol-test
vol-web
vol-test
[root@localhost ~]# docker volume rm my-vol01
my-vol01

-> 컨테이너 삭제하고 볼륨 삭제해야 함

--- 도커 네트워크 관리

[root@localhost ~]# docker network create new-net --subnet 172.31.0.0/16 --ip-range 172.31.0.0/20 --gateway 172.31.0.1
e0ca3b69dbf1a3cb431290a4650cb6808b554cf476acd1ee50f7e8b9179214e8
[root@localhost ~]# docker run -d -P --network new-net nginx
ff2f70d15a62d4b3449221dc10d29b4b25894a078bd99ec75469ea341a71a3d2

--- 운영자 역할

[root@localhost onbuild]# mkdir onbuild && cd $_
[root@localhost onbuild]# vi Dockerfile.base
FROM ubuntu:18.04
RUN sed -i 's/archive.ubuntu.com/ftp.daumkakao.com/g' /etc/apt/sources.list
RUN apt-get -y update
RUN apt-get -y install nginx
EXPOSE 80
ONBUILD ADD website*.tar /var/www/html/
CMD ["nginx", "-g", "daemon off;"]
[root@localhost onbuild]# docker build -t halilinux/web-base:v1.0 -f Dockerfile.base .

--- 개발자 역할

root@ununtu-node02:~# mkdir onbuild && cd $_
root@ununtu-node02:~/onbuild# vi Dockerfile
FROM seozzang3/web-base:v1.0
root@ununtu-node02:~#docker build -t seozzang3/web-site:v1.0 .
root@ununtu-node02:~#docker run -d -p 80:80 --name web-site seozzang3/web-site:v1.0

root@ununtu-node02:~/onbuild# 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: seozzang3
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

root@ununtu-node02:~/onbuild# docker push seozzang3/web-site:v1.0
The push refers to repository [docker.io/seozzang3/web-site]
9571ae99d4b4: Pushed
e96ade5e718b: Mounted from seozzang3/web-base
09ecc8000b0f: Mounted from seozzang3/web-base
2ec7c2de402a: Mounted from seozzang3/web-base
95129a5fe07e: Mounted from seozzang3/web-base
v1.0: digest: sha256:5851b79ec525d6bdbef2a35d65b38d1e45adf0a7155af7c706082b49b8c5e9c1 size: 1371

aws console

  • 인스턴스 생성

  • route53에 레코드 생성

  • 운영자 ip로 접속

  • ubuntu ip로 접속

0개의 댓글

관련 채용 정보