Linux:Powerful Server Administration
上QQ阅读APP看书,第一时间看更新

Starting and managing Docker containers

So, we have installed the latest Docker binary. In this recipe, we will start a new container with Docker. We will see some basic Docker commands to start and manage Docker containers.

Getting ready

Make sure that you have installed Docker and set your user as a member of the Docker group.

You may need sudo privileges for some commands.

How to do it…

Let's create a new Docker container and start it. With Docker, you can quickly start a container with the docker run command:

  1. Start a new Docker container with the following command:
    $ docker run -it --name dc1 ubuntu /bin/bash
    Unable to find image 'ubuntu:trusty' locally
    trusty: Pulling from library/ubuntu
    6599cadaf950: Pull complete
    23eda618d451: Pull complete
    ...
    Status: Downloaded newer image for ubuntu:trusty
    root@bd8c99397e52:/#
    

    Once a container has been started, it will drop you in a new shell running inside it. From here, you can execute limited Ubuntu or general Linux commands, which will be executed inside the container.

  2. When you are done with the container, you can exit from the shell by typing exit or pressing Ctrl + D. This will terminate your shell and stop the container as well.
  3. Use the docker ps command to list all the containers and check the status of your last container:
    $ docker ps -a
    

    By default, docker ps lists all running containers. As our container is no longer running, we need to use the -a flag to list all available containers.

  4. To start the container again, you can use the docker start command. You can use the container name or ID to specify the container to be started:
    $ docker start -ia dc1
    

    The -i flag will start the container in interactive mode and the -a flag will attach to a terminal inside the container. To start a container in detached mode, use the start command without any flags. This will start the container in the background and return to the host shell:

    $ docker start dc1
    
  5. You can open a terminal inside a detached container with docker attach:
    $ docker attach dc1
    
  6. Now, to detach a terminal and keep the container running, you need the key combinations Ctrl + P and Ctrl + Q. Alternatively, you can type exit or press Ctrl + C to exit the terminal and stop the container.
  7. To get all the details of a container, use the docker inspect command with the name or ID of the container:
    $ docker inspect dc1 | less
    

    This command will list all the details of the container, including container status, network status and address, and container configuration files.

    Tip

    Use grep to filter container information. For example, to get the IP address from the docker inspect output, use this:

    $ docker inspect dc1 | grep-iipaddr
    
  8. To execute a command inside a container, use docker exec. For example, the following command gets the environment variables from the dc1 container:
    $ docker exec dc1 env
    

    This one gets the IP address of a container:

    $ docker exec dc1 ifconfig
    
  9. To get the processes running inside a container, use the docker top command:
    $ docker top dc1
    
  10. Finally, to stop the container, use docker stop, which will gracefully stop the container after stopping processes running inside it:
    $ docker stop dc1
    
  11. When you no longer need the container, you can use docker rm to remove/delete it:
    $ docker rm dc1
    

    Tip

    Want to remove all stopped containers with a single command? Use this:

    $ docker rm $(dockerps -aq)
    

How it works…

We started our first Docker container with the docker run command. With this command, we instructed the Docker daemon to start a new container with an image called Ubuntu, start an interactive session (-i), and allocate a terminal (-t). We also elected to name our container with the --name flag and execute the /bin/bash command inside a container once it started.

The Docker daemon will search for Ubuntu images in the local cache or download the image from Docker Hub if the specified image is not available in the local cache. Docker Hub is a central Docker image repository. It will take some time to download and extract all the layers of the images. Docker maintains container images in the form of multiple layers. These layers can be shared across multiple container images. For example, if you have Ubuntu running on a server and you need to download the Apache container based on Ubuntu, Docker will only download the additional layer for Apache as it already has Ubuntu in the local cache, which can be reused.

Docker provides various other commands to manage containers and images. We have already used a few of them in the previous example. You can get the full list of all available commands from the command prompt itself, by typing docker followed by the Enter key. All commands are listed with their basic descriptions. To get more details on any specific subcommand, use its help menu, as follows:

$ docker rmi --help

There's more…

Docker images can be used to quickly create runc containers, as follows:

$ sudo apt-get install runc
$ mkdir -p runc/rootfs && cd runc
$ docker run --name alpine alpine sh
$ docker export alpine > alpine.tar
$ tar -xf alpine.tar -C rootfs
$ runc spec
$ sudo runc start alpine

See also