To deploy a MySQL Docker container and start working with the containerized database.
Docker is one of the most popular tool to containerize the application and consumes very less memory as compared to applications that are lauched directly in Virtual Machines or Bare Metal.
Why do we use enviroment variables in docker ?
Enviroment variable, help to pass the data to the container & configure the mysql container with given credentials.
Steps to setup MySQL with Docker
- Pull the docker image
Docker images for MySQL can be pulled from image registry.redhat.io/rhel8/mysql-80:1
> docker pull registry.redhat.io/rhel8/mysql-80:1
Once the container image is fully downloaded on the host, you can see all the downloaded images by using the below command ( # docker images ).
2. Deploy, use the env variable & Start the container with pulled image
> docker run -dit — name <container-name> -P -e MYSQL_USER=user1 -e MYSQL_PASSWORD-mypa55 -e MYSQL_DATABASE=items -e MYSQL_ROOT_PASSWORD=rootps55 registry.redhat.io/rhel8/mysql-80:1
> docker ps
3. Access the container shell
docker exec command allow you to go inside docker container. It’ll give you a bash shell inside <mysql-container-name> container. Now, to access the database and creating some databases , table or insert some data into the database, use the following cammand
> docker exec -it <container-name> /bin/bash
bash-4.4$ > mysql -uroot -p
bash-4.4$ > show databases;
4. Viewing the container logs
docker logs command shows the information loged by a running container.
> docker logs — tail 40 <container-name>
Hope you liked it 😊