Step 1: Open a terminal and execute the below command.
docker run -d -e MYSQL_ROOT_PASSWORD=tiger mysql:latest
-d: Run the container in background, so we can't see all the log messages of docker in terminal.
-e: Used to set Environment Variables.
$docker run -d -e MYSQL_ROOT_PASSWORD=tiger mysql:latest
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
54fec2fa59d0: Pull complete
bcc6c6145912: Pull complete
951c3d959c9d: Pull complete
05de4d0e206e: Pull complete
319f0394ef42: Pull complete
d9185034607b: Pull complete
013a9c64dadc: Pull complete
42f3f7d10903: Pull complete
c4a3851d9207: Pull complete
82a1cc65c182: Pull complete
a0a6b01efa55: Pull complete
bca5ce71f9ea: Pull complete
Digest: sha256:61a2a33f4b8b4bc93b7b6b9e65e64044aaec594809f818aeffbff69a893d1944
Status: Downloaded newer image for mysql:latest
122d27c2a3467700df7fbf20f0b919da2cade9eadb30c745b35b42fbec1dff6f
Step 2: Run MySQL command in the running container using 'docker exec'
Syntax
docker exec -it {container_id/container_name} {command_to_execute}
Get all the running containers
$docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
122d27c2a346 mysql:latest "docker-entrypoint.s…" 54 seconds ago Up 53 seconds 3306/tcp, 33060/tcp elastic_tesla
As you see ‘elastic_tesla’ is the container name. Now execute below command.
docker exec -it elastic_tesla mysql -h localhost -u root -p
Enter the password as tiger.
$docker exec -it elastic_tesla mysql -h localhost -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 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>
Note
a. You can specify container name using –name option.
docker run -d --name krishna_sql -e MYSQL_ROOT_PASSWORD=tiger mysql:latest
No comments:
Post a Comment