Docker images Filter Options

Docker images Filter Options

Get Social!

The below is an excerpt from docker.com listing the –filter options available for docker images.

danglingboolean – true or false – will show dangling images.
label
label=<key> or label=<key>=<value>
before
<image-name>[:<tag>]<image id> or <[email protected]> – filter images created before given id or references
since
<image-name>[:<tag>]<image id> or <[email protected]> – filter images created since given id or references
reference
(pattern of an image reference) – filter images whose reference matches the specified pattern

Docker ps Filter Options

Get Social!

The below is an excerpt from docker.com listing the –filter options available with docker ps.

idContainer’s ID
nameContainer’s name
labelAn arbitrary string representing either a key or a key-value pair. Expressed as <key> or <key>=<value>
exitedAn integer representing the container’s exit code. Only useful with --all.
statusOne of createdrestartingrunningremovingpausedexited, or dead
ancestorFilters containers which share a given image as an ancestor. Expressed as <image-name>[:<tag>],<image id>, or <[email protected]>
before or sinceFilters containers created before or after a given container ID or name
volumeFilters running containers which have mounted a given volume or bind mount.
networkFilters running containers connected to a given network.
publish or exposeFilters containers which publish or expose a given port. Expressed as <port>[/<proto>] or <startport-endport>/[<proto>]
healthFilters containers based on their healthcheck status. One of startinghealthyunhealthy or none.
isolationWindows daemon only. One of defaultprocess, or hyperv.
is-taskFilters containers that are a “task” for a service. Boolean option (true or false)

Remove Docker Container Based On Regex

Get Social!

This simple one-liner will take a regular expression (regex) and remove any Docker containers matching the pattern based on the name field. You can change the name match to be any other field accepted by the –filter switch.

Run the following docker ps command and substitute NAMEHERE* with the pattern you’d like to match. Careful, this command will delete any containers it finds.

You can also filter on various other keys, such as status and volume using exactly the same method. Just replace the –filter element with the key from the below table, and the expression you want to match. 

idContainer’s ID
nameContainer’s name
labelAn arbitrary string representing either a key or a key-value pair. Expressed as <key> or <key>=<value>
exitedAn integer representing the container’s exit code. Only useful with --all.
statusOne of createdrestartingrunningremovingpausedexited, or dead
ancestorFilters containers which share a given image as an ancestor. Expressed as <image-name>[:<tag>],<image id>, or <[email protected]>
before or sinceFilters containers created before or after a given container ID or name
volumeFilters running containers which have mounted a given volume or bind mount.
networkFilters running containers connected to a given network.
publish or exposeFilters containers which publish or expose a given port. Expressed as <port>[/<proto>] or <startport-endport>/[<proto>]
healthFilters containers based on their healthcheck status. One of startinghealthyunhealthy or none.
isolationWindows daemon only. One of defaultprocess, or hyperv.
is-taskFilters containers that are a “task” for a service. Boolean option (true or false)

See Docker PS Filter Options.

You can also filter for multiple conditions by passing the –filter switch multiple times. For example, name=webserver and status=running would look like this:


Using Dockerfiles to build new Docker images

Category : How-to

Get Social!

docker-logoIn a previous blog post I detailed the steps involved in creating a new Docker container, making some changes and saving the image back to the local repository. The process described works well but it’s a very manual affair which Docker has a solution for.

Docker has a scripting language which can be used to create a new instance with a predefined list of commands and properties which will be used to create your new Docker instance.You could, for example, use a docker file to install Apache, configure the firewall and any further configurations we may need to make.

The benefits to using a Dockerfile, rather than making all the changes directly and saving the image are that the underlying OS and the additions that you wish to make are completely independent. You can, for example, run a Dockerfile on any OS image. Using the example that follows, you could run the Dockerfile on either a Debian or Ubuntu OS without changing a thing.

Create a directory to hold your DockerFile project, which we’ll call apache2 for this example.  I’ll be placing all my DockerFiles in their own project directory under dockerfiles in my home directory.

Open a text file named Dockerfile in your favourite text editor in the project folder we just created. This is the standard file structure that Docker expects when creating DockerFiles.

There are various commands we can use within a Dockerfile. The first command is the FROM statement which indicates which image should be used when creating your instance. I’m going to use the ubuntu image which I have previously downloaded to my local Docker server.

Add MAINTAINER or author for the template. This is your name, username or whatever handle you’d like to be known as.

We are now going to use the RUN command to specify the commands that should be executed on the instance during creation. The commands will be executed in the order they appear in the Dockerfile. We will be installing Apache2 so we’ll be using the apt-get command to install.

Next we’ll make a data directory on the host where we will keep our web files that are to be served by Apache.

It’s a good idea to separate the Docker container from any user data so that a the container can be used for different purposes. What this mean in our example is that we will keep all the website data (HTML files, etc.) out of our container, leaving only the Apache software and general configuration within the container. This means that we can reuse our Docker image to create containers for other websites.

Using the Docker VOLUME command we can map a directory on the Docker host to a folder inside the container which will be configured once your container is created. The below example makes the directory /data/apache/www available for mapping later.

Add the VOLUME reference to your Dockerfile.

We will need to be able to reach our container on port 80 so that we can use the Apache service over the network. Docker uses the EXPOSE command followed by a port number to allow incoming traffic to the container. Add the below entry to allow port 80.

We now need to do some find and replace magic to change the Apache default site configuration to point to our new location, rather than the Apache default. This isn’t a Docker specific command, but is required for this example.

Finally we’ll need to tell Docker what should be executed in order to ‘run’ this container.  For this example, we use the apache2ctl command with the FOREGROUND switch.

And that’s it, your first DockerFile. Run your newly created DockerFile to build the image by changing to the project directory and using the docker build command to create it. Use the -t switch to specify a tag for the image.

It will take a few minutes for the image to build. Once complete, you’ll be able to see it in the Docker image list by using the command docker image.

 

The whole DockerFile:

 


Create Your First Docker Container

Category : How-to

Get Social!

docker-logoDocker is probably one of the easiest environments to create a virtualised instance based on a number of flavours of operating systems. Rather that having to install an operating system yourself, you can download one of the many guests templates or ‘images’ available directly from the Docker community.

See my blog post on installing Docker on Ubuntu 14.04 if you don’t currently have Docker installed.

There are a number of commands which are required to manage Docker containers and images. First off, let’s see if we have any images in our local Docker library.

The docker images command lists the available local images which you can use to create a Docker container. The above output does not show any local images so lets download one from the central Docker repository.

We must choose which image to download and use to create our first Docker container. There are literally thousands of images available on the central repository and all can be downloaded through the docker command. Let’s use the search command to find an image to download.

This will display a huge list of all the images available containing the word ubuntu. As you can imagine, there will be hundreds because not only are base OS images available, but customised images containing specific applications or set ups.

Let’s download the basic ubuntu 14.04 image:

You can check this has downloaded the image to your local store with the above docker images command. We will also need to make a note of the image ID so that we can use it to create a container from it.

The next step is to create a container and make the required changes. Creating a container is Docker is done with the run command followed by, amongst other things, a command to run within the container. We are going to create a new container and use a bash session to customise the container before saving it as a new image for use in the future.

Create the Docker container with the run command and specify the bash shell to be executed on completion. This will leave us with a bash session which we can use the customise the image. Replace the ad892dd21d60 ID with the ID of the image we downloaded in the previous step.

You now have an active shell on the container which has been created with the id 3a09b2588478. Type exit to end the session in your guest container and the container will be stopped and kept available on your Docker system.

Run the ps Docker command to see what containers are known to your Docker system.

The above output shows 3 containers which are available in my Docker system with the container ID on the left. We can re-enter one of these containers to make our changes, but first we need to start it. I’m going to use container ID 3a09b2588478 for the rest of this example but yours will be a different ID.

We can now attach to the container to create a shell where we can make our modifications.

You now have a shell running on the container which you can use to make your changes to the container. Let’s keep it simple and just run an upgrade with apt-get and then exit. In the real world, you might install an application, or define your configuration such as LDAP SSH login.

The last step in our example is to save the container as a new image which can be used to create future Docker containers. You’ll need to specify the container ID as well as the name of the image to use. You can specify a new image name or overwrite the existing image name.

 

And that’s all there is to it! You have created a new Docker container, from one of the images available from Docker, made some changes and saved it locally for future use. Of cause, there are plenty more ways to use Docker, but I hope this has been useful for getting a basic understanding of how Docker works.

Next steps: See my post on using a Dockerfile to automate Docker image creation.

Quick Poll

Are you using Docker.io?

Export and Import a Docker Image Between Nodes

Get Social!

docker-logoOne of the driving forces behind Docker is to create a consistent environment across all Docker enabled machines and to create portable templates, or images, which can be ran on any Docker enabled server.

It would, therefore, make perfect sense that Docker have made it very easy for us to export a running container and re-import it on another Docker server.

Lets assume, for this example, that you have a running container that you would like to move to another host. The summary of the process is to save the container to an image, save it to a tar file, move it to your new host and load the image into the new docker server.

Find the ID of the container that you would like to move.

I’m going to use the above 3a09b2588478 ID for this example.

Commit your changes and save the container to an image called mynewimage.

Save the mynewimage image to a tar file. I will use the /tmp/ directory to save the image but you could easily use a NFS share to make it easier to move the completed tar file.

Copy the mynewimage.tar file to your new Docker instance using whatever method works in your environment, for example FTP, SCP, etc.

Run the docker load command on your new Docker instance and specify the location of the image tar file.

Finally, run the docker images command to check that the image is now available.


Visit our advertisers

Search

Quick Poll

Do you use GlusterFS in your workplace?

Visit our advertisers