Your First SLES Container
How do you trust the random Docker hub package out there that may or may not have open-sourced their Dockerfile? It may be fine for experimenting with the Docker platform, but for mission critical applications, it’s best to know exactly what’s involved.
Docker in SLES gives you all of the power of Docker with the stability and security of Enterprise Linux.
Gathering the pieces:
For this demonstration, you’ll need a registered SUSE Linux Enterprise 12 SP2 server or vm with at least 10GB of storage for your images and containers.
You’ll also need to install the extensions and modules that you’ll want to include in your containers. For example, SDK or the Web and Scripting Module and of course the Containers Module for docker itself.
Your First Dockerfile:
Docker’s official documentation will give you a fantastic overview on the basics of building a Dockerfile. However building one using SLES adds a few extra steps that ensures you are getting official SUSE base images.
Set up Docker for the First Time:
After finishing the installation of SLES and choosing the modules that you want to add to your Dockerfile, install Docker by simply running:
zypper in docker
Add your User ID to the docker group for easy management of the docker service.
usermod -aG docker <userid>
Log out and then back in to apply the new group membership and then enable and start the docker service.
systemctl enable docker systemctl start docker
Most of the time Docker images are stored in Docker’s own repository and can be downloaded via the docker pull command. SLES base images are downloaded as RPM files and then activated.
jsevans@dockerlab:~> zypper se *-image Repository 'SLES12-SP2-Updates' is out-of-date. You can run 'zypper refresh' as root to update it. Repository 'SLE-SDK12-SP2-Updates' is out-of-date. You can run 'zypper refresh' as root to update it. Loading repository data... Reading installed packages... S | Name | Summary | Type --+------------------------+--------------------------------------------------------+----------- | sles11sp3-docker-image | SUSE Linux Enterprise Server 11sp3 image for Docker | package | sles11sp4-docker-image | SUSE Linux Enterprise Server 11sp4 image for Docker | package | sles12-docker-image | SUSE Linux Enterprise Server 12 image for Docker | package | sles12sp1-docker-image | SUSE Linux Enterprise Server 12sp1 image for Docker | package i | sles12sp2-docker-image | SUSE Linux Enterprise Server 12sp2 image for Docker | package
You can download them line any other package from zypper.
zypper in sles12sp2-docker-image
After downloading the images that you want to use, activate them with the sle2docker command.
jsevans@dockerlab:~> sle2docker list Available pre-built images: - sles12sp1-docker.x86_64-1.0.5-Build10.18 - sles12sp2-docker.x86_64-1.0.0-Build3.2
jsevans@dockerlab:~> sle2docker activate sles12sp1-docker.x86_64-1.0.5-Build10.18 Verifying integrity of the pre-built image Activating image suse/sles12sp1:1.0.5 activated
You can now see the new SLES base image when you run the docker images command.
jsevans@dockerlab:~> docker images REPOSITORY TAG IMAGE ID CREATED SIZE suse/sles12sp1 1.0.5 0a4d00395608 33 seconds ago 98.88 MB suse/sles12sp1 latest 0a4d00395608 33 seconds ago 98.88 MB
Writing your Dockerfile:
Copy the .repo and .service modules that you will need in the current working directory. In my example, I will be using the Web and Scripting Module.
cp /etc/zypp/repos.d/Web_and_Scripting_Module_12_x86_64\:SLE-Module-Web-Scripting12-Pool.repo <my working directory> cp /etc/zypp/repos.d/Web_and_Scripting_Module_12_x86_64\:SLE-Module-Web-Scripting12-Updates.repo <my working directory> cp /etc/zypp/services.d/Web_and_Scripting_Module_12_x86_64.service <my working directory>
Open your text editor and begin writing your Dockerfile. In this example, I will create a Dockerfile that will create a very simple web service with Apache and PHP 7.
FROM suse/sles12sp2 MAINTAINER "Jason Evans <email@example.com>" # The ADD command adds files from your directory into the new image ADD *.repo /etc/zypp/repos.d/ ADD *.service /etc/zypp/services.d RUN zypper refs && zypper refresh RUN zypper --non-interactive in apache2 \ apache2-mod_php7 ADD index.html /srv/www/htdocs/ CMD /usr/sbin/apachectl -D FOREGROUND EXPOSE 80
The most important thing to remember when writing a Dockerfile for SLES is this block of code:
ADD *.repo /etc/zypp/repos.d/ ADD *.service /etc/zypp/services.d
Without it, you are limited to writing Dockerfiles with only a limited selection of packages in the base repository.
Your First Container:
With the exception of the special requirements for writing Dockerfiles, the rest of Docker for SLES works just like any other installation. In order to run a Dockerfile, simply run:
docker built -t myfirstimage:latest .
A new image is now available is now available. Using the created image from the example Dockerfile from above, you can run it as a container by doing this:
docker run -d -p 80:80 myfirstimage:latest
This will run Apache on port 80. Here you can find a more detailed version of my sample Dockerfile.
Docker is an amazing tool. You can easily develop and run nearly any application written for any platform. In the above application, I wrote a very simple web service using SLES, but I could easily attach a container to it with a MariaDB database written for Debian and a PHP application written in OpenSUSE. All of this could be done without the system overhead of VM’s and a hypervisor to control them. This is just touching the tip of the iceberg when it comes to what containers can provide.