Docker Container

7. Docker Container

Docker Container 


       Containers are instances of Docker images that can be run using the Docker run command. The basic purpose of Docker is to run containers. Let’s discuss how to work with containers.

Run a Container-

Running of containers is managed with the Docker run command. To run a container in an interactive mode, first launch the Docker container.

    #sudo docker run –it centos /bin/bash

Then hit Crtl+p and you will return to your OS shell.

Listing of Containers-

One can list all of the containers on the machine via the docker ps command. This command is used to return the currently running containers.

#docker ps

docker ps -a-
This command is used to list all of the containers on the system

#docker ps -a

  ─a It tells the docker ps command to list all of the containers on the system.

docker history-
With this command, you can see all the commands that were run with an image via a container.

#docker history ImageID

ImageID This is the Image ID for which you want to see all the commands that were run against it.

docker top-
With this command, you can see the top processes within a container.

#docker top ContainerID

ContainerID – This is the Container ID for which you want to see the top processes

docker stop-
This command is used to stop a running container.

#docker stop ContainerID

ContainerID This is the Container ID which needs to be stopped.

docker rm-
This command is used to delete a container.

#docker rm ContainerID

ContainerID This is the Container ID which needs to be removed.

Return Value
The output will give the ID of the removed container.

docker stats-
This command is used to provide the statistics of a running container.

#docker stats ContainerID

ContainerID This is the Container ID for which the stats need to be provided.
Return Value
The output will show the CPU and Memory utilization of the Container.

docker attach-
This command is used to attach to a running container.

#docker attach ContainerID

Once you have attached to the Docker container, you can run the above command to see the process utilization in that Docker container.


docker pause
This command is used to pause the processes in a running container.

#docker pause ContainerID


docker unpause-
This command is used to unpause the processes in a running container.

#docker unpause ContainerID

The Docker pause command is used to pause an existing Docker container

docker kill-
This command is used to kill the processes in a running container.

#docker kill ContainerID

Docker – Container Lifecycle-
The following illustration explains the entire lifecycle of a Docker container.

             
                 Initially, the Docker container will be in the created state.
             Then the Docker container goes into the running state when the Docker      run command is used.
            The Docker kill command is used to kill an existing Docker container.
            The Docker stop command is used to pause an existing Docker container. 

      The Docker run command is used to put a container back from a stopped state to a running state

Docker Image

Docker Image 

        In Docker, everything is based on Images. An image is a combination of a file system and parameters. Let’s take an example of the following command in Docker.





   #docker run hello-world

1. The Docker command is specific and tells the Docker program on the Operating System that something needs to be done.
2.The run command is used to mention that we want to create an instance of an image, which is then called a container.
3. Finally, "hello-world" represents the image from which the container is made.
    Now let’s look at how we can use the CentOS image available in Docker Hub to run CentOS on our Ubuntu machine. We can do this by executing the following command on our Ubuntu machine.

     #sudo docker run centos –it /bin/bash

       Note the following points about the above sudo command:
We are using the sudo command to ensure that it runs with root access.
*Here, centos is the name of the image we want to download from Docker Hub and install on our Ubuntu machine.
*    ─it is used to mention that we want to run in interactive mode.
*   /bin/bash is used to run the bash shell once CentOS is up and running.

Displaying Docker Images:-
To see the list of Docker images on the system, you can issue the following command.

   #docker images

This command is used to display all the images currently installed on the system.

Return Value
This command is used to display all the images currently installed on the system.


From the above output, you can see that the server has three images: centos, newcentos, and jenkins. Each image has the following attributes:
·     TAG This is used to logically tag images.
·     Image ID This is used to uniquely identify the image.
·     Created The number of days since the image was created.
·     Virtual Size The size of the image.

Downloading Docker Images:- 

Images can be downloaded from Docker Hub using the Docker run command. Let’s see in detail how we can do this.

Syntax
The following syntax is used to run a command in a Docker container.

#docker run imageName

Return Value
The output will run the command in the desired container.


You will now see the CentOS Docker image downloaded. Now, if we run the Docker images command to see the list of images on the system, we should be able to see the centos image as well.


Removing Docker Images:-
The Docker images on the system can be removed via the docker rmi command. Let’s look at this command in more detail.

#docker rmi ImageID

Docker Images ID :-
This command is used to return only the Image ID’s of the images

#docker images -q

Docker Inspect-

This command is used see the details of an image or container.
#docker inspect Repository

Repository This is the name of the Image

Example-
#sudo docker inspect jenkins

When we run the above command, it will produce the following result:


For More Details..

Top Important Docker Commands

  • How Do You Use a Docker?

The biggest advantage of VMs is that they create snapshots which can be revisited instantly later.
Docker containers further enhance the lightweight process virtualization by being OS independent and using the Linux Kernel’s functionality. They are created from Docker images – like snapshots. Docker images are created using a Docker file which can be customized or used as is. The default execution driver for creating a docker container is ‘libcontainer’.  Docker Hub can be used for serching docker images and seeing the way they have been built.


CommandDescription
docker attachAttach local standard input, output, and error streams to a running container
docker buildBuild an image from a Dockerfile
docker checkpointManage checkpoints
docker commitCreate a new image from a container’s changes
docker configManage Docker configs
docker containerManage containers
docker cpCopy files/folders between a container and the local filesystem
docker createCreate a new container
docker deployDeploy a new stack or update an existing stack
docker diffInspect changes to files or directories on a container’s filesystem
docker eventsGet real time events from the server
docker execRun a command in a running container
docker exportExport a container’s filesystem as a tar archive
docker historyShow the history of an image
docker imageManage images
docker imagesList images
docker importImport the contents from a tarball to create a filesystem image
docker infoDisplay system-wide information
docker inspectReturn low-level information on Docker objects
docker killKill one or more running containers
docker loadLoad an image from a tar archive or STDIN
docker loginLog in to a Docker registry
docker logoutLog out from a Docker registry
docker logsFetch the logs of a container
docker networkManage networks
docker nodeManage Swarm nodes
docker pausePause all processes within one or more containers
docker pluginManage plugins
docker portList port mappings or a specific mapping for the container
docker psList containers
docker pullPull an image or a repository from a registry
docker pushPush an image or a repository to a registry
docker renameRename a container
docker restartRestart one or more containers
docker rmRemove one or more containers
docker rmiRemove one or more images
docker runRun a command in a new container
docker saveSave one or more images to a tar archive (streamed to STDOUT by default)
docker searchSearch the Docker Hub for images
docker secretManage Docker secrets
docker serviceManage services
docker stackManage Docker stacks
docker startStart one or more stopped containers
docker statsDisplay a live stream of container(s) resource usage statistics
docker stopStop one or more running containers
docker swarmManage Swarm
docker systemManage Docker
docker tagCreate a tag TARGET_IMAGE that refers to SOURCE_IMAGE
docker topDisplay the running processes of a container
docker unpauseUnpause all processes within one or more containers
docker updateUpdate configuration of one or more containers
docker versionShow the Docker version information
docker volumeManage volumes
docker waitBlock until one or more containers stop, then print their exit codes
Note- These all above commands are basic commands. According to your Permissions try to use. 
For More Details-