Docker usage notes – continued (2)

A few rough usage notes, continuing from my first post a while back.

Delete all containers:

Run a container from an image and delete it when it exits: use –rm:

docker run -it --rm containerid

 

Pass an argument into a build:

  • use ‘ARG argname’ to declare the argument in your Dockerfile
  • pass value for argname with –build-arg argname=value

Test during build if an arg was passed:

RUN test -n "$argname"

 

Building ARM Docker images on the Raspberry Pi

Install Docker for ARM using the install script:

curl -sSL https://get.docker.com | sh

From: https://www.raspberrypi.org/blog/docker-comes-to-raspberry-pi/

Set to startup as a service:

sudo systemctl enable docker

Start the service manually now (or reboot to start automatically):

sudo systemctl start docker

Add user to docker group (to run docker cli without sudo):

sudo usermod -aG docker pi

 

To create a new image from a Raspbian base for ARM, use the Raspbian images from resin (in your Dockerfile):

FROM resin/rpi-raspbian:latest

From: http://blog.alexellis.io/getting-started-with-docker-on-raspberry-pi/

Edit your Dockerfile to include and configure whatever you need, and build an image as normal on the Pi using:

docker build -t tagname .

… and off you go.

Running a React app in a Docker container

I started converting my AngularJS AddressBook app into React. Since this is part of a larger project to run all the parts of my app in Docker containers with Docker Compose, I needed to look at how I can run my React app in a container.

For development, using the webpack dev server works well locally. Running this from within a container though requires doing an ‘npm install’ and a ‘npm run build’ inside the container, which probably doesn’t make sense to run every time, as with all the npm dependencies from create-react-app this take some time to run.

The other option is to build the app locally and then just build a container with the webpack output, and serve it with an HTTP server like nginx.

Let’s take a look at each of these options.

The easiest and simplest approach is to build the app locally first, as you normally would:

npm install
npm run build

A Dockerfile to build the container using the output from ‘npm run build’ then looks like (using an nginx image as a starting point):

FROM nginx
COPY build /usr/share/nginx/html

You build this with:

docker build -t addressbook-react-web-nginx .

And then run with:

docker run -it -p 80:80 addressbook-react-web-nginx

Next up, for a comparison, the Dockerfile to push everything into the image and run with ‘npm run start’ inside the container would look like:

FROM node:6.10-alpine
RUN mkdir -p /home/web
COPY *.html /home/web/
COPY public /home/web/public/
COPY src /home/web/src/
COPY *.json /home/web/
WORKDIR "/home/web"
RUN npm install
ENTRYPOINT npm run start

This is starting from one of the official node images. It copies in all the source files, runs npm install and then sets the entrypoint for when the container starts to run the webpack dev server.

An improvement on this approach could be to build an intermediate container with the package.json and npm install already run, and then build new images from this incrementally adding just the changed source files. Plenty of options to explore depending on what you need to do.

Next up, modify my previous docker compose file to include one of these new images containing my React app and now I’ve got all the pieces ready to go.

Converting my AngularJS AddressBook app to React

Several months back I spent some time looking at Docker and Docker Compose, and put together a sample AngularJS web app served by Nginx in one container, against a Spring Boot JAX-RS RESTful backend in another container, and using MongoDB in another container.

I wrote a couple of articles (part 1, and part 2) describing the Docker containers and how they were configured together using Docker Compose, but I didn’t spend much time talking about the web app itself. I was intending at the time that I’d develop the frontend app using a number of frameworks as a comparison. The AngularJS AddressBook app is functional (on GitHub here), I got part way converting it to an Angular 2 based app (on GitHub here, although clearly I was unsure at the time thinking Angular 2 was called AngularJS2), but the React app I made a rough start at but didn’t get very far.

Given most recently I’ve been spending some time getting up to speed with some React, I’m going to pick up this app again and complete it, so I’ll have an interesting side by side comparison of the same app developed with all three frameworks. More updates to come.