Docker MySQL Container (for Ubuntu)
These instructions are for systems running Ubuntu linux
Links
- Docker Tutorial
- Good Tutorial on Docker and Docker Compose
- Docker compose vs docker-compose
- A good one about setting up a node mongodb environment
- Docker Classroom
- Docker Containers on Google Cloud
Beware that installing docker on your windows machine may mess up your Virtual Box VMS! An article that explains the problem
To check what version of Ubuntu you have:
lsb_release -a
To install docker, run these commands:
sudo apt update
sudo apt install docker.io
To see all the docker commands:
docker
To see the help for a specific command (for example ps):
docker ps --help
To view info about docker:
sudo docker info
Note: For installing the mysql image, I used this guide.
To search for the mysql images
sudo docker search mysql
To get the latest version of the official mysql image:
sudo docker pull mysql
Verify that it now one of your images:
sudo docker images
Create directories for the data and configuration files for the container (more on this soon):
mkdir -p ~/mysql/config
mkdir -p ~/mysql/data
The config folder can be used to set configuration options for the MySQL server running in the container. The data folder will store the raw data that populates the database (this allows you to back up the database).
Now to run the image:
sudo docker run -d --name mymysql \
-p 33061:3306 \
-v ~/mysql/config:/etc/mysql/conf.d \
-v ~/mysql/data:/var/lib/mysql \
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
-e MYSQL_DATABASE=blogdb \
mysql
Notes
- We are naming the container mymysql
- In the container, MySQL is running on port 3306, but we can access it from our host on port 33061.
- We have configured it to use the directories that we just created
- There is no password to log in.
- There is a database created on the server named blogdb
TODO: Im having problems connecting to the mysql server from my Node app
Verify that the container is running:
sudo docker ps -a
To view the logs for the mysql server:
sudo docker logs mymysql
To log into the running container:
sudo docker exec -it mymysql /bin/bash
Once you log in, you can use mysql:
mysql -u root -p
You'll be prompted to enter the password, which is test.
If you run this command, you should see the blogdb database in this list:
show databases;
To stop using the mysql program in the container:
exit
To stop using the shell/terminal in the container:
exit
To stop the container:
sudo docker stop mymysql
To remove it:
docker rm mymysql
#SORT OF LEFT OFF HERE
Using the command line to work with MySQL is painful!
We're going to create two docker containers, one for mysql and another for phpmyadmin. To do this we'll first install docker compose:
sudo apt install docker-compose
This will allow use 'composer' files to define our containers.
Just run this to start everything:
sudo docker compose up
You may have to change the ports in case there is a conflict
Then just navigate to localhost:8080
To stop the containers, just press Ctrl+c in the terminal window
Your changes to the database will be saved.
Docker Compose
https://www.youtube.com/watch?v=HG6yIjZapSA
TEMP NOTES
https://docs.docker.com/desktop/install/debian/
First install docker:
sudo apt install docker.io
Then install docker compose from: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-22-04
mkdir -p ~/.docker/cli-plugins/
curl -SL https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
chmod +x ~/.docker/cli-plugins/docker-compose