So far we have been discussing using containers, largely from inside the container. However, sometimes you simply want to use tools from inside your docker container rather than having the dependency nightmare of having everything installed locally (or on your server).

Attaching/mounting into your Docker container

The process of attaching or mounting local files into your container will give you the ability to use those files inside your container. Or store new files created from inside your container to the local file system. This can be really useful in two ways. Firstly, you can look at using the container to run processes such as npm/yarn or composer or ruby gems or you can look at services, such as a webserver. It gives you the ability to have multiple, separate installations of the same software, and simply and easily manage those processes or services with minimal pain of upgrade or management.

docker run -v /host/directory:/container/directory
docker run -v $(pwd):/container/directory #$(pwd) is your current directory

Selecting your workdir

On a proportion of containers you are going to want to select your workdir (-w) so that you can use the tools straight away as if they are installed locally. This is done by using the -w tag and noting the /container/directory

docker run -v /host/directory:/container/directory -w /container/directory

Knowing your Entrypoint

Your entrypoint is what the command that is running on your container. For some it will be /bin/bash or for containers that have a specific purpose it could be composer (for the composer/composer containers) or node (for the node container). What you have to understand is that the command at the end is running after your entrypoint, so it is similar to having the entrypoint at the front of the command.

Running, Installing and Services inside Containers

The process of attaching or mounting local files into your container will give you the ability to use those files inside your container. Or store new files created from inside your container to the local file system. This can be really useful in two ways. Firstly, you can look at using the container to run processes such as npm/yarn or composer or ruby gems or you can look at services, such as a webserver. It gives you the ability to have multiple, separate installations of the same software, and simply and easily manage those processes or services with minimal pain of upgrade or management.

Discover Docker Images ID

First you are going to need to know the image ID.

➜  ~ docker images
REPOSITORY          TAG           IMAGE ID            CREATED          SIZE
bbcnews/wraith      latest        c675f285cbbc        2 days ago       1.192 GB
node                alpine        a1c188c2c5e1        4 days ago       55.29 MB
frapsoft/yarn       latest        ea15dfeff4f4        2 weeks ago      55.05 MB
composer/composer   alpine        6d48ee9405f5        9 weeks ago      317.6 MB

Docker Run inside Container

Then you can run your command inside the container. You will need to understand what the default running

docker run -v /host/directory:/container/directory -w /container/directory -it [IMAGE_ID]  your command here

Running What?

A lot of different containers have different entrypoints and commands. I have edited this post to include links to my next post which is on real world examples of running Composer, NPM, Yarn and Wraith inside a container. Check it out.