Jak zainstalować MySQL w kontenerze Docker?

Jak zainstalować MySQL w kontenerze Docker?

Kwi 02, 2019 ubuntu code snippets docker knowledge base

Instalacja Docker i uruchomienie kontenera MySQL

Zainstaluj Docker na Ubuntu wg tego poradnika: Jak zainstalować Docker na Ubuntu? (otwiera się w nowej karcie).

Uruchamiamy kontener MySQL w tle. Hasło dla roota to "test". Kontener usunie się sam po wyjściu.

docker run --rm -it -e MYSQL_ROOT_PASSWORD=test -d mysql
  • -d – kontener działa w tle, pokazuje ID.
  • --rm – kasuje kontener po zakończeniu.

Sprawdź działające kontenery

docker ps
CONTAINER ID      IMAGE    COMMAND                  CREATED           STATUS        PORTS                   NAMES
8ee2e6c8174d      mysql    "docker-entrypoint.s…"   9 seconds ago     Up 7 seconds  3306/tcp, 33060/tcp     condescending_snyder

Wejdź do shella MySQL

docker exec -it 8ee2e6c8174d mysql -u root -ptest
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.15 MySQL Community Server - GPL

mysql> 

Szczegóły kontenera

Pokazuje sieć, wolumeny i inne ustawienia.

docker inspect 8ee2e6c8174d
[
    {
        "Id": "8ee2e6c8174d4138026f1d54addf9aadc7fbe22727c7f1fb9ca996ce5f7601b2",
        "Created": "2019-03-28T20:53:46.84185469Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
        ...
        "Networks": {
            "bridge": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": null,
                "NetworkID": "4c9f618087d8f2e6924e7683cfb98055d8cc9a4ec8d0427bd188b5b7c34c0b3f",
                "EndpointID": "cd5d96ad898831d3cc2d0941633533fdf8dcc6cdb0564164c49cad1a2399e8bf",
                "Gateway": "172.17.0.1",
                "IPAddress": "172.17.0.2",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:11:00:02",
                "DriverOpts": null
            }
        }
    }
]

Połączenie z MySQL po IP kontenera

mysql -u root -ptest -h 172.17.0.2

Lista wszystkich kontenerów (również zatrzymanych)

docker ps -a
CONTAINER ID     IMAGE     COMMAND                  CREATED          STATUS                    PORTS                  NAMES
8ee2e6c8174d      mysql    "docker-entrypoint.s…"   2 minutes ago    Up 2 minutes              3306/tcp, 33060/tcp   condescending_snyder
913f33f5a848      mysql    "docker-entrypoint.s…"   12 minutes ago   Exited (0) 11 minutes ago                  jovial_dubinsky
b36cf8b3b664      mysql    "docker-entrypoint.s…"   22 minutes ago   Exited (1) 22 minutes ago                  cocky_mcnulty

Pokaż pobrane obrazy

docker images
REPOSITORY  TAG     IMAGE ID      CREATED       SIZE
mysql       latest  7bb2586065cd  45 hours ago  477MB

Read in other languages:

EN ES SV RO DE FR HU NL EL BG RU CS PT NB FI IT DA ZH-HANS UZ TR