Sunday, April 5, 2015

Useful Docker Commands for Your Workflow

Prerequisites: Read this primer on Docker containers. You'll also need to install Virtualbox and boot2docker. This guide was written for Mac Users.

Standard Commands

You might as well memorize these commands as you'll be using them often.

# initialize and start
boot2docker init
boot2docker up

# overview of docker, containers and images
docker info
docker version
docker ps
docker images

# fetch the boot2docker VM's ip address
boot2docker ip

Leveraging the Docker Registry

Navigate over to the Docker Registry and get familiar with the official containers. A lot of the base images (ubuntu, nginx, nodejs, etc.), you'll be able to use as is. Most of the containers I build to serve frontend web applications are based off of the nginx base image, for example.

# to simply pull down and run a container
docker run ubuntu:14.04

# to pull, run and execute a command
# this creates the image, sets up a full sandbox...
# ...execution environment, runs the command, then exits
docker run ubuntu:14.04 echo 'hello world'

# to run in interactive mode
docker run -it ubuntu:14.04 bash
# then perform any command like creating a folder

# to get container info
docker ps -a

# to diff
docker diff <CONTAINER ID>

# to delete
docker rm <CONTAINER ID>

# run daemon in the background
docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; date; sleep 1; done"

# attach
docker attach <CONTAINER ID>

# start / stop daemon
docker stop <CONTAINER ID>
docker start <CONTAINER ID>

# to view logs
docker logs <CONTAINER ID>

Basic Workflow for Running an Nginx Container

Now, let's try a more hands on approach to learning about how to interact with an Nginx container.

docker run -d -P --name webserver nginx

Unable to find image 'nginx:latest' locally
511136ea3c5a: Pull complete
[...]
nginx:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Status: Downloaded newer image for nginx:latest
28775342759e8fa00d276e3d667b57341a0ab90cb77ba880e99ec2ebcc59bce1

So let me explain some of the arguments we've passed to the docker command.

run Ensures the container is running
-d Run as a background daemon
-P Exposes the ports EXPOSE'd by the Dockerfile
--name webserver Name the container for easy reference
nginx Indicate that we're using the nginx base image
# now let's get some info on the container
docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                           NAMES
28775342759e        nginx:latest        "nginx -g 'daemon of   8 minutes ago       Up 8 minutes        0.0.0.0:49153->443/tcp, 0.0.0.0:49154->80/tcp   webserver

# now let's try to send a request to the container
curl localhost:49154
curl: (7) Failed connect to localhost:49153; Connection refused

Now, why didn't that work? The boot2docker ip is a very important command because you have to remember that the docker containers are running inside the boot2docker VM, so any port mapping is between the container and that VM, on the Mac OSX host. So if you were to try to hit those IPs directly, you'd get the following output:

Instead, you'll want to hit the VMs IP instead, like so:

curl <BOOT2DOCKERIP>:49154

Make your life even easier by adding this IP address to your /etc/hosts file. See this guide if you've never done that before. Also, feel free to stop and remove the container if you no longer need it with docker stop webserver and docker rm webserver. If for any reason, you didn't name your container, you can run these commands against the CONTAINER ID which in our case was 28775342759e.

SSH Access to Containers

Sometimes, you'll want to SSH into a running container and perform some commands. Read on.

Docker exec

docker exec -it CONTAINER_NAME /bin/bash

nsenter

boot2docker ssh

docker run --rm -v /var/lib/boot2docker:/target jpetazzo/nsenter

echo 'export PATH=/var/lib/boot2docker:$PATH' >> ~/.profile
source ~/.profile

which nsenter
# /var/lib/boot2docker/nsenter

PID=$(docker inspect --format '{{ .State.Pid }}' <CONTAINER_NAME>)

sudo nsenter -m -u -n -i -p -t $PID
# or
sudo docker-enter <CONTAINER_NAME>

# now feel free to run any commands
hostname

# run exit when you're done
exit

Pushing to the docker index

docker commit -m "changes" <CONTAINER ID> <username/image>
docker push > <username/image>

Mounting Volumes and Persistence

Add content here.

# make a mountable directory on the host
boot2docker ssh
sudo mkdir -p /data
sudo chgrp staff -R /data
sudo chmod 775 -R /data
exit

# build the image
docker build -t <CONTAINERNAME> .

docker run -d -P --name <CONTAINERNAME> -v /data:/data -t <CONTAINERNAME>

Other Commands

#!/bin/bash
# Delete all containers
docker rm $(docker ps -a -q)
# Delete all images
docker rmi $(docker images -q)

No comments:

Post a Comment