"Quick Guide: Pulling MySQL Docker Image from Docker Hub and Running a Container"
Create an EC2 Instance and Connect
Go to the AWS Management Console.
Launch a new EC2 instance, choosing your preferred Amazon Machine Image (AMI) and instance type.
Configure the instance settings (security groups, key pair, etc.).
Launch the instance and connect to it using SSH.
Install Docker:
- Once connected to the EC2 instance, run the following commands to install Docker:
sudo apt update
sudo apt install docker.io
Create a Project Directory:
- Navigate to your home directory or another suitable location and create a directory for your Docker project:
mkdir docker-project
cd docker-project
Grant User Permission to Docker:
- Add your user to the 'docker' group to allow running Docker commands without using 'sudo':
sudo usermod -aG docker $USER
Reboot to Reflect Docker Permissions:
- To apply the group changes, reboot the EC2 instance:
sudo reboot
Verify Docker Installation:
- After the instance restarts and you've reconnected to it, verify that Docker is installed and accessible by running:
docker ps
This should show the currently running containers.
Using Docker Images from Docker Hub:
Docker Hub is a cloud-based registry service provided by Docker for sharing and storing Docker images. It's a central repository where you can find, store, and manage Docker images. Docker images are used to create containers, which are lightweight, isolated environments that package an application and its dependencies.
- If you have a Docker image available on Docker Hub, you can pull and run it. Replace
image_name
with the name of the image you want to use, i'm using mysql image which is latest. latest is tag here:
- If you have a Docker image available on Docker Hub, you can pull and run it. Replace
docker pull mysql:latest
docker run -d mysql:latest
The -d
flag runs the container in detached mode.
- To see all containers, including stopped ones, use:
docker ps -a
To list the Docker images that are currently available on your system, you can use the following command:
docker images
Running this command will provide you with a list of Docker images along with their repository, tag, image ID, creation date, and size.
Note:
Creating an Image*:*
- When you want to craft a Docker image based on specific instructions, you utilize a Dockerfile. This file contains the steps to assemble the image, such as installing software or configuring settings. By running the
docker build
command, you construct the image layer by layer on your local system.
- When you want to craft a Docker image based on specific instructions, you utilize a Dockerfile. This file contains the steps to assemble the image, such as installing software or configuring settings. By running the
Pulling an Image*:*
- If you'd like to use an existing Docker image shared on Docker Hub, you can employ the
docker pull
command. This command fetches the desired image from Docker Hub's repository and makes it accessible on your local machine, ready for use.
- If you'd like to use an existing Docker image shared on Docker Hub, you can employ the
Running docker MySQL Image (Basic):
- The first command you mentioned starts a MySQL container using the
mysql:latest
image without any additional configurations. However, it's important to note that many images, including MySQL, have default configuration options that might not suit your needs without additional parameters.
- The first command you mentioned starts a MySQL container using the
docker run mysql:latest
Running MySQL Image with Environment Variables (Recommended):
- The second command is a better practice as it sets up the MySQL container with specific environment variables, like setting the root password for the MySQL instance. The
-d
flag runs the container in detached mode (in the background).
- The second command is a better practice as it sets up the MySQL container with specific environment variables, like setting the root password for the MySQL instance. The
docker run -d -e MYSQL_ROOT_PASSWORD=deep@123 mysql:latest
In the second command, -e MYSQL_ROOT_PASSWORD=deep@123
is used to pass the environment variable MYSQL_ROOT_PASSWORD
with the value deep@123
to the container. This sets the root password for the MySQL database to deep@123
.
The
docker exec
command with the-it
flags is indeed used to gain interactive shell access inside a running Docker container. The command structure you provided is correct:docker exec -it container_id sh
Then you will be in the shell and run following commandsto enter in mysql:
sh-4.4# mysql -u root -p
Enter password:deep@123
Run basis mysql commands:
I trust that this information proves beneficial to you. Thank you!