In our last blog, we talked about Docker architecture, how to install Docker, and the main differences between Containerization vs Virtualization. Here, we are going to dive into and see how to use the Docker in action.
Let’s cap up for what we need here, which is the difference between Docker image and Docker container, Docker images are a set of files that contain the source codes, libraries, dependencies, tools, and other files needed for an application to run. Images are read-only. Whereas the container is an active (or inactive if exited) instantiation of an image. In other words, Docker Container is the run time instance of images and it lives as long as the process is running. We can run more than one container at the same time.
Now, we will see the commands of Docker
Check for the Docker version
We can run for the short version
Or run “docker version” for the complete info for installed docker
Check Running Containers
To list all the containers that are running or processing in the Docker Engine
We can notice there are no containers running, and that seems logical because we didn’t run any container yet.
List Images in Local Repo
To list all available images in our local repository is
From the result above, we can see there’re no images in our local repo, so we need to pull one or more images from the docker hub, which is Docker remote repository that is public and accessible to pull image from it.
Pull Image from remote repository
To pull an image from a remote repository we use the command
docker pull <image-name>
Here we choose to pull MySQL database as it’s known RDBMS for almost all engineers. By default, if we didn’t specify a version, it pulls the latest version. Now, when we run the command to check the images in a local repo, we can see the MySQL added.
But for the containers, we still not have any running containers, because all we did is just pull the image from remote repo to our local repo, we can test running container as,
If we need to know the name of the repository from which we pull the image, there’s a command called search.
docker search <image-name>
If we tested this command on MySQL, we will found all the images that have this word, the description for each one, and if this image is an official image or not.
Run Container from the local repository
To run a container from the local repository, we use “docker run <image-name>” command, for this particular image we will add more arguments to set the password for the root user, “docker run –name mysql -container –e MYSQL_ROOT_PASSWORD=root –d mysql:latest”
Let’s break down these parameters:
sudo docker run –> is the command used to run a container
–name mysql-container –> is the name of the container if we didn’t specify the name for the container while running it, it picks up a random name but we can rename it later.
-e MYSQL_ROOT_PASSWORD=root –> is to set a root password to log in when launching MySQL command line.
-d mysql:latest –> is the repository_name:tag, and we can get this info from running “docker images command”.
Now, let’s check for running containers, using command “docker ps”
Let’s breakdown the output,
Container ID: is the unique id for the running container, as we can run more than one container from the same image, so we need a unique identifier to deal with it.
Image: from which image this container run.
Command: is the command used to run this container.
Status: is it up and running or down.
Ports: the up and running ports in the container for the running services.
Names: the name we specified when we run this container, or random name if we didn’t specify any.
Connecting to MySQL Server from within the Container
Once the server is ready, you can run the MySQL client within the MySQL Server container you just started, and connect it to the MySQL Server. Use the “docker exec –it” command to start a MySQL client inside the Docker container you have started, like the following
-it option is to make the execution interactive were you can interact with the container through your shell
docker exec -it <container-name> <command you need to execute>
You will be asked to enter the password that you set in the previous step while we were creating a container, then the client is started and we can start working as usual.
Stop Running Container
To stop running container we will need to know the container ID, then use the following commands
docker kill <container-id>
Run command “docker ps” to check again the container is stopped
Show all containers regardless of status
To show all containers regardless of its status running or not we use the following command
docker ps -a
From the image above, we can notice the status of the container is exited from 2 minutes ago, to remove this container from the list of the containers, we use command
docker rm <docker id or docker name>
Check again to make sure it’s removed,
Show all containers regardless of status
To remove image from a local repository use the following commands
docker rmi <Image ID>
Check again to make sure it’s removed