http://devopscube.com/getting-started-with-docker-contianers
To understand how docker works, you can refer this article, which will give you an introduction about docker and how it works. To install and configure docker, you can refer this article, which will guide you through the steps to install and configure docker.
In this post, we will explain how to create and manage docker containers.
The following “docker run” command launches a new container from busybox image and creates an interactive session in the container.
Note: You cannot create a container with the same name. So whenever you try the examples, delete the previous container using “docker rm -f
The following command lists all the containers.
docker attach
docker rename
Let’s try renaming our containers to “demo”
Let us know your feedback in the comments section.
To understand how docker works, you can refer this article, which will give you an introduction about docker and how it works. To install and configure docker, you can refer this article, which will guide you through the steps to install and configure docker.
In this post, we will explain how to create and manage docker containers.
Getting Started With Docker Containers
Before diving into practical demonstrations, let’s understand few concepts about docker containers.- Containers are sandboxed environments which run multiple processes sharing the same host kernel.
- Containers are created from docker images.Images use a layered AUFS filesystem. An image can contain multiple layers. Layer 0 is called the base image.
- All the layers in an image are read-only except the topmost layer. The writable layer can be called as a container.
- You can commit the changes made to a container and make a new image out of it. All the layers will be preserved and you can make it a parent image to create containers.
- Each container has its own network configurations and unique id (64 hexadecimal digits).
- When you create a container, if the specified image is not present in the host, docker will download it from the docker hub (public image registry maintained by Docker Inc).
Image Taken from docs.docker.com
Commands for Creating and Managing Containers
Following are the important docker commands which are used for creating and managing containers.- run
- ps
- stop
- start
- attach
- exec
- logs
- inspect
- rename
- restart
- rm
Docker run command:
“docker run” command is used to run containers. This command accepts various argument. You can list all the supported argument by executing “docker run –help” command. Explaining all the arguments used by this command is out of scope of this article.The following “docker run” command launches a new container from busybox image and creates an interactive session in the container.
docker run -it --name testcontainer busybox
ubuntu@devopscube:~$ docker run -it --name testcontainer busybox Unable to find image 'busybox:latest' locally df7546f9f060: Pull complete ea13149945cb: Pull complete 4986bf8c1536: Pull complete 511136ea3c5a: Already exists busybox:latest: The image you are pulling has been verified. Important: image verification isa tech preview feature and should not be relied on to provide security. Status: Downloaded newer image for busybox:latest / # ls bin etc lib linuxrc mnt proc run sys usr dev home lib64 media opt root sbin tmp var / #If you see the output of the command, Docker was unable to find the image specified in the command in the host. So it pulled the image from the docker hub. “-i” flag in the command is used for starting an interactive session for the container (Keeps the STDIN open). “-t” flag attaches a pseudo tty. “–name” is used for naming the container. Here we named our container as “testcontainer”, “busybox” is the name of the image. Once the command is executed, Docker created the container and started an interactive session. The output shows the list of files in the container using “ls” command. To exit the container just type “exit” command.
Note: You cannot create a container with the same name. So whenever you try the examples, delete the previous container using “docker rm -f
docker rm -f testcontainerWhen you exit out of the container, the container stops running. To keep the container running, you need to run the container in daemon mode using “-d” flag as shown in the command below.
docker run -d --name testcontainer busyboxAlso, you can use the “–restart” flag in the docker run command, which restarts the container whenever it stops or fails. Command for creating container with restart flag is shown below.
docker run -it --restart="always" --name testcontainer busybox
docker ps command
This command lists all the containers in the host. Let’s have a look at few examples.The following command lists all the containers.
docker ps -aTo list all the running container, use the following command.
docker psTo list all the containers which got launched recently, execute the following command.
docker ps -l
docker stop command
This command stops the running container.docker stop
ubuntu@devopscube:~$ docker stop testcontainer
testcontainer
ubuntu@devopscube:~$
docker start command
This command starts a stopped container.docker start
ubuntu@devopscube:~$ docker start testcontainer
testcontainer
ubuntu@devopscube:~$
docker restart command
This command restarts a running container.docker restart
ubuntu@devopscube:~$ docker start test
test
ubuntu@devopscube:~$
docker attach command
This command is used to get an interactive session of a running container. Let’s say, you want to get a bash session or you want to modify some file and configurations in a running container, you can make use of the attach command. If you exit the container using “exit” command or ctrl + c, the container will stop running. To detach the container by leaving it running, you need to use cntrl +p and cntrl +q commands.docker attach
ubuntu@devopscube:~$ docker attach testcontianer
/ # / # touch dmofile / # ls bin etc lib64 mnt root sys var dev home linuxrc opt run tmp dmofile lib media proc sbin usr / # ubuntu@devopscube:~$ ubuntu@devopscube:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d79900ed6a5 busybox:latest "/bin/sh" About an hour ago Up 4 minutes testcontianer ubuntu@devopscube:~$
docker exec command
This is the another way of getting into containers shell. Using the exec command, you can get the shell session of a running container. One advantage of the exec command over attach command is that, when you exit the container, it will continue in the running state unlike attach command.docker exec -it
ubuntu@devopscube:~$ docker exec -it testcontianer sh / # ls bin etc lib64 mnt root sys var dev home linuxrc opt run tmp dmofile lib media proc sbin usr / # exit ubuntu@devopscube:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d79900ed6a5 busybox:latest "/bin/sh" About an hour ago Up 13 minutes testcontianer ubuntu@devopscube:~$
docker logs command
This command gives you all the logs of a container.docker logs testcontianer
docker inspect command
This command gives you all the information about a specific container. The output of this command is in JSON format.docker inspect testcontianerTo get a specific detail of a container, like the IP address of a container, you can use the inspect command with few options as shown below.
docker inspect --format='{{.NetworkSettings.IPAddress}}' testcontianer
ubuntu@devopscube:~$ docker inspect --format='{{.NetworkSettings.IPAddress}}' testcontianer
172.17.0.15
ubuntu@devopscube:~$
docker rename command
This command is used for renaming a container. It takes the following form.docker rename
Let’s try renaming our containers to “demo”
docker rename testcontianer demo
ubuntu@devopscube:~$ docker rename testcontianer demo
ubuntu@devopscube:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d79900ed6a5 busybox:latest "/bin/sh" 7 hours ago Up 6 hours demo
ubuntu@devopscube:~$
docker rm command
Using this command, you can remove the containers from host. To do this, you need to stop the running container first and them remove it using the rm command.docker rmIf you want to remove a running container without stopping it, you can use the “-f” force flag with the rm command as shown below.
docker rm -fYou can also use one-liners, which will stop and remove all the containers from the host.
docker stop $(docker ps -a -q
docker rm $(docker ps -a -q)In this post, we have covered all the important commands to manage containers. If you want the complete reference of docker commands and its usage, you can follow the official docker documentation here.
Let us know your feedback in the comments section.
No comments:
Post a Comment