Remove terminated/unused docker containers from your system

Problem

After playing around with docker containers for some time the terminated/unused ones are still hanging around your system, and prevent you from using a new container with the same name as any of the previous ones with the error:

Error response from daemon: Conflict. The name "container_name" is already in use by container 61f023f06a98. You have to remove (or rename) that container to be able to reuse that name.

Solution

You can remove all the containers by using the following:

docker rm $(docker ps -aq)

Creating a simple rails docker image for testing in cloud deployment

Trying to deploy a rails application in a cloud provider ie dreamhost, that uses OpenStack these are the steps needed:

  1. Install docker in your development machine and your cloud provider by following the installation instructions from here
  2. Create an account in Docker Hub, that will be needed later on to push your docker image with the application
  3. Pull the official rails docker image to your development environment:
    sudo docker pull rails:latest
    
  4. Create a new simple rails application:
    rails new docker_test
  5. Change to the application directory and add a Dockerfile in the root directory containing the following:
    FROM rails:onbuild
  6. Build your new image by using:
    sudo docker build -t rails_docker_test .
  7. Check that your image was build by using:
    sudo docker images
  8. Start the container with:
    sudo docker run --name rails_test -p 0.0.0.0:3000:3000 -d rails_docker_test
  9. Make sure that you can see the initial rails page by using your browser to go to http://127.0.0.1:3000
  10. Push your image to your Docker Hub account by first logging in to it from the command line:
    sudo docker login --username=yourhubusername --email=youremail@company.com

    , and then when you get ‘Login Succeeded’, push your image to your account:

    sudo docker push yourhubusername/rails_docker_test
  11. TBC

Using Elixir with Docker

In order to be able to use Elixir with the help of Docker, so that you can run different containers with different versions, and to have a shared code folder, you could follow the steps below:

  • Install docker in your system. Installation instructions for different systems are here
  • Download the elixir image from the Docker Hub:
    sudo docker pull trenpixster/elixir
  • List the images on your host:
    sudo ps docker images
  • Start a specific version of the elixir docker container (ie elixir 1.0.3):
    sudo docker run -t -i trenpixster/elixir:1.0.3 /bin/bash
  • Use a shared folder with code between the docker container and your host:
    sudo docker run -v /home/user/Prog:/Prog -t -i trenpixster/elixir:1.0.3 /bin/bash

    where the folders after the -v option are /home/user/Prog (host) and /Prog (docker)

  • Use a port forwarding:
    sudo docker run -p 8000 -v /home/user/Prog:/Prog -t -i trenpixster/elixir:1.0.3 /bin/bash

Some useful docker commands

Having started to use docker recently, some of the commands that are needed are a bit difficult to remember. I’m sure that this will change the more that I use it, but as a quick reminder/look up for this initial phase, I’m just going to list some of them here.

List current images:

sudo docker images

List currrent running containers:

sudo docker ps

Saving the state of a container by using the id from above command:

sudo docker commit -m "latest state comment" -a "John Somebody" 7ed30_id_no new_name_of_container:v2

Starting a container with port forwarding:

sudo docker run -p 8000 -t -i new_name_of_container:v2 /bin/bash

Sharing a directory from the host system (/home/user/Prog) inside the container (Prog):

sudo docker run -p 8000 -v /home/user/Prog:/Prog -t -i new_name_of_container:v2 /bin/bash

Docker error – error response from daemon: Cannot start container….is not within /var/lib/docker/aufs/mnt

Problem

You are trying to use an image from Docker in your Ubuntu 14.04 system, but you are getting a error like the following:

014/12/13 17:10:23 Error response from daemon: Cannot start container 4023610855c0551bdc44d0e602f20999c0527da3cfe010169707248887b3a1f0: /var/lib/docker/aufs/mnt/4023610855c0551bdc44d0e602f20999c0527da3cfe010169707248887b3a1f0 is not within /var/lib/docker/aufs/mnt/4023610855c0551bdc44d0e602f20999c0527da3cfe010169707248887b3a1f0

Solution

Your docker version is outdated (ie 1.0.1) so you would need to upgrade your docker installation.
There is a script for this (information from here):

After running the following you should be able to use your images as normal with: sudo docker run -t -i image/name bin/bash:

$ curl -s https://get.docker.io/ubuntu/ | sudo sh