Mar 17, 2022
Learning Docker: How to Create a Docker Container
Just like Linux was an accidental revolution by Linus Torvalds, Docker was by Solomon Hykes. Solomon was trying to figure out a solution for programmers to provide a code that runs the same both on their development as well as the production environment and this resulted in the creation of Docker Containers. In this tutorial, we’ll show you how you can start a Docker container on your VPS, along with some extra tips and commands.
Today Docker is the most popular software container platform.
At a very basic level it solves the issue of an application running on one platform but not on others. Although present in the entire software development lifecycle, Docker Containers main use is in deployment.
What are Docker Containers
Docker Containers are running instances of Docker images. Running an Image creates a Docker container. Images provide a template that can be used for the creation of containers. They contain information about what’s required to create containers. Images can either be stored locally or remotely.
How to Create a Docker Container
To list all Dockers images on your system you can use the 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 an image first. To do that, first go to Docker hub. Here you can find hundreds of Docker Images. We’ll pull an Ubuntu image. You can browse each image’s page to see more details about the Image:
We can pull the image with the command:
docker pull <image name>
You can replace <image name> with hundreds of images found on Docker Hub like CentOS, MySQL, 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 we wanted to list all images that are not dangling – tagged or referenced by a container – we would use the command:
sudo docker images -f “dangling=false”
Now that we know how to pull and locate an image to start a Docker container, we can get to work.
Next we see how to run an Image, by running an Image we actually create a container out of that image. Lets run our ubuntu image. To start a Docker container use the command:
docker run <image_name>
We’ll run the Ubuntu image. So the command will be:
docker run ubuntu
The container is created, but not started
To start the container we use a command like this:
docker run --name MyContainer -it ubuntu bash
Here –name MyContainer is simply how we want to name the running process, while -it ubuntu bash, names which container we’re running.
Nowe we can open another terminal window, SSH into the server and run the command:
sudo docker ps -a
Open another terminal and issue following command.
We can now see that the container named MyContainer is now running.
To stop the container issue following command:
sudo docker stop MyContainer
If we wish to see the top process of a container we can run the command:
docker top < container ID or Name>
It would look like this:
sudo docker top MyContainer
To see stats of a container like its CPU utilization, memory usage, etc.:
Lastly, if we wish to kill a Docker container:
sudo docker kill MyContainer
That’s all there is to it! You’re ready to start a Docker container and put it to use!
Docker is an incredibly useful tool for any developer. The ability to seamlessly test, deploy and develop applications is a utility that can speed up workflow exponentially. In this tutorial, we show how to create a Docker container, along with some other commands that will definitely come in handy.
To learn more we suggest checking out our other tutorials, or Docker’s official documentation!