Securing Docker containers

In this recipe, we will learn Docker configurations that may result in slightly improved security for your containers. Docker uses some advanced features in the latest Linux kernel, which include kernel namespaces to provide process isolation, control groups to control resource allocation, and kernel capabilities and user namespaces to run unprivileged containers. As stated on the Docker documentation page, Docker containers are, by default, quite secure.

This recipe covers some basic steps to improve Docker security and reduce the attack surface on the Ubuntu host as well as the Docker daemon.

How to do it…

The first and most common thing is to use the latest versions of your software. Make sure that you are using the latest Ubuntu version with all security updates applied and that your Docker version is the latest stable version:

  1. Upgrade your Ubuntu host with the following commands:
    $ sudo apt-get update
    $ sudo apt-get upgrade
    
  2. If you used a Docker-maintained repository when installing Docker, you need not care about Docker updates, as the previous commands will update your Docker installation as well.
  3. Set a proper firewall on your host system. Ubuntu comes preinstalled with UFW; you simply need to add the necessary rules and enable the firewall. Refer to Chapter 2, Networking for more details on UFW configuration.

    On Ubuntu systems, Docker ships with the AppArmor profile. This profile is installed and enforced with a Docker installation. Make sure you have AppArmor installed and working properly. AppArmor will provide better security against unknown vulnerabilities:

    $ sudo apparmor_status
    
  4. Next, we will move on to configure the Docker daemon. You can get a list of all available options with the docker daemon --help command:
    $ docker daemon --help
    
  5. You can configure these settings in the Docker configuration file at /etc/default/docker, or start the Docker daemon with all required settings from the command line.
  6. Edit the Docker configuration and add the following settings to the DOCKER_OPTS section:
    $ sudo nano /etc/default/docker
    
  7. Turn off inter-container communication:
    --icc=false
    
  8. Set default ulimit restrictions:
    --default-ulimitnproc=512:1024 --default-ulimitnofile=50:100
    
  9. Set the default storage driver to overlayfs:
    ---storage-driver=overlay
    
  10. Once you have configured all these settings, restart the Docker daemon:
    $ sudo service docker restart
    
  11. Now, you can use the security bench script provided by Docker. This script checks for common security best practices and gives you a list of all the things that need to be improved.
  12. Clone the script from the Docker GitHub repository:
    $ git clone https://github.com/docker/docker-bench-security.git
    
  13. Execute the script:
    $ cd docker-bench-security
    $ sh docker-bench-security.sh
    
    How to do it…

    Try to fix the issues reported by this script.

  14. Now, we will look at Docker container configurations.

    The most important part of a Docker container is its image. Make sure that you download or pull the images from a trusted repository. You can get most of the images from the official Docker repository, Docker Hub.

  15. Alternatively, you can build the images on your own server. Dockerfiles for the most popular images are quite easily available and you can easily build images after verifying their contents and making any changes if required.

    When building your own images, make sure you don't add the root user:

    RUN group add -r user && user add -r -g user user
    USER user
    
  16. When creating a new container, make sure that you configure CPU and memory limits as per the containers requirements. You can also pass container-specific ulimit settings when creating containers:
    $ docker run --cpu-shares1024 --memory 512 --cpuset-cpus 1
    
  17. Whenever possible, set your containers to read-only:
    $ docker run --read-only
    
  18. Use read-only volumes:
    $ docker run -v /shared/path:/container/path:ro ubuntu
    
  19. Try not to publish application ports. Use a private Docker network or Docker links when possible. For example, when setting up WordPress in the previous recipe, we used a Docker network and connected WordPress and MySQL without exposing MySQL ports.
  20. You can also publish ports to a specific container with its IP address. This may create problems when using multiple containers, but is good for a base setup:
    $ docker run -p 127.0.0.1:3306:3306 mysql
    

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset