Making A Docker Service Cookbook

Checkout Docker In Hindsight

Installation

see installing docker-ce on Ubuntu , installing on Mac OSX or the general docker installation page

Commands

Containers

List running containers

Multiple docker containers can run on a single docker-machine by the docker engine. A container can be thought of as an single application running with all its dependencies provided to it.

$ docker ps [-a]

Start the container for the first time

The first time a container is started, we have to provide any mappings of external tcp/upd ports to the container’s ports as well as specifying any volumes that need to be mounted on the container.

$ docker run -d \
        -p 0.0.0.0:{external-tcp/udp-port1}:{container-tcp/udp-port1} \
        -p 0.0.0.0:{external-tcp/udp-port2}:{container-tcp/udp-port2} \
        ... \
        -p 0.0.0.0:{external-tcp/udp-portN}:{container-tcp/udp-portN} \
         \
        -v "{host-path1}:{container-path1}" \
        -v "{host-path2}:{container-path2}" \
        ... \
        -v "{host-pathN}:{container-pathN}" \
         \
        --name ${name-of-container] \
        {docker-image-name} 

The below starts a version of tomcat in the background

$ docker run -d --name ${name-of-container] tomcat:8 # run docker as a background daemon

This starts bash on centos and mounts a user’s home folder on the exported mount point.

$ docker run -it -v /home/${some-user}/Projects:/exported/ centos:latest bash

Start a container that exists

$ docker start ${name-of-container}

Start an empty container as a sandbox

if you just want to test out some commands in a shell, you can do the following to start a bash shell as the main application

$ docker run -it bash # run interactive shell as main process on alpine

if you want to run bash against ubuntu,

$ docker run -it ubuntu:16.04 bash

Stop a container that exists

$ docker stop ${name-of-container}

Remove a container

$ docker rm ${container-name}

Attach a shell to a running container

$ docker exec [-it] ${container-id} bash # run interactive shell as another process

View the status of a container

$ docker stats --no-stream ${container-name}

View the top running processes in a container

$ docker top ${container-name}

Images

View existing images

$ docker images # to see the images

Build an image given a docker file

$ docker build -t # to create a new image from a docker file

Tag an image

$ docker tag ${existing-image-id} ${new-tag-image-id} # creates an "alias" for the image

Delete an image

$ docker rmi ${image-id}

Push an image to a repository

$ docker push ${image-id-or-tag}

Dockerfile

A Dockerfile is used to build docker images. Below is a sample one:

FROM ${existing-image-name}
MAINTAINER my-team-email@mycompany.com
RUN ${command-such-as-apt-get}
RUN ${command-such-as-apt-get}
RUN ${command-such-as-apt-get}

EXPOSE ${port1} ... ${portN}

ENTRYPOINT [ ${application-to-run}]

Using a Docker Image Registry

Pulling an image from the default registry

$ docker pull registry
$ docker run -d -p 5000:5000 registry

Pulling an image from another registry

docker pull {hostname}/{project-id}/{image}[:{tag}]

For example to get elasticsearch/elasticsearch:5.6.3 from the elastic.co site:

docker pull docker.elastic.co/elasticsearch/elasticsearch:5.6.3

Pushing an image to a registry

To push to server my-machine:5000

$ docker tag ${image-id} my-machine:5000/${remote-image-id}
$ docker push my-machine:5000/${remote-image-id}
$ docker pull my-machine:5000/${remote-image-id}

Docker Machine Management

Create a machine

create a machine that will run a docker engine and host docker containers.

$ docker-machine create --driver virtualbox [${machine-name}]
# to connect to an existing docker engine running on a different host (say 192.168.0.5)
docker-machine create --driver none --url=tcp://192.168.0.5:2376 myremotedocker
# or to create your own engine on a mac or windows box
docker-machine create --driver virtualbox default
# or to create a new engine in the amzaon cloud:
docker-machine create --driver amazonec2 --amazonec2-access-key AKI******* --amazonec2-secret-key 8T93C******* myamazon

List all docker machines

$ docker-machine ls

List URL

$ docker-machine url [${machine-name}]

Delete a machine

$ docker-machine rm --driver virtualbox [${machine-name}]

Start/Stop

To set up environment variables so that the docker command will talk to the correct machine:

$ eval $(docker-machine env default)

To start

$ docker-machine start [${machine-name}]
$ docker-machine restart [${machine-name}]

To stop

$ docker-machine stop [${machine-name}]
$ docker-machine kill [${machine-name}]

To remote into

$ docker-machine ssh [${machine-name}]
$ docker-machine ssh [${machine-name}] -l ${remote-port}:localhost:${localhost-portmapping}

e.g. docker-machine ssh default :9020:localhost:9020:

To copy

$ docker-machine scp ...

e.g. docker-machine scp default:/home/docker/foo.txt .