Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Create a Docker Container: Master Docker Image Compartments
#1


How to Create a Docker Container: Master Docker Image Compartments



Running applications on different operating systems might seem challenging at first. However, Docker makes this process easy.


With its convenient containerization technology, users can deploy numerous applications or services on systems such as virtual private server hosting or dedicated machines without further issues.


Additionally, unlike virtual machines, Docker is much more lightweight, easier to deploy, and performs better.


In this tutorial, we will show you how to create a Docker container alongside all the necessary configurations.


What Is a Docker Container

Docker is among the most popular open-source container-based tools. It is used to run instances of Docker images since these create a new Docker container.


In other words, images can be considered as a template that can be used to create containers. They contain information about what’s required to develop and run a container. Another excellent image feature is that multiple images can be stored locally or remotely.


What Is Docker Hub

To make image sharing easier, Docker came up with a streamlined image-sharing platform – Docker Hub. Here users can easily share, upload and manage their images.


No matter the operating system you use, whether it’s Ubuntu on a virtual instance or Windows, users will still be able to find popular images like MySQL or WordPress on Docker Hub.


Users can always pick the paid version of Docker Hub if the free version proves to be lacking. It offers more robust features, vulnerability scans, and concurrent builds.


What Is a Dockerfile

Docker images instruct the server on the exact requirements for creating a Docker container. On the other hand, a Docker file is an actual text file containing all the Docker commands needed to assemble a container image. If a user wishes to create an image, they would need to create a Dockerfile with all the required commands for the server.


How to Create a Docker Container

In this tutorial, we will use the Ubuntu 20.04 virtual private server and assume you already have Docker installed. Therefore, start by accessing your VPS via SSH.

To list all Docker images on your system, you can use the following command:


sudo docker images

If you want to display additional information, enter the following command in the command line:


sudo docker images --help

We don’t have any Docker images on our system, so let’s pull a new one first.


To do that, first, go to the Docker Hub. Here you will find thousands of Docker base images.


In this example, we will pull a MySQL image. Note that you can browse each image’s page to see more details about the image.



You can pull the new image to the current directory with the command:


docker pull 

Pro Tip: It’s possible to replace “image name or image id” with hundreds of images found on Docker Hub like CentOS, redis, mariaDB, Python, etc.


Using the -q option will list only numeric IDs of images available on your system:


sudo docker images -q

The -f is the filter flag. If you want to list all images that are not dangling (tagged or referenced by a new container), use the following command in the command line:


sudo docker images -f 'dangling=false'

Now that you know how to pull and locate an image to start a Docker container, it’s time to run it. By running an image, you will create a container out of that image.


To start a Docker container, use the docker run command:


docker run 

We will run the MySQL image. As such, the command will be:


docker run mysql

Our container is created but not started. To start it, use this command via the command prompt:


docker run --name MyContainer -it mysql bash

Here –name MyContainer is how we wish to call the running process, while -it mysql bash names which container we are running.


Now, open another terminal window. SSH into the server, and run the Docker ps command:


sudo docker ps -a

As you can see, the container named MyContainer is now running. To stop the container, issue the following command:


sudo docker stop MyContainer

If you wish to see the top process of a container, we can run this command:


docker top MyContainer

MyContainer represents your container name.



To see additional stats, such as container ID, CPU utilization, or memory usage, use this command:


docker stats


Lastly, if you need to terminate a Docker container, use the following:


sudo docker kill MyContainer

That’s all there is to it – our Docker container is now ready to use.


How to Start and Stop a Docker Image Container

If you already have existing containers created, you can easily start them by using the following command:


docker start 

Confirm whether it’s running by opening a new SSH session and running the following command:


docker top MyContainer

With the container running, you can stop it with the following command:


docker stop 

If you wish to terminate all of the containers at the same time, use the following:


docker kill $(docker ps -q)





Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)