Using Docker macOS / Ubuntu 14.04 LTS

#Option 1: Installing on macOS

Let's start off by installing Docker on our Mac. Visit the docker site to download the Mac application.

Creating an Image

Then we will need to create a directory in which to start building our image.

cd ~ && mkdir first-docker-image

After creating your directory, let's create an image in which we can create our own container. We will first start off by creating a Dockerfile which will hold our image and its dependencies.

cd first-docker-image && touch Dockerfile

Once created, vim into our file and paste the code you need for your container. For me, I will do the one I run ruby code in.

FROM ruby:2.4.1
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
WORKDIR /run-tests
RUN gem install rspec

Here are some additional commands you can have.

# COPY Gemfile /myapp/Gemfile
# COPY Gemfile.lock /myapp/Gemfile.lock
# RUN bundle install
# COPY . /myapp

Building The Image

After that is done, we then need to build our image so that we can use it and generate a container to install our stuff in.

docker build -t rubyrspectests .

You will see after running this command that it will install all of the dependencies you specified you needed, and then we will run it later in a UNIX container provided by Docker.

Running The Container

To run our container we can simply use the following command.

docker run -it rubyspectests

But for my case, I need to run it in a certain directory and then make it read only so that no malicious code can be run.

docker run -it -v `pwd`:/run-tests:ro rubyrspec rspec rspect_spec.rb

# You can also run it using bash to have a command line within your container.
docker run -it -v `pwd`:/run-tests:ro rubyrspec bash

# The -w specifies a working directory, a little redundant, but good for debugging
docker run -it -v `pwd`:`pwd` -w `pwd` rubyrspec bash

# Running the image on the ubuntu server
sudo docker run -v /path/to/testing/files:/run-tests:ro username/rubyrspec rspec rspect_spec.rb

# Run specific folder within Docker image
docker run -tiv /path/to/desired/dir:/root/destination_folder image:tag

So in order to run it in the path I need to and make the files accessible to the container, I have to specify a volume I need to be available.

Tagging, Pulling, and Pushing to Docker Cloud

To tag, pull, and push your image to the Docker Cloud, simply login and use the following commands in your terminal. Docker will always check locally first and if it finds nothing, it will check remotely.

docker login
docker tag my_image username/my_image
docker push username/my_image
docker pull username/my_image

To build from an existing image, use the following code below.

docker build -t username/name:tag .
docker push username/name:tag

#Option 2: Installing on Ubuntu 14.04 LTS

Let's update our packages before proceeding.

sudo apt-get update

Now let’s install the docker-io package:

sudo apt-get -y install

Now we need to link and fix paths.

sudo ln -sf /usr/bin/ /usr/local/bin/docker
sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/

Optional, to configure Docker to start when the server boots.

sudo update-rc.d defaults

Downloading Your Docker Container

sudo docker login
sudo docker pull username/repo

Running a Docker Container

Docker will run the following command in a new container, -i attaches stdin and stdout, -t allocates a tty, and we’re using our own repo.

sudo docker run -it username/repo

If you need to search for a bunch of containers that are already pre-made, you can do the following.

sudo docker search any_package

Additional Commands

You can also run a few of these commands to see extra details.

# This will show you containers currently running
docker ps

# This will show a history of run containers
docker ps -a

# Force destroy a container
docker rm -f XXXXX