- Dockerize MySQL server
A. launch a docker in local
B. Hosting the MySQL server in Docker container in IMAGE
C. Listening to the host port of MySQL server in local by whatever like Springboot
Rest API or Django Rest API
Command:
1 | docker run -d -p 3306:3306 --name=mysql-server --env="MYSQL_ROOT_PASSWORD=123456" mysql |
Notes: Check what other app is listening to the port on 3306
Command:
1 | -p 12345:3306 |
– If you cannot connect to MySQL from another docker and got this error:
1 | Then you can add the parameter below when running the container. |
– you can access the MySQL through your host
1 | # Through Host |
Create user and database for connection
1 | CREATE DATABASE databaseName; |
If you are using Django, here is what you need to put in the settings.py
1 | DATABASES = { |
That is that.
//Resources for mySQL and docker commands to practice.
//With Docker-composer set up docker-compose1.yml
1 | version: "3" |
or docker-compose2.yml
‘’’
version: ‘2’
services:
mysql:
image: mariadb:10.1.19
ports:
- 8083:3306
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: wp
1 |
|
Access the mysql db with command:
1 | mysql -p findeasily --protocol=tcp -u root -p |
for the first docker-compose1.yml.
And for second docker-compose2.yml, we can access the mysql database with
Command like:
1 | docker-compose up |
– Running a Postgres using docker
Command lines:
laungch a docker container
1 | # Create the docker container over a specific image |
if we already have the docker container running, then we can just launch an images and if there is not
local image it will pull from remote community server.
Let’s now run a specific process in the container. This is going to be postgres. Here are the operations are broken down:
We’re naming the container local-postgres10.3.
Set the password to “password” using the environment tag -e.
Set the port to 5432 so we can access it from Postico later using -p.
The –volumes-from tells the container to mount the /var/lib/postgresql/data volume from the postgres10.3-database container that we created in the previous step.
Lastly, use postgres:10.3 to launch the container.
1 | docker run --name local-postgres10.3 -p 5432:5432 -e POSTGRES_PASSWORD=password -d --volumes-from postgres10.3-database postgres:10.3 |
this is running the images base on postgres in local, so since we do not have local-postgres10.3 images, it will just pull the image from remote and then we can run the image by the image id.
1 | docker run dsf3edfdsfw |
like that, so right now we running the postgres db in the docker, we can check by
1 | now when we access the db by psql command, we can take like: |
This is meaning that:
docker exec command psql in database name as cocky_varahamihira.
Notes: we don’t need to do like
1 | docker run --name local-postgres10.3 -p 5432:5432 -e POSTGRES_PASSWORD=password -d --volumes-from postgres10.3-database postgres:10.3 |
Cauz it will run psql in local compouter instead of running in docker. so you will get in trouble of
install psql in your local computer, that will be not good I think.