The Docker Book

Common tasks with docker and docker-compose

How to create a container

$ docker create <image-name>

How to start a container

$ docker start -a <container-id>
$ docker start --attach <container-id>

How to run a container

$ docker run <image-name>
$ docker run <image-name> <alternate-startup-command>
$ docker run --detach <image-name>

How to list all containers

$ docker ps
$ docker ps --all
$ docker ps -a

How to delete all containers

$ docker container prune --force

How to get logs from a running container

$ docker logs <container-id>

How to terminate a container

$ docker stop <container-id>
$ docker kill <container-id>

How to run multiple container commands

$ docker exec -it <container-id> <comand>

How to get shell access to a container

$ docker exec --interactive --tty <container-id> sh
$ docker run --interactive --tty <image-name> sh

How to build a custom image from a Dockerfile

# Define base image
FROM alpine

# Define dependencies
RUN apk add --update redis

# Define start-up command
CMD ["redis-server"]

How to build a tagged image from a container

// (1) startup a container from an alpine image
$ docker run -it alpine sh

// (2) install redis on the container
% apk add --update redis

// (3) in a new shell, create image from modified container 
// with default start-up command 
docker commit -c 'CMD ["redis-server"]' 06bbfbcbc3bf debugme/redis:home

// (4) Start up the image you just created
docker run debugme/redis:home

How to run a custom server in a docker container

# Dependencies least likely to change
FROM node:alpine
WORKDIR /home/mywebapp
EXPOSE 8080
CMD ["npm", "start"]
COPY ./package.json ./
RUN npm install

# Dependencies most likely to change
COPY ./ ./

How to delete all Docker images that have been downloaded

$ docker image prune --all --force

Docker-compose commands

$ docker-compose build        // builds docker container(s)
$ docker-compose up           // starts up docker container(s) in foreground
$ docker-compose up -d        // starts up docker container(s) in background
$ docker-compose down         // brings docker containers down
$ docker-compose ps           // get the status of the running containers

How to run an application composed of multiple docker containers

const express = require("express");
const redis = require("redis");

const rootPath = "/";

const {
  env: { nodePort, redisHost, redisPort }
} = process;

const rootHandler = client => (_, response) => {
  client.get("visits", (error, visits) => {
    if (error) {
      response.send(error);
    } else {
      response.send(`visits: ${visits}`);
      client.set("visits", parseInt(visits, 10) + 1);
    }
  });
};

const callback = port => () =>
  console.log(`Listening on http://localhost:${port}`);

const client = redis.createClient({ host: redisHost, port: redisPort });
client.set("visits", 0);

const server = express();
server.get(rootPath, rootHandler(client));
server.listen(nodePort, callback(nodePort));

How to specify a restart policy for your docker container

const express = require("express");
const redis = require("redis");

const rootPath = "/";

const {
  env: { nodePort, redisHost, redisPort }
} = process;

const rootHandler = client => (_, response) => {
  // process.exit(0)
  process.exit(1)
  client.get("visits", (error, visits) => {
    if (error) {
      response.send(error);
    } else {
      response.send(`visits: ${visits}`);
      client.set("visits", parseInt(visits, 10) + 1);
    }
  });
};

const callback = port => () =>
  console.log(`Listening on http://localhost:${port}`);

const client = redis.createClient({ host: redisHost, port: redisPort });
client.set("visits", 0);

const server = express();
server.get(rootPath, rootHandler(client));
server.listen(nodePort, callback(nodePort));

How to sync local frontend changes with Docker

from node:alpine
workdir /app
cmd [ "npm", "run", "start" ]
copy package.json .
run npm install
copy . .

How to sync local frontend changes with Docker Compose

from node:alpine
workdir /usr/app
cmd ["npm", "start"]
copy package.json .
run npm install
copy . .

How to sync local backend changes with Docker Compose

FROM node:alpine
WORKDIR /usr/app
CMD ["npm", "start"]
COPY package.json .
RUN npm install
COPY . .

How to create a container using a multi-step approach

version: "3"
services: 
  app:
    stdin_open: true
    ports:
      - 3000:3000
    volumes:
      - .:/usr/app
      - /usr/app/node_modules
    build:
      context: .
      dockerfile: Dockerfile.dev

How to deploy code changes to GitHub and test pull requests on TravisCI

version: "3"
services: 
  app:
    ports:
      - 3000:3000
    volumes:
      - .:/usr/docker-react
      - /usr/docker-react/node_modules
    build:
      context: .
      dockerfile: Dockerfile.dev

How to get container id via image name

// In the example below we find the container by looking for "35mm" 
// which is part of the image name and then opening a shell on it

$ docker exec -it `docker ps | grep 35mm | awk '{ print $1}'` sh

Last updated