home.


Tagged: docker


Docker: Simple script with Java8, Jetty, Scala, Git

We’ll run this from ubuntu trusty.

FROM ubuntu:trusty-20160711
# Install ubuntu instllation tools and the java 8 repo
RUN apt-get update && apt-get -y install software-properties-common && add-apt-repository -y ppa:webupd8team/java
# Auto accept the java 8 licence
RUN echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections
RUN echo debconf shared/accepted-oracle-license-v1-1 seen true | debconf-set-selections
# Install java8, git and wget
RUN apt-get update && apt-get -y install oracle-java8-installer git wget vim
# Download and install scala 2.11.8
RUN wget http://downloads.lightbend.com/scala/2.11.8/scala-2.11.8.deb && dpkg -i scala-2.11.8.deb
# Download the jetty runner web server
RUN wget http://central.maven.org/maven2/org/eclipse/jetty/jetty-runner/9.4.0.M0/jetty-runner-9.4.0.M0.jar
# Ignore this if you don't want something run on every container start
ENTRYPOINT /bin/bash -c "ls /"
docker

Docker: Stop a running docker container from a script

If you want to stop a docker container from a script, you may not know the container id or name, so docker stop may be insufficient.

With gawk and grep, however, you can get around that.

docker ps | grep IMAGE_NAME | gawk '{print $1}' | xargs docker stop || true

This firstly lists all the running containers.

Then it searches for any line with IMAGE_NAME in it.

Then it sends it to gawk and we print the container id.

Then we send that to docker stop.

Finally we return ‘true’ so as not to halt a script if it can’t find such a container.

docker

Docker: install Java8 automatically on ubuntu/debian

If you want to create a docker image with Java, then there’s a problem: it’ll ask you manually confirm that you agree to the terms and conditions.

To automatically do this, add some debconf-set-selections to your script.

So the steps are now update, install software properties utils, add the webupd8team repo, set the debconf selections, update again, install java.

apt-get update
apt-get -y install software-properties-common
add-apt-repository -y ppa:webupd8team/java
echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections
echo debconf shared/accepted-oracle-license-v1-1 seen true | debconf-set-selections
apt-get update
apt-get -y install oracle-java8-installer
docker java

Docker: Mount a host volume / directory

If you want to give a Docker container access to a direcory on your host system, you need to use volumes, the -v argument to docker run:

docker.io run -t -i -v /var/:/host_var/:ro ubuntu /bin/bash

This command will mount the host’s /var/ directory as /host_var/ in the container with read only access.

unix docker docker-volumes

Installing Docker in Debian (testing/Jessie)

You need to be running a 3.8 kernel or above, hence the need for Jessie and the installation of that kernel.

You need to install golang, and specially 1.2 or above else you’ll get ipv6 errors, too. Get it from golang.org

You also need to have lxc and aufs-tools

apt-get install lxc aufs-tools

Then you need to setup your GOPATH environmental variable, create the docker directory in the src directory and clone docker into that.

export GOPATH=/your/go/directory
cd $GOPAth
mkdir -p src/github.com/dotcloud/
cd src/github.com/dotcloud
git clone https://github.com/dotcloud/docker

Then cd into the docker directory to use golang to pull all the dependencies and install docker into your GOPATH’s bin directory

cd docker
go get -v github.com/dotcloud/docker/...
go install -v github.com/dotcloud/docker/...

(The three dots mean do this for all the packages under said directory)

You should be able to run docker via

$GOPATH/bin/docker

Before you can run a docker image you need to mount cgroups. Put this in your /etc/fstab

none        /cgroup        cgroup        defaults    0    0

And then run

mkdir /cgroup && mount -a

You also need to enable ipv4 forwarding else your container won’t be able to talk to the outside world.

sysctl -w net.ipv4.ip_forward=1

Finally you need to run docker as a daemon, and as root, too.

$GOPATH/bin/docker -d &
2013/10/19 23:15:48 WARNING: cgroup mountpoint not found for memory
2013/10/19 23:15:48 Listening for HTTP on /var/run/docker.sock (unix)

Now you can issue a pull request to get the base 120MB ubuntu container from the internet.

$GOPATH/bin/docker pull ubuntu

After it’s downloaded it all, you can now use that container.

$GOPATH/bin/docker run ubuntu echo 'omg'

This should product something like

2013/10/19 23:22:55 POST /v1.6/containers/create
2013/10/19 23:22:55 POST /v1.6/containers/3fe0676d7e00/attach?stderr=1&stdout=1&stream=1
2013/10/19 23:22:55 POST /v1.6/containers/3fe0676d7e00/start
omg
2013/10/19 23:22:55 GET /v1.6/containers/3fe0676d7e00/json
unix unix-debian docker

Page 1 of 2
Next