본문 바로가기

따배도 스터디

6-2 Docker 컨테이너 사용하기 -실습편

728x90

 

1. MySQL Docker 이미지 다운로드

다음 명령어로 MySQL Docker 이미지를 다운로드한다. 태그에 버전을 지정하지 않으면 최신 버전을 다운로드한다.

$ docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
bb79b6b2107f: Pull complete
49e22f6fb9f7: Pull complete
842b1255668c: Pull complete
9f48d1f43000: Pull complete
c693f0615bce: Pull complete
8a621b9dbed2: Pull complete
0807d32aef13: Pull complete
a56aca0feb17: Pull complete
de9d45fd0f07: Pull complete
1d68a49161cc: Pull complete
d16d318b774e: Pull complete
49e112c55976: Pull complete
Digest: sha256:8c17271df53ee3b843d6e16d46cff13f22c9c04d6982eb15a9a47bd5c9ac7e2d
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

 

ubuntu@ip-172-31-45-232:~$ docker images
REPOSITORY                TAG       IMAGE ID       CREATED       SIZE
mysql                     latest    8189e588b0e8   3 days ago    564MB
kimminwoo1234/webserver   v1        09b848442d5b   10 days ago   204MB
webserver                 v1        09b848442d5b   10 days ago   204MB
hellojs                   latest    50dd87d32f95   10 days ago   918MB
httpd                     latest    dc1a95e13784   2 weeks ago   145MB
localhost:5000/httpd      latest    dc1a95e13784   2 weeks ago   145MB
registry                  2         8db46f9d7550   3 weeks ago   24.2MB
nginx                     latest    080ed0ed8312   3 weeks ago   142MB

2. MySQL Docker 컨테이너 생성 및 실행

$ docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=<password> -d -p 3306:3306 mysql:latest

3. Docker 컨테이너 리스트 출력

$ docker ps -a

4. MySQL Docker 컨테이너 시작/중지/재시작

# MySQL Docker 컨테이너 중지
$ docker stop mysql-container

# MySQL Docker 컨테이너 시작
$ docker start mysql-container

# MySQL Docker 컨테이너 재시작
$ docker restart mysql-container

5. MySQL Docker 컨테이너 접속

$ docker exec -it mysql-container bash
root@dc557b92f573:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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.01 sec)

 

6. docker logs mysql-container 로그찍기

docker logs -f : 현재 로그상태를 실시간으로 모니터링

ubuntu@ip-172-31-45-232:~$ docker logs mysql-container
2023-04-21 01:38:18+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
2023-04-21 01:38:18+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2023-04-21 01:38:18+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
2023-04-21 01:38:19+00:00 [Note] [Entrypoint]: Initializing database files
2023-04-21T01:38:19.055768Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-04-21T01:38:19.057128Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.33) initializing of server in progress as process 78
2023-04-21T01:38:19.091371Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-04-21T01:38:20.020165Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-04-21T01:38:22.374401Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2023-04-21 01:38:26+00:00 [Note] [Entrypoint]: Database files initialized
2023-04-21 01:38:26+00:00 [Note] [Entrypoint]: Starting temporary server
2023-04-21T01:38:26.655629Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-04-21T01:38:26.658055Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.33) starting as process 118
2023-04-21T01:38:26.706988Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-04-21T01:38:27.076086Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-04-21T01:38:27.563829Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-04-21T01:38:27.564073Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-04-21T01:38:27.567701Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-04-21T01:38:27.603940Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
2023-04-21T01:38:27.604312Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.33'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
2023-04-21 01:38:27+00:00 [Note] [Entrypoint]: Temporary server started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.

2023-04-21 01:38:30+00:00 [Note] [Entrypoint]: Stopping temporary server
2023-04-21T01:38:30.858784Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.33).
2023-04-21T01:38:32.574874Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.33)  MySQL Community Server - GPL.
2023-04-21 01:38:32+00:00 [Note] [Entrypoint]: Temporary server stopped

2023-04-21 01:38:32+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.

2023-04-21T01:38:33.308786Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-04-21T01:38:33.310534Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.33) starting as process 1
2023-04-21T01:38:33.325938Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-04-21T01:38:33.723575Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-04-21T01:38:34.007645Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-04-21T01:38:34.007867Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-04-21T01:38:34.011486Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-04-21T01:38:34.047189Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2023-04-21T01:38:34.047600Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.33'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

 

7. 컨테이너 내부 프로세스 확인 docker top <cotainername>

ubuntu@ip-172-31-45-232:~$ docker top mysql-container
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
lxd                 1592                1570                0                   01:38               ?                   00:00:03            mysqld

8. 컨테이너 삭제 docker rm -f <container_id>

ubuntu@ip-172-31-45-232:~$ docker rm -f a0fa3288f1e3
a0fa3288f1e3
ubuntu@ip-172-31-45-232:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

 

728x90