César D. Velandia

Docker


Getting started

Images are the blueprint to instantiate containers that are built, run, and distributed by the Docker Daemon, which used via a Docker Client and pull images from a registry such as Docker Hub

Basic commands

#runs a container (downloads image if not local)
docker run

#runs and attaches iteractive console (depends on image)
docker run -it ubuntu bash

# run and remove container on exit
docker run --rm ubuntu bash

#fetches an image from the registry, default is dockerhub
docker pull

#lists all images that have been downloaded to this syst
docker images

#list all containers regardless of status
docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
305297d7a235        busybox             "uptime"            11 minutes ago      Exited (0) 11 minutes ago                       distracted_goldstine
ff0a5c3750b9        busybox             "sh"                12 minutes ago      Exited (0) 12 minutes ago                       elated_ramanujan
14e5bd11d164        hello-world         "/hello"            2 minutes ago       Exited (0) 2 minutes ago                        thirsty_euclid

#monitor events as they happen
docker stats

#clean up containters, free space
docker rm thirsty_euclid
docker rm ff0a5c3
## remove all stopped containers

docker rm $(docker ps -a -q -f status=exited)
docker container prune

## remove unused images
docker rmi hello-world

Run, create and host images

Running a webapp image

# flags: detach terminal, publish exposed ports and name container
docker run -d -P --name cheese errm/cheese:cheddar

docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                           NAMES
7d4d2f89a1b8        errm/cheese:cheddar   "nginx -g 'daemon of…"   6 seconds ago       Up 5 seconds        0.0.0.0:32772->80/tcp, 0.0.0.0:32771->443/tcp   cheese


docker port cheese
443/tcp -> 0.0.0.0:32771
80/tcp -> 0.0.0.0:32772

# stop running container by name
docker stop cheese

Creating images

docker images:

  • Repository
  • Tag
  • Image ID

Base (e.g., ubuntu, alpine) and child images (e.g., ansible/ubuntu14.04-ansible)

Official (e.g., busybox, python) and user images (e.g., user/my-image)

Search for images at hub.docker.com or  CLI, e.g., docker search cheese

To create an image from an existing web project, simple add a Dockerfile

# Prepare the image from current dir using Dockerfile
docker build .

# build and name your image using a Dockerfile
docker build -t a-user/image-name .

# run the image(download if needed) and use port 8888 in the host 
docker run -p 8888:5000 a-user/image-name

Dockerfile

a plain file with the instructions to build an image automatically. Commands are linux-like. The steps to create a simple webapp Dockerfile:

  1. Add a base image FROM
  2. Define a working directory and copy files: WORKDIR & COPY
  3. Install requirements and dependencies using RUN
  4. Set up ports that need to be exposed EXPOSE
  5. Run the application CMD
FROM python:3
WORKDIR /www/var
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
CMD ["python", "./app.py"]

Build (and tag) the image where Dockerfile is with

docker build -t user-name/app-name .

Any required images, such as python will be downloaded if not present locally, double check using docker images

Then run the image an serve the app in the host using port 8888

docker run -p 8888:5000 user-name/app-name

AWS Elastic Beanstalk

Publish your image to any available registry (public or private) such as hub.docker.com

docker push user-name/app-name

Using https://console.aws.amazon.com/elasticbeanstalk/ or use the elastic beanstalk CLI

  1. Create a new application
  2. Name and select Docker as platform
  3. Upload you local config file (see below) and hit create
{
  "AWSEBDockerrunVersion": "1",
  "Image": {
    "Name": "user-name/app-name",
    "Update": "true"
  },
  "Ports": [
    {
      "ContainerPort": 5000,
      "HostPort": 8000
    }
  ],
  "Logging": "/var/log/nginx"
}

Tools

wagoodman/dive
A tool for exploring each layer in a docker image. Contribute to wagoodman/dive development by creating an account on GitHub.