Here are step-by-step instructions on how to pull Docker images from the repository and run containers using the images. You will need to have a server running Docker to perform below tasks. If you don’t have one, and you have an Ubuntu server lying around, you may refer to our previous article on how to install Docker on an Ubuntu server.
The command examples here were done on an Ubuntu virtual machine hosted in SimplerCloud, however the same commands will also work on Docker running on any other platforms (such as CentOS, Fedora, Debian etc). Since root account was being used, sudo command was omitted on most of the steps. If you are using a non-root user with sudo access on a Debian or Ubuntu environment, please append sudo in front of each command (e.g. sudo service docker status instead of service docker status).
Basic Docker Commands
You can verify if the Docker daemon is running on your server by running below commands:
service docker status
ps awx | grep docker
You can see that the Docker is already running on pid 2836. If for some reason, the Docker daemon is not running, you can start the daemon by running below command:
service docker start
To start using Docker and list down the available commands, type:
To check the Docker information, use “docker info” command.
To check the Docker version, use “docker version” command.
How to work with Docker images
To search for available Docker images, use “docker search” command. For example, to list down available images for Ubuntu, use “docker search ubuntu” command.
You might notice that in addition to the official Ubuntu Docker images (those which are marked OFFICIAL = [OK]), there are also some additional images uploaded by members of the community. Those images are normally started with the username of the member, e.g. esycat/ubuntu or zoni/ubuntu. Those are custom images made by the users mainly for their own purposes and you should refrain from pulling third-party images unless you know what you are doing. For security reasons, it’s advisable for you to stick with the official Docker images provided.
To pull an image, use “docker pull” command. For example, if you want to pull the official Ubuntu image:
docker pull ubuntu
To list down all Docker images currently on your server, use “docker images” command.
You can see that the latest Ubuntu images you have just pulled appears on the list, in addition to the default hello-world Docker image, which is already installed by default.
How to work with Docker containers?
To check the list of Docker containers which are running on your server, use “docker ps” command.
You can see that there are no containers running yet. To list all the available containers on your server, even though it’s not running, use “-l” option on “docker ps” command:
docker ps -l
Let’s try to run the hello-world container. Use “docker run hello-world” command to run the container.
docker run hello-world
You can also use the “docker run” command to create a new Docker container. For example, below is the command on how to create a Docker container using the Ubuntu image which we have just pulled earlier:
docker run -i -t ubuntu /bin/bash
The command will start the container, and you will then be redirected to the bash shell of your newly created Ubuntu container. If you notice, the “[email protected]:/#” prompt is actually the bash shell prompt within the Ubuntu container that we have just created.
You can also run normal Linux commands such as “df” and “uname -a” inside the container.
To exit the container, just type “exit” on the terminal shell.
You will then be re-directed back to the parent operating system. Note that when you exit, the container will also be stopped. You can verify by performing another “docker ps -l“ command.
That’s it, you have managed to run a Docker container on your server.
Next article: how to create your own images (update and commit the images), and push your images into the Docker Hub repository.
Disclaimer : This is another great guest post article by Indra Pramana from SimplerCloud.com, a cloud servers provider from Singapore with solutions built from the ground up to provide truly real-time, scalable and easily managed cloud infrastructure for start-ups, developers and business throughout Asia.
This post How To Pull Docker Images and Run Docker Containers is part of ServerMom.