Create Docker Container And The Basic Docker Commands
Now that you have installed Docker on your First2Host KVM NVMe VPS it’s time to learn the basics and create a docker container. This guide assumes you have followed our simple Docker Install post so you should now be ready to go.
Things To Remember Before You Create Docker Containers
1- You must have “sudo” in front of Docker commands for them to run.
2- The $ indicates you are at the command prompt of your server
3- The # icon indicates you are at the command line of a container
Basic Docker Commands
Docker status and configuration
sudo docker info
List Docker containers
sudo docker ps
List The latest Docker container
sudo docker ps -l
Show all docker containers
sudo docker ps -a
Show Docker Images and tags
sudo docker images
Connect & login to work interactively on a container
sudo docker run -it <container> <app>
Show status and log for Docker
systemctl status docker
Enable Docker On Boot
sudo systemctl enable docker
sudo systemctl start docker
sudo service docker stop
sudo service docker start
sudo service docker restart
Add Linux User To Docker
sudo usermod -aG docker <LinuxUser>
The docker pull Command
The docker pull command is used to pull OS images from Docker to your server. These will then be used to create containers which you can then work inside. The syntax is docker pull <argument> you can use the docker search <argument> to search for specific OS images. For example docker search centos
As you can see from the image above, there are a number of CentOS images. The official image is just “centos” which would download the CentOS 7 image.
Download the latest CentOS image with the below command
docker pull centos
Create Docker Container With The docker run command
Now you have your desired OS version it’s time to create your first container. We use the docker run command to do this. You could just issue docker run centos but this would not provide you with any output to confirm the image does indeed work so, it’s best to tell Docker to talk to you once the command has been completed. To do this we echo a statement. Issue the below command to create a container based on the CentOS image you pulled to your server
docker run centos echo "Hi! I'm Running On A First2Host VPS"
You should get a response like the below
[[email protected] ~] docker run centos echo "Hi! I'm Running On A First2Host VPS" Hi! I'm Running On A First2Host VPS
This confirms the container was able to start, docker was able to issue our echo command and the container then shut down. Great, you have created your first container on Docker. Did you notice how fast this was? Now, as this container is not running it will not appear in the docker ps list. Instead, we use the docker ps -a command
List All Docker Containers
To list all of your Docker containers, issue the below command
sudo docker ps -a
As you can see from the image we just have a single container which was created a few moments ago. You can see the last command the container run under the “Command Collum” The Status indicates the server is offline and the Image collum indicates the OS version this container is using. The Container ID is displayed in the Container ID collum.
Run a Docker Container And Work Inside It
Next, we are going to start our CentOS container and do some work inside the container whilst it’s online. We will use the -it flag to start the container and enter it so we can work inside. From the sudo docker ps -a screen above get your image name and start the container with the below command
docker run -it centos sh
You are now inside your CentOS container. Let’s see if everything is up to date with the yum update -y command. Issue it in your console and your container should update.
To exit this container just type exit and you will be sent back to the hostnodes command line prompt. When you do this the container will be shutdown and you will lose the updates you just did to the container.
Use the Commit Command To Make Changes Persistent
The commit command can be used to perform tasks and make them permanent on the OS image. Issue the below command to create another container and run an update
sudo docker run centos yum update -y
Then issue the ps -l command to get the container ID
sudo docker ps -l
Issue the below commands to set this image to update on boot.
sudo docker commit <container_id> centos/update
Then run the container and tell it to update. You will notice it’s already updated.
sudo docker run centos/update yum update -y
You can start and enter your new container again with the docker run command.
sudo docker run -it centos/update
Did you notice when you started the container and logged in the server first looked for updates? You have now created your first container and a basic OS template which updates when you first start the container. In the next article, we will teach you how to package containers for use in creating new containers based on the template you created here.
How Was This Article? – Create Docker Container And The Basic Docker Commands