도커에 CentOS 실행하기
docker run --rm -it centos:8 /bin/sh
Go
복사
•
도커는 다양한 리눅스 배포판을 실행할 수 있습니다. 공통점은 모두 동일한 커널을 사
용한다는 점입니다.
•
Ubuntu 또는 CentOS에 포함된 다양한 기본기능이 필요 없는 경우, Alpine 이라는 초
소형 (5MB) 이미지를 사용할 수도 있습니다.
도커에 웹 어플리케이션 실행하기
docker run --rm -p 5678:5678 hashicorp/http-echo -text="hello world"
Go
복사
•
detached mode(백그라운드 모드)로 실행하기 위해 -d 옵션을 추가하고 -p 옵션을
추가하여 컨테이너 포트를 호스트의 포트로 연결하였습니다.
•
브라우저를 열고 localhost:5678에 접속하면 메시지를 볼 수 있습니다.
•
hashicorp는 hashicorp에서 만든 웹서버이다.
•
이걸 실행 하고 터미널창을 하나 더 열어서 아래의 명령어를 입력해본다.
curl localhost:5678
Go
복사
•
위의 명령어를 실행하면 아래의 문구가 나온다.
(base) glory@Gloryui-MacBookPro ~ % curl localhost:5678
hello world
(base) glory@Gloryui-MacBookPro ~ % curl localhost:5678
hello world
(base) glory@Gloryui-MacBookPro ~ %
Go
복사
•
그리고 이전에 docker를 열었던 창에서는 다음과 같은 메시지가 나오게 된다.
2021/02/02 14:31:02 localhost:5678 172.17.0.1:44646 "GET / HTTP/1.1" 200 12 "curl/7.64.1" 31µs
2021/02/02 14:31:08 localhost:5678 172.17.0.1:44650 "GET / HTTP/1.1" 200 12 "curl/7.64.1" 17.6µs
Go
복사
•
(중요)여기서 놀라운 점은 docker run --rm -p 5678:5678 hashicorp/http-echo -text="hello world" 명령어에서 5678대신 5679 라고 쓰고 또 실행시키면 웹서버 2개를 동시에 열수도 있다는 점이다.
도커에 Redis 실행하기
•
명령어
docker run --rm -p 1234:6379 redis
Go
복사
•
Redis라는 메모리기반 데이터베이스를 실행합니다.
•
1234에 redis를 설치하는 것으로 다른 cmd창 열어서 다음 명령어를 검색한다. (telnet 설치 필수)
$ telnet localhost 1234
Go
복사
•
명령어에 set hello world 라고 검색하면
•
+OK
•
이게 나오며
•
get hello
•
이걸 검색하면
•
$5
•
world
•
이렇게 나오는데 hello에 world를 집어 넣고 hello를 검색하면 world가 나오는 시스템이다.
•
종료하려면 quit를 써주자
•
telenet이 설치가 안되었다면 brew를 이용해서 설치해주자 (brew 필수)
$ brew tap theeternalsw0rd/telnet
$ brew install telnet
Go
복사
도커에 MySQL 실행하기
•
명령어
docker run -d -p 3306:3306 \
-e MYSQL_ALLOW_EMPTY_PASSWORD=true \
--name mysql \
mysql:5.7
Go
복사
•
실행화면
(base) glory@Gloryui-MacBookPro ~ % 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
aaaaaaaaaaaa: Already exists
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
Digest: sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Status: Downloaded newer image for mysql:5.7
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
(base) glory@Gloryui-MacBookPro ~ %
Go
복사
•
MySQL 데이터베이스를 실행합니다.
•
명령어
docker exec -it mysql mysql
Go
복사
•
실행화면
(base) glory@Gloryui-MacBookPro ~ % docker exec -it mysql mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
Go
복사
•
아래처럼 기입 하면 현재 DB도 확인할 수 있다.
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
Go
복사
•
데이터베이스에 워드프레스를 넣을려고 하면 이렇게 하면 된다. (명령어)
create database wp CHARACTER SET utf8;
grant all privileges on wp.* to wp@'%' identified by 'wp';
flush privileges;
quit
Go
복사
•
실행화면
mysql> create database wp CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on wp.* to wp@'%' identified by 'wp';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
(base) glory@Gloryui-MacBookPro ~ %
Go
복사
EXEC 명령어
•
exec 명령어는 run 명령어와 달리 실행중인 도커 컨테이너에 접속할 때 사용하며 컨테 이너 안에 ssh server등을 설치하지 않고 exec 명령어로 접속합니다.
•
도커는 다양한 데이터베이스를 손쉽게 생성/삭제할 수 있기 때문에 개발할때도 많이 사용합니다.
도커에 워드프레스 블로그 실행하기
•
명령어
docker run -d -p 8080:80 \
-e WORDPRESS_DB_HOST=host.docker.internal \
-e WORDPRESS_DB_NAME=wp \
-e WORDPRESS_DB_USER=wp \
-e WORDPRESS_DB_PASSWORD=wp \
wordpress
Go
복사
•
실행화면
(base) glory@Gloryui-MacBookPro ~ % docker run -d -p 8080:80 \
-e WORDPRESS_DB_HOST=host.docker.internal \
-e WORDPRESS_DB_NAME=wp \
-e WORDPRESS_DB_USER=wp \
-e WORDPRESS_DB_PASSWORD=wp \
wordpress
Unable to find image 'wordpress:latest' locally
latest: Pulling from library/wordpress
aaaaaaaaaaaa: Already exists
aaaaaaaaaaaa: Pulling fs layer
aaaaaaaaaaaa: Pulling fs layer
aaaaaaaaaaaa: Pulling fs layer
aaaaaaaaaaaa: Pulling fs layer
aaaaaaaaaaaa: Pulling fs layer
aaaaaaaaaaaa: Pulling fs layer
aaaaaaaaaaaa: Pulling fs layer
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
aaaaaaaaaaaa: Pull complete
Digest: sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Status: Downloaded newer image for wordpress:latest
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
(base) glory@Gloryui-MacBookPro ~ %
Go
복사
•
앞에서 만든 MySQL을 실행한 상태에서 생성합니다.
•
웹브라우저 localhost:8080으로 접속합니다.