Showing posts with label docker. Show all posts
Showing posts with label docker. Show all posts

Wednesday, July 27, 2022

Install Mysql in Docker on Ubuntu Linux server

In this tutorial, we will learn how to install Mysql on Docker.

Please follow the steps to install docker using this tutorial Install Docker on Ubuntu

Install Mysql using Docker:

For installing the desired version of MySQL first, visit docker mysql image section where we can find the available MySQL version. In this tutorial, we are using MySQL version 5.7.

Use the following command on the server to install the MySQL image

docker pull mysql:5.7 

Use your own version instead of 5.7

This will pull the image and install Mysql inside docker. We can verify the image using the following command

docker images

The output of the above command will be similar to this

mysql          5.7                   314749b3a5c   17 hours ago    431MB

Create Mysql Container:

Let’s create the MySQL container. To create a MySQL container, use the following command.

docker run --name=cn-mysql -d -p 3316:3306 --env="MYSQL_ROOT_PASSWORD=root1249" mysql:5.7

Note: use your own MySQL root password instead of root1249 and the port instead of 3316 and container name instead of cn-mysql

This will create the container. We can see the container using the following command

docker container ls -a 
docker ps -a

Enter into the Mysql server:

If we want to do the regular MySQL operation, then we need to enter the MySQL server inside docker as below

docker exec -it elint-mysql bash

Now login into MySQL using the command

mysql -u root -p

Use your own MySQL password which is used while creating a container i.e root1249

Now we can do operations like creating databases and users.

Creating Mysql User:

In order to create a MySQL user and give permission visit the tutorial, Create Mysql User and Grant Privileges

Let's create a user

create user 'username'@'%' identified by 'password';

Note: change your own username and password. If you need to connect your created user from any client, you should create the user with “%” which signifies, that you can connect this user from any host.

Now, let's grant privileges

grant all privileges on *.* to username;

Use the previously created username

flush privileges;

Install Docker on Ubuntu 20.04

In this tutorial, we will learn how to install docker on Ubuntu Linux.

Install docker using the repository:

For this, first set up the repository. Use the following command.

sudo apt-get update
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \

Now, add docker's GPG key

sudo mkdir -p /etc/apt/keyrings
curl -fsSL | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Now give the sufficient permission

sudo chmod a+r /etc/apt/keyrings/docker.gpg

In order to set up the repository, use the following command

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Now for installing Docker use the following command, which will install the latest docker.

sudo apt-get update & sudo apt-get install docker-ce docker-ce-cli docker-compose-plugin

Install Docker using .deb package:

If we might have a problem with installing the docker with the above or wants to install a specific version of the docker then using .deb package is a good solution.

Download the desired version of .deb from here

In order to test the ubuntu version use the following command

lsb_release -a

we can get the output as below

No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.3 LTS
Release:	20.04
Codename:	focal

Here, the codename is focal so We chose the same as above. Then browse to pool/stable/, choose amd64, armhf, arm64, or s390x, and download the .deb file

In order to test the CPU available use the following command


Go to the download folder and execute the deb package using the following command

sudo dpkg -i docker-ce-cli_18.09.0_3-0_ubuntu-bionic_amd64.deb

Note: make sure to use your own version of the .deb package. Now start the docker

Now, we successfully install docker.

Check the Docker Version:

docker --version

Uninstall Docker:

Uninstall docker, CLI, Containerd, and Docker Compose packages using the following command

sudo apt-get purge docker-ce docker-ce-cli docker-compose-plugin

Now, delete all images, containers, and volumes

sudo rm -rf /var/lib/docker
 sudo rm -rf /var/lib/containerd