Start a docker container

Start a docker container

To start a docker container from an instance of an image, the command is

docker run imageName

For example, to test if the docker daemon runs and is properly configured, you can run

docker run hello-world

However, in the most cases one would either start a container in the background with some listener ports (e.g. as a webserver for for a database) or then interactively. For example to run my docker image with plenty of bioinformatics tools installed, you can run it interactive with

docker run -it fischuu/bitools

In that case, the ‘-it’ option starts the docker image interactively.

One ‘downside’ with the run command is, that it actually creates everytime a new container instance from the requested image. That means, if you start your working day every time with that command, you will accumulate quite a much containers on your system. The more elegant way would be (maybe) to start a named contained and then come back to it later by referring to its name. This example shows how to do that

docker run -it --name Project1 fischuu/bitools
docker start --interactive Project1

Here we use mainly the same command to initiate a docker container, but this time we assign also an own name (–name Project1) to it. That name can then later be used to refer to that container. If you exit the interactive run of that container and you would like to come back to where you were (here your new created folders remain, as it is still the same container instance, you do not spawn a new one!), you can start the container then interactively again by using the ‘start’ options instead of the ‘run’ option.

In many cases it is desired to start a container such that it can access files from the host system. For that, volumes can be attached to the container, using the ‘-v’ option. If, for example, the folder ‘/home/daniel/Project1/’ should be available in the docker container, in the folder ‘/projectData’, the command to initiate an interactive container for that is

docker run -it -v /home/daniel/Project1:/projectData fischuu/bitools

that means, the ‘-v’ option requires two options, separated by ‘:’, first the folder address on the host system and then the mount point in the container. Of course, this method can be combined also with a named call. The start command would then be the same and only the initial run command would get also the -v option still.

One other use case is that one container hosts the data of a project and that is then made available for another docker container. That option is ‘–volumes-from’, but so far I haven’t used that so much, as I am not sure what is the largest container size that can be pushed to Docker hub.