Dec 22, 2022
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 servers (VPS) 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 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 Dockers 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 <image name or image id>
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
-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 <image_name>
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:
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 <container name>
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 <container name>
If you wish to terminate all of the containers at the same time, use the following:
docker kill $(docker ps -q)
Docker is a handy tool for every kind of developer. Its ability to seamlessly test, deploy and develop applications can speed up workflow exponentially.
In this tutorial, we’ve gone through the process of creating a Docker container on a virtual private server running Ubuntu 20.04. We have also covered how to manage already generated containers.
We hope you found this tutorial useful. If you have any further questions or suggestions, leave them in the comments below.
How to Create a Docker Container FAQ
Here are a few frequently asked questions about creating Docker containers.
What Is a Docker Image vs. Container?
Docker images grant a server a set of instructions and requirements on how a Docker container should be generated. On the other hand, a Docker container is a software package that includes everything needed to run an application. In other words, a Docker container results from requirements given by the Docker image.
Can a Docker Image Have Multiple Containers?
A single container is built from one image. That means you won’t be able to create a couple of containers with a single image. However, it is possible to instruct Docker to run multiple images to be built in sequence. It is worth mentioning that the result will still result in a single image.
Can Docker Images Be Edited?
Yes, Docker does provide the functionality to edit or even create your own images via the Dockerfile. To edit an image, you need to delete it first and then edit the Dockerfile, implementing your preferred changes. Lastly, you will only need to recreate the container using the new file.