Working with Docker – II

In the previous chapter we learnt how to install Docker Engine and how to make it available for other system users. In this chapter we will learn how to start a simple docker container and we will discuss about few basic docker commands. The commands will be useful in day to day life while working with  Docker.

Lets start with Lunching our First container:

In the previous chapter we already discussed how to test the Docker is installed properly or not. however, lets test it again by using the below command:

The above command will pull the hello-world latest image from Docker Registries. Let lunch another container using centos image. The same thing will happen here again. Docker will search for the image locally and if it wont able to get that image it will go and download the image from Docker Registries.

Lets understand the complete command: docker run  will be responsible to lunch the container. centos is the argument which contains the value of the image. echo "Hello Linuxfunda"  is the command we supplied to run inside the container. So, after downloading the image docker will make the image to a running container and execute the supplied command inside it. That’s why we were able to see the echo message in the last line of the output.

Now let’s again issue the same command to see what is happening.

See only the echo message is coming as the output. Because, docker was able to find the image centos locally as it downloaded the image in our previous attempt. But why it is not letting us to login inside the container? Silly question but worthy. Below command will tell Docker to give us a shell inside the container.

As we know docker run  will lunch the container. -i  flag will ask Docker for interactive session. -t  will ask for a tty . /bin/bash  will provide us a bash shell. But remember one thing here, the containers are not persistent. After exiting the shell the continer will stop. Then again the question is how to make a container up and running? Using -d  flag we can run a container in the background. You will only get the containers ID in the output. Let’s use it:

Let’s login to the container. Using container’s ID you can login to the container.

To see how many containers are running or to get few basic info about the running container use the below command.

How to stop a running container. Using the command docker stop <container ID>  you can stop a running container:

You can verify the container status using the command docker ps -a . To again start the container you can use docker start <container ID>  command. Now how to start a container with custom Name. Below command will help you to start the container with custom name.

Now we will learn how to remove/ delete a container. Use the docker rm <container ID > command to delete a container. How to use custom host name for a container? Use -h flag to give a custom host name to the container.

How to forward port of the container to the host machine? Using the below command you can forward the ports of the container to the host machine. For example let’s forward the 80 port of the container to the host machine.

In the next chapter we will learn few advance docker commands and after that we will learn how to run web applications inside the container.

The following two tabs change content below.

Tapas Mishra

Sr. Engineer (DevOps)
Loves to work on Opensource products. Having experience on Linux environment. Knowledge on Public cloud services like AWS, Rackspace, DigitalOcean, Linode. Please don't hesitate to give a comment on the posts. Your comments are my strength.

Latest posts by Tapas Mishra (see all)

One thought on “Working with Docker – II

Leave a Reply