본문 바로가기
dev

docker

by igooo 2016. 2. 24.
728x90

이미지 검색
sudo docker search ubuntu

NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                            Ubuntu is a Debian-based Linux operating s...   3254      [OK]      
ubuntu-upstart                    Upstart is an event-based replacement for ...   60        [OK]      
torusware/speedus-ubuntu          Always updated official Ubuntu docker imag...   25                   [OK]
ubuntu-debootstrap                debootstrap --variant=minbase --components...   24        [OK]      


이미지 받기

sudo docker pull ubuntu:latest

docker pull <이미지 이름>:<태그>

latest: Pulling from library/ubuntu
a64038a0eeaa: Pull complete
2ec6e7edf8a8: Pull complete
0a5fb6c3c94b: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:d8b3a9876f7b00798144e75ac6c7f98ab10772867c76dd05f3dbd8687588ab6d
Status: Downloaded newer image for ubuntu:latest


이미지 목록 출력

sudo docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              14b59d36bae0        6 days ago          188 MB


이미지 삭제

sudo docker rmi ubuntu:latest

sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              14b59d36bae0        6 days ago          188 MB
sudo docker rmi ubuntu:latest
Untagged: ubuntu:latest
Deleted: sha256:14b59d36bae04ba5e4c2bb3ff2906c16d6beac3b6c3ea15273f7458bb5687a3d
Deleted: sha256:8c68d485c13c4e8026259f8a3bc244475a2f72a2121207b6c2582fca7c7cbc01
Deleted: sha256:a8bb501b2b3585faff8363af2ff746b9bad5e2cc882ea949e2a8c749dcd5c660
Deleted: sha256:6920d539da4816a51a9b42adf33ca82232a7472532ca17fb03034504e97e1ec4
Deleted: sha256:fd0e26195ab2d3543ed33098e068c617dbe49985108fb4fbb77032abecb59755


컨테이너 생성

sudo docker run -i -t --name hello ubuntu /bin/bash

docker run <옵션> <이미지 이름> <실행할 파일>

root@77e99c748b85:/#


컨테이너 목록 확인

sudo dokcer ps -a (정지된 컨테이너도 조회)

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
77e99c748b85        ubuntu              "/bin/bash"         4 minutes ago       Exited (127) 12 seconds ago                       hello

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
77e99c748b85        ubuntu              "/bin/bash"         6 minutes ago       Up 9 seconds                            hello



컨테이너 실행

sudo docker start hello

hello


컨테이너 정지

sudo docker stop hello

docker stop <컨테이너 이름>


컨테이너 재실행

sudo docker restart hello
hello


attach 컨테이너 접속

sudo docker attach hello

root@77e99c748b85:/#


exec 컨테이너 안의 명령 실행

sudo docker exec hello echo "Hello World"

docker exec <컨테이너 이름> <명령> <매개 변수>

Hello World


컨테이너 삭제

sudo docker rm hello



docker file

Dockerfile

FROM ubuntu:14.04
MAINTAINER Foo Bar <foo@bar.com>

RUN apt-get update
RUN apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
RUN chown -R www-data:www-data /var/lib/nginx

VOLUME ["/data", "/etc/nginx/site-enabled", "/var/log/nginx"]

WORKDIR /etc/nginx

CMD ["nginx"]

EXPOSE 80
EXPOSE 443


FROM : 어떤 이미지를 기반으로 할지 설정

MAINTAINER : 메인테이너 정보

RUN : 셀 스크립트 혹인 명령을 실행합니다.

VOLUME : 호스트와 공유할 디렉터리 목록 -v /root/data:/data는 호스트의 /root/data 디렉터리를  Docker 컨테이너의 /data 디렉터리에 연결

CMD : 컨테이너가 시작되었을 때 실행할 실행 파일 또는 셀 스크립트

WORKDIR : CMD에서 설정한 실행 파일이 실행된 디렉터리

EXPOSE : 호스트와 연결할 포트 번호



build 명령으로 이미지 생성하기

sudo docker build --tag hello:0.1 .

docker build <옵션> <Dockerfile 경로>


Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu:14.04
14.04: Pulling from library/ubuntu
a64038a0eeaa: Pull complete
2ec6e7edf8a8: Pull complete
0a5fb6c3c94b: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:a3799e46440cabaa5414d42972b1693980b30bd2772b969fe11d08d99a8b753c
Status: Downloaded newer image for ubuntu:14.04
 ---> 14b59d36bae0
Step 2 : MAINTAINER Foo Bar <foo@bar.com>
 ---> Running in d049d1e55cb5
 ---> c99fa9ed623d
Removing intermediate container d049d1e55cb5
Step 3 : RUN apt-get update
 ---> Running in 2ffefefd404a
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Get:2 http://archive.ubuntu.com trusty-security InRelease [65.9 kB]
Hit http://archive.ubuntu.com trusty Release.gpg
Get:3 http://archive.ubuntu.com trusty-updates/main Sources [325 kB]
Get:4 http://archive.ubuntu.com trusty-updates/restricted Sources [5217 B]
Get:5 http://archive.ubuntu.com trusty-updates/universe Sources [189 kB]
Get:6 http://archive.ubuntu.com trusty-updates/main amd64 Packages [897 kB]
Get:7 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [23.5 kB]
Get:8 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [438 kB]
Get:9 http://archive.ubuntu.com trusty-security/main Sources [132 kB]
Get:10 http://archive.ubuntu.com trusty-security/restricted Sources [3920 B]
Get:11 http://archive.ubuntu.com trusty-security/universe Sources [37.9 kB]
Get:12 http://archive.ubuntu.com trusty-security/main amd64 Packages [529 kB]
Get:13 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [20.2 kB]
Get:14 http://archive.ubuntu.com trusty-security/universe amd64 Packages [160 kB]
Hit http://archive.ubuntu.com trusty Release
Get:15 http://archive.ubuntu.com trusty/main Sources [1335 kB]
Get:16 http://archive.ubuntu.com trusty/restricted Sources [5335 B]
Get:17 http://archive.ubuntu.com trusty/universe Sources [7926 kB]
Get:18 http://archive.ubuntu.com trusty/main amd64 Packages [1743 kB]
Get:19 http://archive.ubuntu.com trusty/restricted amd64 Packages [16.0 kB]
Get:20 http://archive.ubuntu.com trusty/universe amd64 Packages [7589 kB]
Fetched 21.5 MB in 1min 3s (339 kB/s)
Reading package lists...
 ---> 67bdfce4db7e
Removing intermediate container 2ffefefd404a
Step 4 : RUN apt-get install -y nginx
 ---> Running in 552975fd209f
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
  libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
  libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
  libxslt1.1 nginx-common nginx-core sgml-base xml-core
Suggested packages:
  libgd-tools geoip-bin fcgiwrap nginx-doc sgml-base-doc debhelper
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
  libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
  libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
  libxslt1.1 nginx nginx-common nginx-core sgml-base xml-core
0 upgraded, 25 newly installed, 0 to remove and 1 not upgraded.
Need to get 5614 kB of archives.
After this operation, 19.8 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libgeoip1 amd64 1.6.0-1 [71.0 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/main libxau6 amd64 1:1.0.8-1 [8376 B]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty/main libxdmcp6 amd64 1:1.1.1-1 [12.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/main libxcb1 amd64 1.10-2ubuntu1 [38.0 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ trusty/main libx11-data all 2:1.6.2-1ubuntu2 [111 kB]
Get:6 http://archive.ubuntu.com/ubuntu/ trusty/main libx11-6 amd64 2:1.6.2-1ubuntu2 [560 kB]
Get:7 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libxml2 amd64 2.9.1+dfsg1-3ubuntu4.7 [571 kB]
Get:8 http://archive.ubuntu.com/ubuntu/ trusty/main sgml-base all 1.26+nmu4ubuntu1 [12.5 kB]
Get:9 http://archive.ubuntu.com/ubuntu/ trusty/main fonts-dejavu-core all 2.34-1ubuntu1 [1024 kB]
Get:10 http://archive.ubuntu.com/ubuntu/ trusty-updates/main fontconfig-config all 2.11.0-0ubuntu4.1 [47.4 kB]
Get:11 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libfreetype6 amd64 2.5.2-1ubuntu2.5 [304 kB]
Get:12 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libfontconfig1 amd64 2.11.0-0ubuntu4.1 [123 kB]
Get:13 http://archive.ubuntu.com/ubuntu/ trusty/main libjpeg-turbo8 amd64 1.3.0-0ubuntu2 [104 kB]
Get:14 http://archive.ubuntu.com/ubuntu/ trusty/main libjpeg8 amd64 8c-2ubuntu8 [2194 B]
Get:15 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libjbig0 amd64 2.0-2ubuntu4.1 [26.1 kB]
Get:16 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libtiff5 amd64 4.0.3-7ubuntu0.3 [143 kB]
Get:17 http://archive.ubuntu.com/ubuntu/ trusty/main libvpx1 amd64 1.3.0-2 [556 kB]
Get:18 http://archive.ubuntu.com/ubuntu/ trusty/main libxpm4 amd64 1:3.5.10-1 [38.3 kB]
Get:19 http://archive.ubuntu.com/ubuntu/ trusty/main libgd3 amd64 2.1.0-3 [147 kB]
Get:20 http://archive.ubuntu.com/ubuntu/ trusty/main libxslt1.1 amd64 1.1.28-2build1 [145 kB]
Get:21 http://archive.ubuntu.com/ubuntu/ trusty/main geoip-database all 20140313-1 [1196 kB]
Get:22 http://archive.ubuntu.com/ubuntu/ trusty/main xml-core all 0.13+nmu2 [23.3 kB]
Get:23 http://archive.ubuntu.com/ubuntu/ trusty-updates/main nginx-common all 1.4.6-1ubuntu3.4 [18.3 kB]
Get:24 http://archive.ubuntu.com/ubuntu/ trusty-updates/main nginx-core amd64 1.4.6-1ubuntu3.4 [325 kB]
Get:25 http://archive.ubuntu.com/ubuntu/ trusty-updates/main nginx all 1.4.6-1ubuntu3.4 [5616 B]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin:
Fetched 5614 kB in 12s (459 kB/s)
Selecting previously unselected package libgeoip1:amd64.
(Reading database ... 11542 files and directories currently installed.)
Preparing to unpack .../libgeoip1_1.6.0-1_amd64.deb ...
Unpacking libgeoip1:amd64 (1.6.0-1) ...
Selecting previously unselected package libxau6:amd64.
Preparing to unpack .../libxau6_1%3a1.0.8-1_amd64.deb ...
Unpacking libxau6:amd64 (1:1.0.8-1) ...
Selecting previously unselected package libxdmcp6:amd64.
Preparing to unpack .../libxdmcp6_1%3a1.1.1-1_amd64.deb ...
Unpacking libxdmcp6:amd64 (1:1.1.1-1) ...
Selecting previously unselected package libxcb1:amd64.
Preparing to unpack .../libxcb1_1.10-2ubuntu1_amd64.deb ...
Unpacking libxcb1:amd64 (1.10-2ubuntu1) ...
Selecting previously unselected package libx11-data.
Preparing to unpack .../libx11-data_2%3a1.6.2-1ubuntu2_all.deb ...
Unpacking libx11-data (2:1.6.2-1ubuntu2) ...
Selecting previously unselected package libx11-6:amd64.
Preparing to unpack .../libx11-6_2%3a1.6.2-1ubuntu2_amd64.deb ...
Unpacking libx11-6:amd64 (2:1.6.2-1ubuntu2) ...
Selecting previously unselected package libxml2:amd64.
Preparing to unpack .../libxml2_2.9.1+dfsg1-3ubuntu4.7_amd64.deb ...
Unpacking libxml2:amd64 (2.9.1+dfsg1-3ubuntu4.7) ...
Selecting previously unselected package sgml-base.
Preparing to unpack .../sgml-base_1.26+nmu4ubuntu1_all.deb ...
Unpacking sgml-base (1.26+nmu4ubuntu1) ...
Selecting previously unselected package fonts-dejavu-core.
Preparing to unpack .../fonts-dejavu-core_2.34-1ubuntu1_all.deb ...
Unpacking fonts-dejavu-core (2.34-1ubuntu1) ...
Selecting previously unselected package fontconfig-config.
Preparing to unpack .../fontconfig-config_2.11.0-0ubuntu4.1_all.deb ...
Unpacking fontconfig-config (2.11.0-0ubuntu4.1) ...
Selecting previously unselected package libfreetype6:amd64.
Preparing to unpack .../libfreetype6_2.5.2-1ubuntu2.5_amd64.deb ...
Unpacking libfreetype6:amd64 (2.5.2-1ubuntu2.5) ...
Selecting previously unselected package libfontconfig1:amd64.
Preparing to unpack .../libfontconfig1_2.11.0-0ubuntu4.1_amd64.deb ...
Unpacking libfontconfig1:amd64 (2.11.0-0ubuntu4.1) ...
Selecting previously unselected package libjpeg-turbo8:amd64.
Preparing to unpack .../libjpeg-turbo8_1.3.0-0ubuntu2_amd64.deb ...
Unpacking libjpeg-turbo8:amd64 (1.3.0-0ubuntu2) ...
Selecting previously unselected package libjpeg8:amd64.
Preparing to unpack .../libjpeg8_8c-2ubuntu8_amd64.deb ...
Unpacking libjpeg8:amd64 (8c-2ubuntu8) ...
Selecting previously unselected package libjbig0:amd64.
Preparing to unpack .../libjbig0_2.0-2ubuntu4.1_amd64.deb ...
Unpacking libjbig0:amd64 (2.0-2ubuntu4.1) ...
Selecting previously unselected package libtiff5:amd64.
Preparing to unpack .../libtiff5_4.0.3-7ubuntu0.3_amd64.deb ...
Unpacking libtiff5:amd64 (4.0.3-7ubuntu0.3) ...
Selecting previously unselected package libvpx1:amd64.
Preparing to unpack .../libvpx1_1.3.0-2_amd64.deb ...
Unpacking libvpx1:amd64 (1.3.0-2) ...
Selecting previously unselected package libxpm4:amd64.
Preparing to unpack .../libxpm4_1%3a3.5.10-1_amd64.deb ...
Unpacking libxpm4:amd64 (1:3.5.10-1) ...
Selecting previously unselected package libgd3:amd64.
Preparing to unpack .../libgd3_2.1.0-3_amd64.deb ...
Unpacking libgd3:amd64 (2.1.0-3) ...
Selecting previously unselected package libxslt1.1:amd64.
Preparing to unpack .../libxslt1.1_1.1.28-2build1_amd64.deb ...
Unpacking libxslt1.1:amd64 (1.1.28-2build1) ...
Selecting previously unselected package geoip-database.
Preparing to unpack .../geoip-database_20140313-1_all.deb ...
Unpacking geoip-database (20140313-1) ...
Selecting previously unselected package xml-core.
Preparing to unpack .../xml-core_0.13+nmu2_all.deb ...
Unpacking xml-core (0.13+nmu2) ...
Selecting previously unselected package nginx-common.
Preparing to unpack .../nginx-common_1.4.6-1ubuntu3.4_all.deb ...
Unpacking nginx-common (1.4.6-1ubuntu3.4) ...
Selecting previously unselected package nginx-core.
Preparing to unpack .../nginx-core_1.4.6-1ubuntu3.4_amd64.deb ...
Unpacking nginx-core (1.4.6-1ubuntu3.4) ...
Selecting previously unselected package nginx.
Preparing to unpack .../nginx_1.4.6-1ubuntu3.4_all.deb ...
Unpacking nginx (1.4.6-1ubuntu3.4) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up libgeoip1:amd64 (1.6.0-1) ...
Setting up libxau6:amd64 (1:1.0.8-1) ...
Setting up libxdmcp6:amd64 (1:1.1.1-1) ...
Setting up libxcb1:amd64 (1.10-2ubuntu1) ...
Setting up libx11-data (2:1.6.2-1ubuntu2) ...
Setting up libx11-6:amd64 (2:1.6.2-1ubuntu2) ...
Setting up libxml2:amd64 (2.9.1+dfsg1-3ubuntu4.7) ...
Setting up sgml-base (1.26+nmu4ubuntu1) ...
Setting up fonts-dejavu-core (2.34-1ubuntu1) ...
Setting up fontconfig-config (2.11.0-0ubuntu4.1) ...
Setting up libfreetype6:amd64 (2.5.2-1ubuntu2.5) ...
Setting up libfontconfig1:amd64 (2.11.0-0ubuntu4.1) ...
Setting up libjpeg-turbo8:amd64 (1.3.0-0ubuntu2) ...
Setting up libjpeg8:amd64 (8c-2ubuntu8) ...
Setting up libjbig0:amd64 (2.0-2ubuntu4.1) ...
Setting up libtiff5:amd64 (4.0.3-7ubuntu0.3) ...
Setting up libvpx1:amd64 (1.3.0-2) ...
Setting up libxpm4:amd64 (1:3.5.10-1) ...
Setting up libgd3:amd64 (2.1.0-3) ...
Setting up libxslt1.1:amd64 (1.1.28-2build1) ...
Setting up geoip-database (20140313-1) ...
Setting up xml-core (0.13+nmu2) ...
Setting up nginx-common (1.4.6-1ubuntu3.4) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up nginx-core (1.4.6-1ubuntu3.4) ...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up nginx (1.4.6-1ubuntu3.4) ...
Processing triggers for libc-bin (2.19-0ubuntu6.7) ...
Processing triggers for sgml-base (1.26+nmu4ubuntu1) ...
 ---> 293eec6b3e82
Removing intermediate container 552975fd209f
Step 5 : RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
 ---> Running in f7badb373c66
 ---> dcbbd4f6447d
Removing intermediate container f7badb373c66
Step 6 : RUN chown -R www-data:www-data /var/lib/nginx
 ---> Running in 92a8c9d8864f
 ---> d1443e1b7d77
Removing intermediate container 92a8c9d8864f
Step 7 : VOLUME /data /etc/nginx/site-enabled /var/log/nginx
 ---> Running in ff00a9cf21bd
 ---> fcf1ababd686
Removing intermediate container ff00a9cf21bd
Step 8 : WORKDIR /etc/nginx
 ---> Running in b4d60bfe74d4
 ---> 872f8da3f243
Removing intermediate container b4d60bfe74d4
Step 9 : CMD nginx
 ---> Running in 196090f16f2d
 ---> 31a0e00a216f
Removing intermediate container 196090f16f2d
Step 10 : EXPOSE 80
 ---> Running in 51a5d96ba7e8
 ---> 635af20fa452
Removing intermediate container 51a5d96ba7e8
Step 11 : EXPOSE 443
 ---> Running in 3d804a8faefe
 ---> eb72a0239f62
Removing intermediate container 3d804a8faefe
Successfully built eb72a0239f62



sudo docker run --name hello-nginx -d -p 80:80 -v /root/data:/data hello:0.1
82d5ab6620cf1306a21d4c7e92f61447b665b9636f7c722635433dfe5ff96622

-d 컨테이너를 백그라운드로 실행

-p 80:80 옵션으로 호스트의 80포트와 컨테이너의 80 포트를 연결하고 외부로 노출 http://<호스트 IP>:80에 접속하면 컨테이너 80 포트로 접속된다.

-v /root/data:/data 호스트 /root/data 디렉터리를 컨테이너의 /data 디렉터리에 연결합니다.


sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                         NAMES
82d5ab6620cf        hello:0.1           "nginx"             2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp, 443/tcp   hello-nginx



history 명령으로 이미지 히스트로 살펴보기

opdev@opdev-web08:~/docker$ sudo docker history hello:0.1
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
eb72a0239f62        16 minutes ago      /bin/sh -c #(nop) EXPOSE 443/tcp                0 B                
635af20fa452        16 minutes ago      /bin/sh -c #(nop) EXPOSE 80/tcp                 0 B                
31a0e00a216f        16 minutes ago      /bin/sh -c #(nop) CMD ["nginx"]                 0 B                
872f8da3f243        16 minutes ago      /bin/sh -c #(nop) WORKDIR /etc/nginx            0 B                
fcf1ababd686        16 minutes ago      /bin/sh -c #(nop) VOLUME [/data /etc/nginx/si   0 B                
d1443e1b7d77        16 minutes ago      /bin/sh -c chown -R www-data:www-data /var/li   0 B                
dcbbd4f6447d        16 minutes ago      /bin/sh -c echo "\ndaemon off;" >> /etc/nginx   1.614 kB           
293eec6b3e82        16 minutes ago      /bin/sh -c apt-get install -y nginx             18.14 MB           
67bdfce4db7e        17 minutes ago      /bin/sh -c apt-get update                       21.57 MB           
c99fa9ed623d        18 minutes ago      /bin/sh -c #(nop) MAINTAINER Foo Bar <foo@bar   0 B                
14b59d36bae0        6 days ago          /bin/sh -c #(nop) CMD ["/bin/bash"]             0 B                
<missing>           6 days ago          /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/   1.895 kB           
<missing>           6 days ago          /bin/sh -c echo '#!/bin/sh' > /usr/sbin/polic   194.5 kB           
<missing>           6 days ago          /bin/sh -c #(nop) ADD file:620b1d9842ebe18eaa   187.8 MB  

docker history <이미지 이름>:<태그>


cp 명령으로 파일 꺼내기

sudo docker cp hello-nginx:/etc/ngninx/nginx.conf ./

docker cp <컨테이너 이름>:<경로> <호스트경로>


commit 명령으로 컨테이너의 변경사항을 이미지로 생성

sudo docker commit -a "Foo Bar <foo@bar.com>" -m "add hello.txt" hello-nginx hello:0.2
sha256:a726c1381855c6f0111f611f3dc00a118183432a66dcfed482b34ad4e939b0e6

docker commit <옵션> <컨테이너 이름> <이미지 이름>:<태그>



diff 명령으로 컨테이너에서 변경된 파일 확인

 sudo docker diff hello-nginx
A /data
C /etc
C /etc/nginx
A /etc/nginx/site-enabled
C /run
C /var
C /var/lib
C /var/lib/nginx
A /var/lib/nginx/body
A /var/lib/nginx/fastcgi
A /var/lib/nginx/proxy
A /var/lib/nginx/scgi
A /var/lib/nginx/uwsgi



inspect 명령으로 세부 정보 확인

sudo docker inspect hello-nginx
[
    {
        "Id": "82d5ab6620cf1306a21d4c7e92f61447b665b9636f7c722635433dfe5ff96622",
        "Created": "2016-02-24T05:58:17.80378228Z",
        "Path": "nginx",
        "Args": [],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2016-02-24T06:10:53.854737454Z",
            "FinishedAt": "2016-02-24T06:11:12.517151814Z"
        },
        "Image": "sha256:eb72a0239f625c7383439a674532bedb8443c19a2658773602a857a1127b1025",
        "ResolvConfPath": "/var/lib/docker/containers/82d5ab6620cf1306a21d4c7e92f61447b665b9636f7c722635433dfe5ff96622/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/82d5ab6620cf1306a21d4c7e92f61447b665b9636f7c722635433dfe5ff96622/hostname",
        "HostsPath": "/var/lib/docker/containers/82d5ab6620cf1306a21d4c7e92f61447b665b9636f7c722635433dfe5ff96622/hosts",
        "LogPath": "/var/lib/docker/containers/82d5ab6620cf1306a21d4c7e92f61447b665b9636f7c722635433dfe5ff96622/82d5ab6620cf1306a21d4c7e92f61447b665b9636f7c722635433dfe5ff96622-json.log",
        "Name": "/hello-nginx",
        "RestartCount": 0,
        "Driver": "aufs",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/root/data:/data"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "80"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "ShmSize": 67108864,
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "KernelMemory": 0,
            "Memory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null
        },
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "Mounts": [
            {
                "Name": "86349c8f898c2b36c576addc43800f14caf59284f367cf7a793773608d63cd25",
                "Source": "/var/lib/docker/volumes/86349c8f898c2b36c576addc43800f14caf59284f367cf7a793773608d63cd25/_data",
                "Destination": "/etc/nginx/site-enabled",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Name": "3f0d4eb6da43d84e5744abb98d09a330a468e2b41b563a2d213b6ee0acdfa433",
                "Source": "/var/lib/docker/volumes/3f0d4eb6da43d84e5744abb98d09a330a468e2b41b563a2d213b6ee0acdfa433/_data",
                "Destination": "/var/log/nginx",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Source": "/root/data",
                "Destination": "/data",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "82d5ab6620cf",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "443/tcp": {},
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "nginx"
            ],
            "Image": "hello:0.1",
            "Volumes": {
                "/data": {},
                "/etc/nginx/site-enabled": {},
                "/var/log/nginx": {}
            },
            "WorkingDir": "/etc/nginx",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {},
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "219204db7e9b8348cb2c14bc4365049b8de3922c5a698667d04cc92094a06370",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": null,
            "SandboxKey": "/var/run/docker/netns/219204db7e9b",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "94bf88d00078dd837adfb772660b032eea884fcef19a136b6293f03ff2fc81f9",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": ""
                }
            }
        }
    }
]
docker inspect <이미지 또는 컨테이너 이름>



docker 개인 저장소 구축

레지스트리 이미지 받기

sudo docker pull registry:latest

레지스트리 이미지를 컨테이너로 실행

sudo docker run -d -p 5000:5000 --name hello-registry -v /tmp/registry:/tmp/registry registry


push 명령으로 이미지 올리기

sudo docker tag hello:0.1 localhost:5000/hello:0.1

docker tag <이미지 이름>:<태그> <Docker 레지스트리 URL>/<이미지 이름>:<태그>

sudo docker push localhost:5000/hello:0.1

docker push <Docker 레지스트리 URL>/<이미지 이름>:<태그>

sudo docker pull 192.168.0.39:5000/hello:0.1




docker 컨테이너 연결하기

sudo docker run -name db -d mongo


sudo docker run --name web -d -p 80:80 --link db:db ngnix

--link <컨테이너 이름>:<별칭> (같은 서버에 있는 컨테이너끼리 연결)

컨테이너 안에서 다른 컨테이너에 접속할 떄는 <별칠>:<포트 번호> 형식으로 사용



다른 서버의 docker 컨테이너에 연결하기

앰배서더 컨테이너

CMD env | grep _TCP= | \

send 's/.*_RPOT_\(0-9]*\)_TCP=tcp:\/\/\(.*\):\(.*\)/socat \

TCP4-LISTEN:\1,fork,reuseaddr TCP4:\2:\3 \&/' \

| sh && top



Docker로 애플리케이션 배포하기

git push => git hook => docker 이미지 생성 => 컨테이너 실행


FROM ubuntu:14.04


RUN apt-get update

RUN apt-get install -y nodejs npm


ADD app.js /var/www/app.js

ADD package.json /var/www/package.json


WROKDIR /var/www


RUN npm install


CMD nodejs app.js


서버에 git, docker 설치

sudo apt-get install git

git init exampleapp

git config receive.denycurrentbranch ignore


sudo apt-get install docker.io

sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker


sudo usermod -aG docker ${USER}

sudo service docker restart


서버에 git hook 설정

.git/hooks/post-receive

#!/bin/bash


APP_NAME=exampleapp

APP_DIR=$HOME/$APP_NAME

REVISION=$(expr substr $(git rev-parse --verify HEAD) 1 7)


GIT_WORK_TREE=$APP_DIR git checkout -f


cd $APP_DIR

docker build --tag $APP_NAME:$REVISION .

docker stop $APP_NAME

docker rm $APP_NAME

docker run -d --name $APP_NAME -p 80:80 $APP_NAME:$REVISION


git remote add origin <서버 사용자 계정>@<서버 IP 주소 또는 도메인>:exampleapp

git push origin master



서버 여러대에 애플리케이션 배포하기

git push => git hook => docker 이미지 생성 => docker 레지스트리 등록 => 배포 서버 ssh로 어플 서버에 docker pull => docker run 실행

배포서버 docker 설치까지 동일

docker 레지스트리 설정

sudo docker pull registry:latest

sudo docker run -d -p 5000:5000 -name example-registry -v /tmp/registry:/tmp/registry registry


배포할 서버에 SSH 설정, docker 설치


.git/hooks/post-receive

#!/bin/bash


APP_NAME=exampleapp

APP_DIR=$HOME/$APP_NAME

REVISION=$(expr substr $(git rev-parse --verify HEAD) 1 7)


REGISTRY=192.168.0.40:5000

APP_SERVERS=(

"pyrasis@192.168.0.101"

"pyrasis@192.168.0.102"

)


GIT_WORK_TREE=$APP_DIR git checkout -f


cd $APP_DIR

docker build --tag $APP_NAME:$REVISION .

docker tag $APP_NAME:$REVISION $REGISTRY/$APP_NAME:$REVISION

docker push $REGISTRY/$APP_NAME:$REVISION


SSH="ssh -o StrictHostKeyChecking=no"

for SERVER in ${APP_SERVERS[@]}

do

$SSH $SERVER docker pull $REGISTRY/$APP_NAME:$REVISION

$SSH $SERVER docker stop $APP_NAME

$SSH $SERVER docker rm $APP_NAME

$SSH $SERVER docker run -d --name $APP_NAME -p 80:80 $REGISTRY/$APP_NAME:$REVISION

done









'dev' 카테고리의 다른 글

LLM 프롬프트 디자인  (0) 2024.11.12
Dapr Quick Start  (0) 2024.06.05
git remote 변경, hook 처리  (0) 2014.01.10
용어 정리  (0) 2010.08.24
java 개발자의 Ubuntu 10.04 설치 및 사용 후기  (0) 2010.06.09