별도의 네트워크를 위한 기본네트워크가 생성되고 컨테이너도 생성된다.
네트워크를 지정하지 않으면 별도의 네트워크가 생성된다. 하지만, 기존에 생성되어 있는 네트워크를 재사용하고싶다면
rapa@rapa:~/0822/test1$ docker network create test1net
b9c5b3224f5ee48d6b9778ba4d9f3110b02ab81d08cfdf15f70ab4b6151f4bf3
rapa@rapa:~/0822/test1$ docker container run -it --name centos01 --net test1net centos:7
[root@e721ff00ce83 /]#
ctrl+p ctrl+q로 빠져 나오기
centos01 컨테이너의 네트워크 정보 확인
rapa@rapa:~/0822/test1$ docker container inspect centos01 --format "{{.HostConfig.NetworkMode}}"
test1net
yaml파일에서 network를 지정해서 그냥 하게된다면 앞에 디렉토리 이름이 붙게 되어 적합하지 않다.
external: true를 쓰면, 기존에 만든 test1net 네트워크를 쓰게 한다는 뜻으로, 아래에 정의하고, web과 db에 networks를 추가 해준다.
rapa@rapa:~/0822/test1$ cat docker-compose.yml
version: '3.7'
services:
web:
image: httpd
ports:
- "80"
command: httpd -D FOREGROUND
depends_on:
- db
links:
- db:mysql
networks:
- test1net
db:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=test123
- MYSQL_DATABASE=testdb
networks:
- test1net
networks:
test1net:
external: true # 새로 test1_test1net이 만들어지지않고, 기존 test1net에 연결된다.
rapa@rapa:~/0822/test1$ docker-compose up -d
Creating test1_db_1 ... done
Creating test1_web_1 ... done
rapa@rapa:~/0822/test1$ docker container inspect test1_db_1 --format "{{.HostConfig.NetworkMode}}"
test1net
rapa@rapa:~/0822/test1$ docker container inspect test1_web_1 --format "{{.HostConfig.NetworkMode}}"
test1net
rapa@rapa:~/0822/test1$ docker volume create testvol2
testvol2
rapa@rapa:~/0822/test1$ cat docker-compose.yml
version: '3.7'
services:
web:
image: httpd
ports:
- "80"
command: httpd -D FOREGROUND
depends_on:
- db
links:
- db:mysql
networks:
- test1net
volumes:
- testvol2:/usr/local/apache2/htdocs
db:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=test123
- MYSQL_DATABASE=testdb
networks:
- test1net
networks:
test1net:
external: true
volumes:
testvol2:
external: true
rapa@rapa:~/0822/test1$ dc up -d
test1_db_1 is up-to-date
Recreating test1_web_1 ... done
만들어 놓은 testvol2를 가져다 쓰기 때문에 db를 새로 만드는 내용이 없음
rapa@rapa:~/0822/test1$ dc down
Stopping test1_web_1 ... done
Stopping test1_db_1 ... done
Removing test1_web_1 ... done
Removing test1_db_1 ... done
Network test1net is external, skipping
rapa@rapa:~/0822/test1$ docker volume ls
DRIVER VOLUME NAME
local 0f3246bbc0a89ff2cb31e31447e8cebbbe9dc25420e8b58c75e8204dd0aaf852
local 2f428ea91feb0e4f519780557800916f7c8f9890a2b449b48477fab47ed52c32
local 46b5dcb46e72f7008fe4ef9380de903c9aab2601ef266a91dc818e66c3018980
local 0840da823c36b361d4ad0474008c2f6f016e78aea68d816b99f3d44671f9cf13
local 926e7fcd06454d6a893fc581e0a2ffd93ac9709f4c9a02f735f4f8e0b29c122d
local 54685ba7f52e548375c1efa585fab241a51cbea9b8d1ec1a3bef62cc2d719daa
local bec956610a9982b9a05296d43dcd046bc0068dcb7119169afd59da7646240a30
local c4b4a7f1e69f2397012df445a20fd0c308730731946c5df3917135464c491181
local c4beb363c9f4b3019fb25cadedbe1025c2563921d4d61de72b97071560d71ba0
local ef0cb1ba9a4eadb584b34b1326235f65987f030612b23685ee5456544701d1af
local f2bb3af6c4dd188c6a17f3682bf4880f63e556abef765d0e61efe7798411bc18
local test1_testvol1
local testvol2
local wpdb1
/home/rapa/0822/test1/shared 디렉토리를 컨테이너의 root와 연결하겠다. (volumes 부분)
rapa@rapa:~/0822/test1$ cat docker-compose.yml
version: '3.7'
services:
web:
image: httpd
ports:
- "80"
command: httpd -D FOREGROUND
depends_on:
- db
links:
- db:mysql
networks:
- test1net
volumes:
- testvol2:/usr/local/apache2/htdocs
- /home/rapa/0822/test1/shared:/root
db:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=test123
- MYSQL_DATABASE=testdb
networks:
- test1net
networks:
test1net:
external: true
volumes:
testvol2:
external: true
rapa@rapa:~/0822/test1$ docker-compose up -d
Creating test1_db_1 ... done
Creating test1_web_1 ... done
rapa@rapa:~/0822/test1$ docker container exec test1_web_1 ls /root
test.txt
rapa@rapa:~/0822/test1$ touch shared/ddd.txt
rapa@rapa:~/0822/test1$ docker container exec test1_web_1 ls /root
ddd.txt
test.txt