Learn Basic Docker Containers Manipulation by Examples – Part 1
In this article, I’ll show you by examples, how to deal/manipulate Docker containers, we’ll start, stop, restart containers. We’ll attach, detach, and run commands against running containers.
Docker is probably one of the easiest environments to create a visualized instance based on a number of flavors of operating systems. Instead of installing an operating system by yourself, you can download one of the many guests templates or ‘images’ available directly from the Docker community “Docker Hub”. So, Let’s start
Before we dive into Docker containers manipulation, we need to install the Docker daemon and we need to know how to create a container.
According to your Linux OS here’s our installation articles for different Linux releases:
- For CentOS 7 Linux systems, you can install Docker on CentOS 7 using this URL:
- For Debian 8 Linux systems, you can install Docker on Debian 8 using this URL:
- For Ubuntu 16.04 and 14.04 Linux systems, you can install Docker on Ubuntu using this URL:
For creating a container from an image, check this post:
Now, you successfully installed Docker and created a container on your Linux machine, let’s continue with our article
Learn Basic Containers Manipulation
Basic Container Manipulation “Dealing with containers” is very important part of Docker world, but Why this part is too important?
Because you’ll use the commands in this part many many many times against all previous commands. You’ll use “
docker run" command once to create your development/staging/production containers “sure, you’ll use with “
docker run" all options you need your container to have”, but once the container is created you’ll need commands to deal with your containers like “start, stop. restart, list, attach, execute commands, etc,,”. You’ll use these commands periodically. There are many many commands with a lot of options to deal with containers, we’ll only list some of them in this part, other advanced commands will be in our other articles. Let’s start.
- Listing Containers
You’ll need to list your containers to know the running containers, container’s name, container’s ID, and all containers on your host. You’ll need this info “container’s name and container’s ID for use with other container’s commands.
To list the running containers on your docker host, run the following command:
# docker ps
As you see, we’ve three running containers, we created two of them from the previous part. From the above command, we knew the container’s name and ID, also the image used for creating each container, container’s up time, etc..
To list all containers on your docker host “running and non-running”, run the following command:
# docker ps -a
As you see, I’ve 12 containers, three of them are up and 9 are stopped. Actually, I do not need most of them and will remove them in a few seconds.
To list the latest created container on your docker host “either running or non-running”, run the following command:
# docker ps -l
As seen from the above image, the latest container is a stopped one “on my docker host”. Also latest created container is the first one appears when running “
docker ps -a ” command. In The next page you’ll know more about the basic container manipulations.
- Starting, Stopping, Restarting, and Deleting Containers
Once you get any container name/ID, you can start, stop, and delete it. Of course there are many other commands than those three commands, we’ll explain them later. The following examples will be against the container that has Name “centos_with_nama_newe1″ and has ID “7fc1cef7513c“
To start a stopped container either use it’s ID or it’s Name, run any of the following command:
# docker start centos_with_nama_newe1
# docker start 7fc1cef7513c
If you run ”
docker ps ” command , you’ll find that the container is up.
To stop a running container either use it’s ID or it’s Name, run any of the following command:
# docker stop centos_with_nama_newe1
# docker stop 7fc1cef7513c
If you run ”
docker ps ” command , you’ll find that the container is stopped “not found”.
To restart a running or a stopped container either use it’s ID or it’s Name, run any of the following command:
# docker restart centos_with_nama_newe1
# docker restart 7fc1cef7513c
If you run ”
docker ps ” command , you’ll find that the container is up since few seconds.
To stop a non-responding running container, and
docker stop command failed to stop it, use ”
docker kill" command instead, run the following command:
docker kill ea3964d7ffea
Here’s we killed the non-responding running container.
Finally, if you do not need the container anymore, and want to delete it, run the following command if the container is stopped:
# docker rm ea3964d7ffea
But, if the container is running, and want to delete it either stop it first and then run the above command or use
-f option with the above command. Here’s I’ll force deletion for a running container, as the following:
# docker rm -f centos_with_nama_newe1
Before moving to the next point, here’s the error you’ll get if you tried to delete a running container without using
# docker rm centos_with_name
Error response from daemon: You cannot remove a running container 8a1b73684a02f32f455881ab36180f691ff54673f816e7299cf99bcb1109c650. Stop the container before attempting removal or use -f
Now, continue reading the next page for it’s important info
If You Appreciate What We Do Here On Mimastech, You Should Consider:
- Stay Connected to: Facebook | Twitter | Google+
- Support us via PayPal Donation
- Subscribe to our email newsletters.
- Tell other sysadmins / friends about Us - Share and Like our posts and services
We are thankful for your never ending support.