Docker: 7 Essential Things to Get Started

onestop_databy:

Docker

This tutorial teaches the 7 essential things to get started with Docker. They are: What is Docker, how to install docker on Ubuntu, Mac Os, or Microsoft Windows, what is a Dockerfile. Moreover, it will teach how to use the commands Docker build, run, image, and pull.

1. What is Docker?

First and foremost, Docker is an aggregate of the platform as a service (PAAS) products and tools using OS-level virtualization to create, deploy, build, and run applications in packages known as Containers.

The Containers enable developers to deliver a software application with all the tools it needs like libraries, dependencies and deploy as one package. This assures the developer that his application will definitely run on any other machine irrespective of differences in the machine’s customized settings from the machine which is used to write and test the code.

The software hosting the docker containers is known as Docker Engine. Its service is available both as free and premium versions, and it is founded by Solomon Hykes and is first started in 2013 in Santa Clara.

1.2 Docker vs Traditional Virtual Machine

Docker might look similar to the traditional virtual machine but is actually a lot different. Unlike a virtual machine, it creates a complete virtual operating system.

Due to this, applications can use the same Linux kernel as the original developer system and only need the application to be deployed and shipped along with the things that are not available on the new host system. This boosts up the performance and reduces the application significantly.

1.3 Users and Uses of Docker

Docker has the capability to bundle up the application or software, and all its dependencies in a virtual container that can run on any Linux server contributing to its flexibility, portability.

Hence it can run on any kind of location, say it is on-premises or public cloud or private cloud. It is designed and developed to benefit the developers and the system administrators by including it as a part of most of the DevOps chains. It helps developers to focus on writing code rather than worrying about the computer or the system that it should finally run on.

Also, it gives them a head start to use thousands of programs already designed and developed to execute in Docker containers as part of developing their applications. Docker offers flexibility and a potential reduction in the number of required systems because of its small size and less overhead. Docker containers being lightweight, a single machine can run many docker containers in parallel.

It is an open-source enabling everyone to contribute and extend to meet their respective requirements if the features they need are out of the box.

1.4 Security with Docker

Its containers give better security for applications that are in a shared environment. Of course, while they do not completely replace the existing security measures, they do enhance the security of an application.

Docker is a containerization technology that is currently trending so well and many companies have moved from virtual machines to containers that it is directing various technologies, software, and tools to be built as Docker compatible.

In case you want to learn more about it, please check this Youtube video:

In short words: Docker can be defined as a much lighter version of a Virtual machine.

2. How to Install Docker?

Next, Docker contains great documentation on how to install it on Ubuntu, Mac OS, and Microsoft Windows. Please check it out and get your system set up before we can move on with the other essential things you need to learn about it.

3. What is a Dockerfile?

Furthermore, a Dockerfile defines the instructions that will be executed when building a docker image. Any program and settings wanted to be in a Docker image should be defined in the Dockerfile before building it.

For instance, bellow there is a Dockerfile for one of my graduate school tools. The file defines the use of miniconda image version 4.7.10, selects a Python 3.6 environment, creates the bioconda channels, installs FOCUS version 1.4 using conda, and sets what is the entry point when I run the image.

# conda setup
FROM continuumio/miniconda3:4.7.10
RUN conda create -n env python=3.6
RUN echo "source activate env" > ~/.bashrc
ENV PATH /opt/conda/envs/env/bin:$PATH

# creating conda environment
RUN conda config --add channels defaults
RUN conda config --add channels bioconda
RUN conda config --add channels conda-forge

# installs focus using bioconda
RUN conda install focus==1.4

# Entrypoint
CMD /bin/bash

It is simple, isn’t it? It was defined everything that the image contains, and that is what should be in it.

4. Build Image

Now that it was defined everything that will be in the image, it should be built with the following command at the same folder of the Dockerfile:

# command
$ docker build -t {IMAGE_NANE}:{TAG} .

$ # in my case
$ docker build -t one_stop_data_analysis:0.1 .

If a {VERSION} is not passed, the image will receive the ‘latest’ as a tag. Next, once the build is concluded, we should see a tail message such as:

Removing intermediate container 06a822133317
 ---> f096ba172202
Step 9/9 : CMD /bin/bash
 ---> Running in 5ced4ae9cb17
Removing intermediate container 5ced4ae9cb17
 ---> a2061ccf40b1
Successfully built a2061ccf40b1

5. Docker Images

Now that the image was successfully built, its information can be checked by using the images subcommand:

$ docker images

# Displays

$ docker images
REPOSITORY             TAG     IMAGE ID       CREATED            SIZE
one_stop_data_analysis  0.1   a2061ccf40b1  2 minutes ago       1.05GB

As shown above, the Docker image name is ‘one_stop_data_analysis’, the tag that was defined `0.1`, and the Image ID, when it was created and the size of the image.

If the build command is executed again for another tag, it shows display another row to the images above and/or if it is given another name.

6. Run Image

Next, now that the image was build and it has a name, tag, and ID, it can be run to make sure that everything is working. There are many ways to run a docker image, and below there are different flavors:

6.1 Run the Latest Version of an image

# syntax
$ docker run -i -t {IMAGE_NAME}
# running our image
$ docker run -i -t one_stop_data_analysis

6.2 Run a Specific Version for of the Image

# syntax
$ docker run -i -t {IMAGE_NAME}:{TAG}
# running our image
$ docker run -i -t one_stop_data_analysis:0.1

6.3 Run with the Image ID

# syntax
$ docker run -i -t {IMAGE_ID}
# running our image
$ docker run -i -t a2061ccf40b1

Let’s make sure it really installed what we defined in the Dockerfile. For example, it was installed FOCUS which has Jellyfish as one of its dependencies. Thus, when running the image, it should have FOCUS and Jellyfish installed.

# run our image
$ docker run -i -t one_stop_data_analysis:0.1

# confirm FOCUS is installed in the path
$ which focus

# confirm Jellyfish is installed in the path
$ which jellyfish

Awesome! Now we have can run FOCUS and Jellyfish on using an image. Well… Yes, we can run FOCUS and Jellyfish in the Docker image, but we need to tell the image where the files we want to live on our computer. Docker is like a virtual machine, remember? So it is needed to tell the virtual side of the machine where the files we want to mount the machine live.

Don’t worry! It is pretty simple – see below

6.4 Run Image with a Mount Volume

This allows us to define amount volume (directory) on our computer side which will be also present on the virtual side.

# syntax
$ docker run -i -v {PATH_LOCAL_MACHINE}:/{FOLDER_NAME_VM} -t {IMAGE_NAME}:{TAG}

# example
docker run -i -v /Users/onestop_data/Desktop:/my_files_virtual_side -t one_stop_data_analysis:0.1

Attention:

  • {PATH_LOCAL_MACHINE}: Path to the directory mount to on our computer side
  • {FOLDER_NAME_VM}: Path to mount on the virtual side

If we want to mount more than one folder, we need to pass multiple ‘-v’ calls in the docker run call.

Now, we can test that the directory was indeed mounted

# example
docker run -i -v /Users/onestop_data/Desktop:/my_files_virtual_side -t one_stop_data_analysis:0.1

# Inside the image, we check for the defined folder
$ (env) root@bb202535f57a:/# ls my_files_virtual_side/
$ my_files/  tutorials/ 

Great! At this point, we learned so much about Docker. We can create a Dockerfile, build the image, and run it in multiple ways even mounting a directory from our computer to it.

7. Docker Pull Image

Last but not least, Docker pull is great if we have an image on the cloud, we don’t need to define the Dockerfile and build it, we just need to pull it into a computer and run as taught above.

For example, bioconda has a repository for tools that we can just pull the image. CheckM container name on the cloud is quay.io/biocontainers/checkm-genome, so we can pull the image from the web and pass which everything we want to be pulled – see below.

$ docker pull quay.io/biocontainers/checkm-genome:1.1.2--py_0

Once we pull the image, we can check if it indeed exists and we can run it

# check for image
$ docker images | grep checkm-genome

# found image and get ID
$ quay.io/biocontainers/checkm-genome 1.1.2--py_0 04fa265258d1 1 minute ago  1.1GB

# run image and mount to folder with files
$ docker run -i -v /Users/onestop_data/Desktop:/my_files_virtual_side -t 04fa265258d1

# check for tool inside of image
$ which checkm
$ /usr/local/bin/checkm

More Resources

Here are two of my favorite Docker Books in case you want to learn more about it.

Conclusions

In summary, we learn all the essentials commands needed to use Docker on our computer. Also, we learned how to create a Dockerfile with the instructions we want. I hope this was very clear, and you were able to see the beauty of this tool.

If there is anything else that you would like to learn about this tool, please leave a comment below, and I will be more than happy to help.

Related Posts