Getting Started With Docker : Working With Containers

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.

  1. Containers are sandboxed environments which run multiple processes sharing the same host kernel.
  2. 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.
  3. All the layers in an image are read-only except the topmost layer. The writable layer can be called as a container.
  4. 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.
  5. Each container has its own network configurations and unique id (64 hexadecimal digits).
  6. 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).

You might like: Docker Video Course for beginners

Docker Contianer ArchitectureImage 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.

  1. run
  2. ps
  3. stop
  4. start
  5. attach
  6. exec
  7. logs
  8. inspect
  9. rename
  10. restart
  11. 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.

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

When 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.

Also, 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 ps command

This command lists all the containers in the host. Let’s have a look at few examples.

READ  Running Custom Scripts In Docker With Arguments - ENTRYPOINT Vs CMD

The following command lists all the containers.

To list all the running container, use the following command.

To list all the containers which got launched recently, execute the following command.

docker stop command

This command stops the running container.

docker start command

This command starts a stopped container.

docker restart command

This command restarts a running container.

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

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 logs command

This command gives you all the logs of a container.

READ  Kubernetes Deployment Tutorial For Beginners

docker inspect command

This command gives you all the information about a specific container. The output of this command is in JSON format.

To 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 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 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.

If you want to remove a running container without stopping it, you can use the “-f” force flag with the rm command as shown below.

You can also use one-liners, which will stop and  remove all the containers from the host.

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.

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.