Introduction to Docker

1 What is Docker

“Docker containers wrap up a piece of software in a complete filesystem that contains everything it needs to run: code, runtime, system tools, system libraries – anything you can install on a server. This guarantees that it will always run the same, regardless of the environment it is running in.”

  • Open source.
  • Written in Go.
  • Similar to Vagrant.

2 Why?

  • Easy to isolate applications.
  • Easy to test new stuff out.
  • Keep your computer clean.
  • Many hosting providers support Docker containers.

3 Docker Hub

A place to distribute containers.

  • Like GitHub but for Docker containers.
  • Organizations creates official containers for their products.
  • It’s easy to get containers from Docker Hub.

4 React demo

Create a basic React app and run it in a container.

5 Get a official images

Pull a Docker Node image from Dockerhub:

docker pull node:latest

6 Start it

Start the container and attach to it.

We also want a bash shell in the container.

docker run -it --rm -v $PWD:/app \
      --net host node:latest bash

7 Install and run a Node/React app

Create the base app and start it.

npm install -g create-react-app
create-react-app /app
cd /app
npm start

Check permissions on the files. Change if needed.

sudo chown -R $USER:users .

8 Java 9 Repl

Try out new Java 9 stuff.

docker run -it --rm openjdk:9-jdk bash
jshell
System.out.println("Kodkollektivet in a REPL near you!");
java.util.stream.Stream.of(11, 2, 16).sorted().findFirst();

9 Show containers

docker ps     # Shows running containers
docker ps -a  # Show all containers

10 Dockerfile

Specifies a container environment and builds an image.

11 Unitime Docker

A Dockerfile is a file that specifies an environment in a image.

FROM python:3.6
WORKDIR /app
RUN git clone \
https://github.com/Kodkollektivet/unitime-api.git unitime
WORKDIR /app/unitime
RUN mkdir logs
RUN pip install -r requirements.txt
RUN python unitime/manage.py makemigrations
RUN python unitime/manage.py migrate
ENTRYPOINT ["python"]
CMD ["unitime/manage.py", "runserver"]

Save it as: /tmp/unitime-docker/Dockerfile

12 Build the Dockerfile.

Navigate to: /tmp/unitime-docker

docker build -t unitime-docker .

13 Run interactive

Running it with an interactive tty.

docker run -it --rm --net host unitime-docker

14 Run in daemon mode

Running container in daemon mode.

docker run -d --rm --net host unitime-docker

15 Attach a bash to the container

Attach a bash shell to the running daemon container.

docker exec -it <CONTAINER ID> bash

16 Attach to a tty on the container

Deattach with Ctrl + c.

docker attach --sig-proxy=false <CONTAINER ID>

17 Stop a daemon container

Stop a container that’s in daemon mode.

docker stop <CONTAINER ID>

18 Delete containers and images.

docker rm -f `docker ps -a -q` # Remove containers
docker rmi `docker images -q`  # Remove images

19 More

  • Docker compose

20 Resources

Date: 2017-02-21 Tue

Author: John Herrlin

Created: 2017-02-23 Thu 17:59

Emacs 25.1.1 (Org mode 8.2.10)

Validate