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.

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 Java 9 Repl

Try out new Java 9 stuff.

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

5 React demo

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

6 Get a official container

Pull a Docker Node container from Dockerhub:

docker pull node:latest

7 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

8 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 .

9 Show containers

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

10 Dockerfile

Specifies a container environment.

11 Unitime Docker

A Dockerfile is a file that can build an environment for us.

FROM python:3.6
RUN git clone \ unitime
WORKDIR /app/unitime
RUN mkdir logs
RUN pip install -r requirements.txt
RUN python unitime/ makemigrations
RUN python unitime/ migrate
ENTRYPOINT ["python"]
CMD ["unitime/", "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-09-06 Wed 22:52