root@ubuntu:/java# apt-get -y install docker-compose
root@ubuntu:/java# docker-compose -version
root@ubuntu:/compose# docker-compose -version
docker-compose version 1.25.0, build unknown
root@ubuntu:/java# mkdir /compose
root@ubuntu:/java# cd /compose/
root@ubuntu:/compose# vi docker-compose.yml
version: "3" # 처음에는 버전을 지정해야 함
services:
my_nginx: # 컨테이너명
image: nginx
ports: #<HOST_PORT>:<CONTAINER_PORT>
- "7676:80" #""로 안 묶으면 시간으로 인식됨
root@ubuntu:/compose# docker-compose up --detach
root@ubuntu:/compose# docker-compose up --detach
Creating network "compose_default" with the default driver
Pulling my_nginx (nginx:)...
latest: Pulling from library/nginx
025c56f98b67: Pull complete
ca9c7f45d396: Pull complete
ed6bd111fc08: Pull complete
e25b13a5f70d: Pull complete
9bbabac55ab6: Pull complete
e5c9ba265ded: Pull complete
Digest: sha256:ab589a3c466e347b1c0573be23356676df90cd7ce2dbf6ec332a5f0a8b5e59db
Status: Downloaded newer image for nginx:latest
Creating compose_my_nginx_1 ... done
-d
는 사실 --detach
root@ubuntu:/compose# curl localhost:7676
root@ubuntu:/compose# curl localhost:7676
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="<http://nginx.org/>">nginx.org</a>.<br/>
Commercial support is available at
<a href="<http://nginx.com/>">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
캬 오졋다
root@ubuntu:/compose# docker-compose ps
root@ubuntu:/compose# docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------
compose_my_nginx_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:7676->80/tcp,:::7676->
80/tcp
설정한 이름은 my_nginx지만 하나만 띄워지는 게 아니므로 다른 식별자가 붙는다. → composer_<NAME>_<NUMBER>
root@ubuntu:/compose# docker-compose down
root@ubuntu:/compose# docker-compose up -d
**root@ubuntu:/compose# docker-compose down**
Stopping compose_my_nginx_1 ... done
Removing compose_my_nginx_1 ... done
Removing network compose_default
**root@ubuntu:/compose# docker-compose up -d**
Creating network "compose_default" with the default driver
Creating compose_my_nginx_1 ... done
root@ubuntu:/compose# vi docker-compose.yml
version: "3" # 처음에는 버전을 지정해야 함
services:
my_nginx: # 컨테이너명
image: nginx
ports: #<HOST_PORT>:<CONTAINER_PORT>
- "**~~7676~~**7575:80" #""로 안 묶으면 시간으로 인식됨
root@ubuntu:/compose# docker-compose up -d
root@ubuntu:/compose# docker-compose up -d
Recreating compose_my_nginx_1 ... done
재생성된다.
root@ubuntu:/compose# curl localhost:7676
root@ubuntu:/compose# curl localhost:7575
root@ubuntu:/compose# curl localhost:7676
curl: (7) Failed to connect to localhost port 7676: Connection refused
root@ubuntu:/compose# curl localhost:7575
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="<http://nginx.org/>">nginx.org</a>.<br/>
Commercial support is available at
<a href="<http://nginx.com/>">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
이제 7575
로만 접속이 가능하다.
root@ubuntu:/compose# vi docker-compose.yml
version: "3" # 처음에는 버전을 지정해야 함
services:
my_nginx: # 컨테이너명
image: nginx
ports: #<HOST_PORT>:<CONTAINER_PORT>
- "7575:80" #""로 안 묶으면 시간으로 인식됨
**volumes:
- "/data:/usr/share/nginx/html"**
volumes 추가 ㄱㄱ
root@ubuntu:/compose# mkdir /data
root@ubuntu:/compose# cd /data
root@ubuntu:/data# touch index.html
root@ubuntu:/data# echo hello > index.html
root@ubuntu:/data# ls /data
root@ubuntu:/data# ls /data
index.html
root@ubuntu:/compose# vi docker-compose.yml
version: "3" # 처음에는 버전을 지정해야 함
services:
my_nginx: # 컨테이너명
image: nginx
ports: # <HOST_PORT>:<CONTAINER_PORT>
- "7575:80" # ""로 안 묶으면 시간으로 인식됨
volumes:
- "/data:/usr/share/nginx/html"
**build: # ./db-Dockerfile로 image를 build한다
context: .
dockerfile: db-Dockerfile**
root@ubuntu:/compose# docker run -d -P --name nginx1 nginx
root@ubuntu:/compose# docker run -d -P --name nginx1 nginx
78fe22da97f63556dbdb1419edd775d5ed4c13ac6109f827e140503315ead027
-P
Host의 port를 random으로 부여한다.
root@ubuntu:/compose# docker ps
root@ubuntu:/compose# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
**78fe22da97f6 nginx "/docker-entrypoint.…" 20 seconds ago Up 19 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginx1**
dd8e1f0173a5 nginx "/docker-entrypoint.…" 14 minutes ago Up 14 minutes 0.0.0.0:7575->80/tcp, :::7575->80/tcp compose-my_nginx-1
root@ubuntu:/compose# vi docker-compose.yml
version: "3" # 처음에는 버전을 지정해야 함
services:
my_nginx: # 컨테이너명
image: nginx
ports: #<HOST_PORT>:<CONTAINER_PORT>
- "7575:80" #""로 안 묶으면 시간으로 인식됨
volumes:
- "/data:/usr/share/nginx/html"
build: # ./db-Dockerfile로 image를 build한다
context: .
dockerfile: db-Dockerfile
**expose: # 컨테이너 내부 포트를 고정한다.
- "80"**
컨테이너 내부 포트를 <PORT_NUMBER>로 고정한다.
expose
옵션을 사용하면 랜덤으로 부여된 host port와 expose
로 고정된 container 내부 포트를 연결할 수 있다.
Dockerfile의 EXPOSE <PORT_NUMBER>
와 같다.
root@ubuntu:/compose# vi docker-compose.yml
version: "3" # 처음에는 버전을 지정해야 함
services:
my_nginx: # 컨테이너명
image: nginx
ports: #<HOST_PORT>:<CONTAINER_PORT>
- "7575:80" #""로 안 묶으면 시간으로 인식됨
volumes:
- "/data:/usr/share/nginx/html"
build: # ./db-Dockerfile로 image를 build한다
context: .
dockerfile: db-Dockerfile
expose: # 컨테이너 내부 포트를 고정한다.
- "80"
**links:
- "db"
depends_on: # db 컨테이너에 의존성이 걸려있다.
- "db"
db:
image: mariadb**
depends_on
윗줄부터 읽으므로 links: - "db"
에서는 아직 db
컨테이너가 없으면 연결이 불가능하다.
즉 links
옵션은 db
컨테이너에 의존성이 걸려 있다.
root@ubuntu:/compose# docker-compose up -d
root@ubuntu:/compose# docker-compose up -d
Pulling db (mariadb:)...
latest: Pulling from library/mariadb
e96e057aae67: Pull complete
13360dd5ccba: Pull complete
dd5c4b73b925: Pull complete
7f870965a3fa: Pull complete
fd3f1ea3ff32: Pull complete
a57a6862e470: Pull complete
039ecd174df7: Pull complete
cbb1c35b18de: Pull complete
Digest: sha256:a98834606bea049d3094d0d90964eb749d9a10c46f60e58e67ca75a6a155c1ad
Status: Downloaded newer image for mariadb:latest
Creating compose_db_1 ... done
Recreating compose-my_nginx-1 ... done
root@ubuntu:/compose# vi docker-compose.yml
root@ubuntu:/compose# vi docker-compose.yml
version: "3" # 처음에는 버전을 지정해야 함
services:
my_nginx: # 컨테이너명
image: nginx
ports: #<HOST_PORT>:<CONTAINER_PORT>
- "7575:80" #""로 안 묶으면 시간으로 인식됨
volumes:
- "/data:/usr/share/nginx/html"
build: # ./db-Dockerfile로 image를 build한다
context: .
dockerfile: db-Dockerfile
expose: # 컨테이너 내부 포트를 고정한다.
- "80"
links:
- "db"
depends_on: # db 컨테이너에 의존성이 걸려있다.
- "db"
db:
image: mariadb
**environment:
MYSQL_ROOT_PASSWORD: "1234"**
환경변수도 지정할 수 있다.