Docker

A quick summary of Docker

Docker how-to

A virtualisation tool at the per-app level. Encapsulate all your necessary libraries, dependencies etc, expose a port and run!

Jargon: A container is an instance of an image. You can run many containers of the same image (such as to load balance)

Running images

Create a new container/instance

Will create a new instance based on the image. If you have already created an instance before (and you aren't intentionally trying to run multiple ie: to load balance), use docker start

docker run -d --name <name_of_instance> -p <external_port>:<internal_port> <author/image>

  • -d = run as a daemon (ie: in the background)
  • [name_of_instance] = the name you wish to assign to the image
  • external_port = what port your host machine will use to access the application
  • internal_port = what port your application has been written to accept requests on
  • author - the dockerhub account you are getting a project image from
  • image - the dockerhub image to fetch

eg:

docker run -d --name helloworld -p 8080:80 tutum/hello-world
docker run python:3.6

Starting an existing container/instance

docker start <name_of_instance>

Stopping an container/instance

docker stop <name_of_instance>

View running container/instance

docker ps

View all containers/instances (including stopped instances)

docker ps -a

View all images

docker images

Delete a container/instance

docker rm <name>

Delete an image

docker rmi <image>

Login to DockerHub (or other repo)

docker login

Creating an image

Install Docker on your development machine.
https://docs.docker.com/machine/

Example Dockerfile

FROM nginx

RUN mkdir /etc/nginx/logs && touch /etc/nginx/logs/staticlog

ADD ./nginx.conf /etc/nginx/conf.d/defaults.conf
ADD /src /www
EXPOSE 80

ADD will copy from your local project folder (where you are creating the Dockerfile), into the docker image location

An example Dockerfile that will create a python image

# Use an official Python runtime as a parent image
FROM python:3.6

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

requirements.txt ... a list of the packages to install with pip, eg:

Flask

Build the image file

docker build -t <project> .
docker build -t <project>:<tag> .

The dot is the path to my Dockerfile

The tag is optional, think of it as the version number of your project.

Verify it has been built with:
docker image ls

Run it
docker run -d --name <project_instance> -p 8080:80 <project>

Stop it
docker stop <project_instance>

Login to cloud.docker.com

docker login

Associate the local image with a repository on a registry

docker tag <image> <username>/<repository>:<tag>

Deploy to the cloud

docker push <author>/<project>:<tag>
docker push <author>/<project>:<tag>

Troubleshooting

Behind a proxy server?

DNS settings

Proxy servers can block connections to your web app once it’s up and running. If you are behind a proxy server, add the following lines to your Dockerfile, using the ENV command to specify the host and port for your proxy servers:

# Set proxy server, replace host:port with values for your servers
ENV http_proxy host:port
ENV https_proxy host:port

Proxy server settings

DNS misconfigurations can generate problems with pip. You need to set your own DNS server address to make pip work properly. You might want to change the DNS settings of the Docker daemon. You can edit (or create) the configurarion file at /etc/docker/daemon.json with the dns key, as following:

{
  "dns": ["your_dns_address", "8.8.8.8"]
}

In the example above, the first element of the list is the address of your DNS server. The second item is the Google’s DNS which can be used when the first one is not available.

Before proceeding, save daemon.json and restart the docker service.

sudo service docker restart

Once fixed, retry to run the build command.