1. Launch docker desktop server on MacOS via Launchpad icon.
Docker desktop application can be downloaded from the docker
web site.
After launch the docker icon should appear on the right top icon tray of the menu. Docker can be used from command line now, lets check the docker is running and the version.
docker --version
Docker version 23.0.5, build bc4487a
1.1 Get docker mysql image.
docker run mysql:latest
2. Launch MySQL docker container.
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pwd -d mysql
Arguments:
-p
publish port from container and map it to host OS port, this is to connect to MySQL from the outside environment later.
-e
env variables, used only MYSQL_ROOT_PASSWORD in this case and set a new 'pwd' for the root user. Optionally, we can create more users with corresponding passwords on this step, providing more env parameters like this:
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pwd MYSQL_USER=newuser MYSQL_PASSWORD newpwd -d mysql
-d
daemon mode
-'mysql'
would be a new container name
2.1 Check that container is running and get its id from table CONTAINER ID.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7fc7ee05859d mysql "docker-entrypoint.s…" 26 minutes ago Up 26 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp angry_fermi
3. Connect to mysql container by CONTAINER ID (get it from previous step) using docker shell.
docker exec -it 7fc7ee05859d bash
Arguments:
-it
interactive mode (shell)
-'7fc7ee05859d'
is a container id
Now, inside the container, we can launch mysql console providing the root user name and actual password that was set on step 2.
mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
3.1 Lets check that MySQL is really available to the outside of the
container, as we mapped internal port of the container to OS port 3306 on step
2.
One of the options to do this is to install MySQL workbench application from
Oracle on the host computer. When launched it allows to create a new MySQL
Connection. Supplying 127.0.0.1:3306 for this new connection (that is
localhost:port, that we mapped to on step 2), we should
be able to connect to the DB inside the docker container.