[docker] Docker 기본 사용법 #01

Docker install

# Docker EE 와 CE 차이

– Docker Enterprise Edition / Docker Community Edition

 

# Docker install

Ubuntu 16.04

test@docker-test:~$ curl -s https://get.docker.com/ | sudo sh

 

CentOS7

# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# yum install docker-ce
# systemctl enable docker
# systemctl start docker

 

 

# sudo 없이 사용하기

docker는 기본적으로 root권한이 필요합니다. root가 아닌 사용자가 sudo없이 사용하려면 해당 사용자를 docker그룹에 추가합니다.

sudo usermod -aG docker $USER # 현재 접속중인 사용자에게 권한주기

test@docker-test:~$ sudo usermod -aG docker test

사용자가 로그인 중이라면 다시 로그인 후 권한이 적용됩니다.

# Docker Version 확인

test@docker-test:~$ docker version
Client:
 Version:      17.03.0-ce
 API version:  1.26
 Go version:   go1.7.5
 Git commit:   60ccb22
 Built:        Thu Feb 23 11:02:43 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.03.0-ce
 API version:  1.26 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   60ccb22
 Built:        Thu Feb 23 11:02:43 2017
 OS/Arch:      linux/amd64
 Experimental: false
test@docker-test:~$

 

# Container 실행

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

옵션 설명

-d : detached mode 흔히 말하는 백그라운드 모드
-p : 호스트와 컨테이너의 포트를 연결 (포워딩)
-v : 호스트와 컨테이너의 디렉토리를 연결 (마운트)
-e : 컨테이너 내에서 사용할 환경변수 설정
-name : 컨테이너 이름 설정
-rm : 프로세스 종료시 컨테이너 자동제거
-it : -i 와 -t 를 동시에 사용한것으로 터미널 입력을 위한 옵션
-link : 컨테이너 연결 [컨테이너명:별칭]

 

# Ubuntu 16.04 Container 생성

test@docker-test:~$ docker run ubuntu:16.04
Unable to find image 'ubuntu:16.04' locally
16.04: Pulling from library/ubuntu
d54efb8db41d: Pull complete
f8b845f45a87: Pull complete
e8db7bf7c39f: Pull complete
9654c40e9079: Pull complete
6d9ef359eaaa: Pull complete
Digest: sha256:dd7808d8792c9841d0b460122f1acf0a2dd1f56404f8d1e56298048885e45535
Status: Downloaded newer image for ubuntu:16.04
test@docker-test:~$

 

# Ubuntu 16.04 컨테이너 실행 및 컨테이너 접속

test@docker-test:~$ docker run --rm -it ubuntu:16.04 /bin/bash
root@ecc3042c5486:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@ecc3042c5486:/# cat /etc/issue
Ubuntu 16.04.2 LTS \n \l

root@ecc3042c5486:/# uname -a
Linux ecc3042c5486 4.4.0-62-generic #83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
root@ecc3042c5486:/#

컨테이너 내부에 들어가기 위해 bash쉘을 실행 키보드 입력을 위해 -it 옵션을 사용
접속을 종료 하면 컨테이너가 자동으로 삭제 되도록 –rm 옵션을 추가합니다.

 

# apache Container 생성

-p 옵션을 사용하여 외부와 내부 80 포트를 연결 합니다.

–name 옵션을 사용하여 httpd-test 로 컨테이너 name 을 지정 합니다.

test@docker-test:~$ docker run -d -p 80:80 --name httpd-test httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
f2b6b4884fc8: Pull complete
b58fe2a5c9f1: Pull complete
e797fea70c45: Pull complete
6c7b4723e810: Pull complete
02074013c987: Pull complete
a4a11b801d86: Pull complete
70d17a98bee0: Pull complete
Digest: sha256:8359424a58cf59f1ea6a1a55e3974d5d569a510ebec0004357cf200adce5f27a
Status: Downloaded newer image for httpd:latest
a88a9dbfe728a3d9a9ea1c209b6773eb24d7de63a16de97519d2acab5d762650
test@docker-test:~$

 

컨테이너 확인

test@docker-test:~$ docker ps
CONTAINER ID        IMAGE               COMMAND              CREATED              STATUS              PORTS                NAMES
a88a9dbfe728        httpd               "httpd-foreground"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp   httpd-test
test@docker-test:~$

 

접속 확인

 

컨테이너 정지시 docker stop $Container_id 를 사용합니다.

test@docker-test:~$ docker ps
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                NAMES
a88a9dbfe728        httpd               "httpd-foreground"   7 minutes ago       Up 6 minutes        0.0.0.0:80->80/tcp   httpd-test
test@docker-test:~$ docker stop a88a9dbfe728
a88a9dbfe728
test@docker-test:~$

 

컨테이너 재사용시 docker ps -a 으로 확인후

docker start $Container_id 를 하시면 됩니다.

test@docker-test:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                      PORTS               NAMES
a88a9dbfe728        httpd               "httpd-foreground"   8 minutes ago       Exited (0) 18 seconds ago                       httpd-test
test@docker-test:~$ docker start a88a9dbfe728
a88a9dbfe728
test@docker-test:~$

 

컨테이너 삭제시 docker rm 을 사용합니다.

test@docker-test:~$ docker rm a88a9dbfe728
a88a9dbfe728

 

docker run 으로 image 에서 만든 컨테이너만 삭제 되며 해당 이미지는 남아 있습니다.

test@docker-test:~$ docker images |grep -i httpd
httpd                 latest              e2a1033f4f86        2 hours ago         178MB
test@docker-test:~$

 

# MySQL5.7 Container 생성

test@docker-test:~$ docker run -d -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=true --name mysql mysql:5.7
Unable to find image 'mysql:5.7' locally
5.7: Pulling from library/mysql
693502eb7dfb: Already exists
08d0e9d74b1b: Pull complete
e700ebfbe6bc: Pull complete
f718f1976629: Pull complete
575a0830e278: Pull complete
8461dfcf361d: Pull complete
349434898dfb: Pull complete
78d351522443: Pull complete
21897ab46952: Pull complete
ca6ffbbedc10: Pull complete
ba8ff064032b: Pull complete
Digest: sha256:6d4b33d189d62afe590ee4b35f92aae31ffa79ccc4d4db8bd3d3b893c8019596
Status: Downloaded newer image for mysql:5.7
f33c70888d81987a40957ea0f65fcf108d6a15248a25c139eebcf60ac6de4474
test@docker-test:~$

 

Host OS 에서 접속 테스트

test@docker-test:~$ mysql -h127.0.0.1 -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> quit
Bye
test@docker-test:~$

 

# mariadb Container

docker search mariadb 명령어로 배포중인 mariadb 컨테이너를 확인 할수 있습니다.

공식 image 의 경우 OFFICIAL [OK] 를 확인 할수 있습니다.

test@ubuntu1604:~$ docker search mariadb
NAME                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mariadb                                MariaDB is a community-developed fork of M...   1462      [OK]
bitnami/mariadb                        Bitnami MariaDB Docker Image                    40                   [OK]
paintedfox/mariadb                     A docker image for running MariaDB 5.5, a ...   29                   [OK]
toughiq/mariadb-cluster                Dockerized Automated MariaDB Galera Cluste...   19                   [OK]
linuxserver/mariadb                    A Mariadb container, brought to you by Lin...   18
million12/mariadb                      MariaDB 10 on CentOS-7 with UTF8 defaults       14                   [OK]
webhippie/mariadb                      Docker images for mariadb                       10                   [OK]
colinmollenhour/mariadb-galera-swarm   MariaDb w/ Galera Cluster, DNS-based servi...   9                    [OK]
monitoringartist/zabbix-db-mariadb     Docker image of MariaDB optimized for Zabbix    8                    [OK]
panubo/mariadb-galera                  MariaDB Galera Cluster                          8                    [OK]
wodby/mariadb-alpine                   mariadb-alpine                                  5                    [OK]
wodby/drupal-mariadb                   MariaDB for Drupal                              4                    [OK]
tianon/mariadb                         DEPRECATED; use mariadb:* -- ♪ "I just met...   4                    [OK]
tutum/mariadb                          Base docker image to run a MariaDB databas...   3
lsioarmhf/mariadb                      ARMHF based Linuxserver.io image of mariadb     2
juanluisbaptiste/otrs-mariadb          Preconfigured MariaDB database for OTRS         1                    [OK]
wodby/mariadb                                                                          1                    [OK]
drupaldocker/mariadb                   MariaDB for Drupal                              1                    [OK]
needo/mariadb                                                                          1                    [OK]
jkleczkowski/mariadb                   You can change server charset and collatio...   0                    [OK]
benyoo/mariadb                         Alpine MariaDb run in docker                    0                    [OK]
centos/mariadb-101-centos7             MariaDB 10.1 SQL Database Server Docker image   0
whatwedo/mariadb                                                                       0                    [OK]
kitpages/mariadb-galera                MariaDB with Galera                             0                    [OK]
gjchen/mariadb                         Alpine Linux with MariaDB configured.           0                    [OK]
test@ubuntu1604:~$

 

mariadb 이미지 다운

mariadb:latest 의 경우 마지막 버전을 의미합니다.

test@ubuntu1604:~$ docker pull mariadb:latest
latest: Pulling from library/mariadb
ad74af05f5a2: Pull complete
0639788facc8: Pull complete
de70fa77eb2b: Pull complete
724179e94999: Pull complete
57fbc7ff5cf1: Pull complete
9d5794cf4e5c: Pull complete
b29a1331369b: Pull complete
66ad135be9a5: Pull complete
bb60a21b125b: Pull complete
8313b6b444b9: Pull complete
5891f0cb9ab8: Pull complete
55b71a694f6c: Pull complete
Digest: sha256:8ea33570152349b827d7121b88dc3f44a64e1cc7646cfae01faee4824b9b0007
Status: Downloaded newer image for mariadb:latest
test@ubuntu1604:~$

 

mariadb 컨테이너을 실행 합니다.

port 3306 으로 맵핑 하고 password 를 my-secret-pw 로 지정 하였습니다.

test@ubuntu1604:~$ docker run --name mariadb -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mariadb:latest
(MYSQL_ROOT_PASSWORD= mariadb root password 입니다.)

test@ubuntu1604:~$ docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                                      NAMES
5e66b8ea5c14        mariadb:latest           "docker-entrypoint..."   4 seconds ago       Up 4 seconds        0.0.0.0:3306->3306/tcp                                     mariadb
c8c1c38656cb        sanbsd/pure-ftpd:14.04   "/bin/sh -c '/usr/..."   18 hours ago        Up 36 minutes       0.0.0.0:21->21/tcp, 0.0.0.0:20000-20099->20000-20099/tcp   ftpd
331d935c6000        ssh-server               "/usr/sbin/sshd -D"      18 hours ago        Up 36 minutes       0.0.0.0:22222->22/tcp                                      ssh-server

 

Host 에서 mariadb 접속 테스트

-p 3306:3306 으로 맵핑 되어 있기 때문 -h 127.0.0.1 로 접속 할수 있습니다.

test@ubuntu1604:~$ mysql -h127.0.0.1 -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 10.2.7-MariaDB-10.2.7+maria~jessie mariadb.org binary distribution

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> status;
--------------
mysql  Ver 15.1 Distrib 10.0.29-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Connection id:          14
Current database:
Current user:           root@172.17.0.1
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server:                 MariaDB
Server version:         10.2.7-MariaDB-10.2.7+maria~jessie mariadb.org binary distribution
Protocol version:       10
Connection:             127.0.0.1 via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:               3306
Uptime:                 14 min 11 sec

Threads: 7  Questions: 48  Slow queries: 0  Opens: 36  Flush tables: 1  Open tables: 30  Queries per second avg: 0.056
--------------

MariaDB [(none)]>

 

 

 

댓글 남기기