Building and running a Docker container

In this post we’ll go through how to build and run a docker container!

For our example we will use a simple python script, longest_orf.py.

For a recap on what this script does, see the post on Finding the longest orf and in-frame stop codons using Python.

1. Create a working directory

Create a directory on your computer and copy over the longest_orf.py script and the test_frames.faa file from the Sequence-tools repo here.

For this example let’s make a directory called longest_orf_docker in /home/analysis/

Change into this directory in your terminal:

cd /home/analysis/longest_orf_docker

2.Create the Dockerfile.

In a text editor create an empty Dockerfile, eg with nano:

nano Dockerfile

Enter the following and save the text file:

FROM ubuntu:22.04

# install necessary dependencies
RUN apt update && apt install python3 python3-regex -y

# set your working directory
WORKDIR /usr/app/Longest_Orf

# copy your local files to the container
COPY . ./

# create the output directory
RUN mkdir -p /usr/app/Longest_Orf/output

# run the script and redirect output
CMD ["sh", "-c", "python3 ./longest_orf.py ./test_frames.faa test > /usr/app/Longest_Orf/output/seq_id.faa"]

As mentioned in Containerisation for research software, some Docker commands in the Dockerfile create layers.

At a glance we can see that our Docker image will be made up of a few different layers built on top of a base Ubuntu OS layer.

Exploring the Dockerfile

A few things are going on here so let’s go through the Dockerfile in a bit more detail.

1.The FROM command creates the first layer of the image, pulling the ubuntu:22.04 image.

2. The RUN command creates a layer for Ubuntu package installation and python3. As the commands are combined we only create one additional layer here.

3.The WORKDIR command creates another layer, setting the working directory. Note this is not a directory on your computer, this is defining the path where you will copy your files for the container. By default this is also the directory that output will be returned to.

4.The COPY command, using . ./ will also create a layer and copy all files you have in your local directory to the container directory.

For example, this will copy all of the files, including our longest_orf.py script, in/home/analysis/longest_orf_docker to /usr/app/Longest_Orf/

5. The RUN mkdir will build an output directory, creating another layer, note that it’s good practice to have a specified output directory.

6.Finally the CMD command, which does not create a layer, initiates our script.

In the Dockerfile we have also specified that we want to redirect the output by invoking shell (sh) to our output directory in the container: /usr/app/Longest_Orf/output/seq_id.faa

3. Building the image

To build the Docker image we use the docker build command:

docker build -t longest_orf .

In this example we will name our image longest_orf and use . to specify the current directory, telling Docker where to find our files.

4. Run the container

To run the script using the container we use the docker run command.

We also want the results of our script to be output onto our Desktop, to achieve this we use the -v argument in the run command.

This will mount a local directory as a volume.

In our example we mount the longestorf_output directory which will be created on our Desktop after the script has run.

docker run -v ~/Desktop/longestorf_output:/usr/app/Longest_Orf/output longest_orf

Once this has run check the output folder longestorf_output has been created.

Inside it you should find the result file named seq_id.faa 🙌

Additional docker commands

Interactive container access

If we hadn’t specified an output directory in the previous step we would need to use docker cp or interactively access the container to get to our result file.

We can interactively access a container using -it :

docker run -it longest_orf bash 

This takes us to a bash interpreter where we can interact with container in the terminal, in this case we would need to run the script to get the output.

List containers

To list existing docker containers you can use:

docker ps -a

Note you can use the option -a to show all containers (not just the ones currently running).

View image layers and sizes

You can view the date of creation and size of your image layers using:

docker image history longest_orf

Deleting containers and images

In case things go a little pear shaped, find below the commands to remove docker containers and images.

To delete a docker image you need to first delete the containers.

To do this first get the container ID using docker ps -a then use the docker rm command:

docker rm container_id

Once all of the containers associated with your image are deleted you can remove the image using docker rmi command and the name of your image:

docker rmi longest_orf

I hope you found this quick tutorial useful! For Docker documentation and some nice learning modules provided by Docker developers follow this link.

For more tutorials on Docker there are two really nice ones available here and here.

Next we will look at singularity!