Docker

Docker is a platform for developers and sysadmins to develop, ship, and run applications. Docker lets you quickly assemble applications from components and eliminates the friction that can come when shipping code. Docker lets you get your code tested and deployed into production as fast as possible.

With Docker, developers can build any app in any language using any toolchain. “Dockerized” apps are completely portable and can run anywhere - colleagues’ macOS and Windows laptops, QA servers running Ubuntu in the cloud, and production data center VMs running Red Hat.

Docker for Mac

Docker for Mac is the current release of Docker for macOS.

Installation

Docker for Mac can be downloaded here.

Prerequisite

You’ll need homebrew-cask to install Docker Toolbox, if you don’t have it refer to this chapter on Cask.

Installation

There are two ways to install Docker

Option 1: These are the steps to install docker using brew

  • Install the docker and docker machine from brew
brew install docker docker-machine
  • Install VirtualBox to let Docker create the images.
brew install --cask virtualbox

If you encounter an issue with the installer with an error message like

The install failed (The installer encountered an error that caused the installation to fail.
Contact the software manufacturer for assistance.)

Use the following When you do fail, turn on System Preference and see if ‘System software from developer “Oracle America, inc” was blocked from loading.’ If you see that message, click Allow button and try to install again.

This should complete the installation


Now to create a Machine, follow the following steps:

docker-machine create --driver virtualbox default

Run the following to tell Docker which machine to execute Docker on

docker-machine env default

Finally, to verify all the installations:

docker run hello-world

You can find more about Docker in the documentation.

Option 2: Install using the Docker App

  • Navigate to the following link
https://hub.docker.com/editions/community/docker-ce-desktop-mac/

This installation should provide you all the necessary GUI tools.

Useful Docker Commands

Here follows a list of useful Docker commands with useful flags for each command.

docker build

Build an image from a Dockerfile.

docker build [DOCKERFILE PATH]

Example

Build an image tagged my-org/my-image where the Dockerfile can be found at /tmp/Dockerfile.

docker build -t my-org:my-image -f /tmp/Dockerfile

Useful flags

  • --file -f Path where to find the Dockerfile
  • --force-rm Always remove intermediate containers
  • --no-cache Do not use cache when building the image
  • --rm Remove intermediate containers after a successful build (this is true) by default
  • --tag -t Name and optionally a tag in the ‘name:tag’ format

docker exec

Execute a command inside a running container.

docker exec [CONTAINER ID]

Example

docker exec [CONTAINER ID] touch /tmp/exec_works

Useful flags

  • --detach -d Detached mode: run command in the background
  • -it This will not make the container you started shut down immediately, as it will create a pseudo-TTY session (-t) and keep STDIN open (-i)

docker images

List all downloaded/created images.

docker images

Useful flags

  • -q Only show numeric IDs

docker inspect

Shows all the info of a container.

docker inspect [CONTAINER ID]

docker logs

Gets logs from container.

docker logs [CONTAINER ID]

Useful flags

  • --details Log extra details
  • --follow -f Follow log output. Do not stop when end of file is reached, but rather wait for additional data to be appended to the input.
  • --timestamps -t Show timestamps

docker ps

Shows information about all running containers.

docker ps

Useful flags

  • --all -a Show all containers (default shows just running)
  • --filter -f Filter output based on conditions provided, docker ps -f="name="example"
  • --quiet -q Only display numeric IDs

docker rmi

Remove one or more images.

docker rmi [IMAGE ID]

Useful flags

  • --force -f Force removal of the image

docker run

Creates and starts a container in one operation. Could be used to execute a single command as well as start a long-running container.

Example:

docker run -it ubuntu:latest /bin/bash

This will start a ubuntu container with the entrypoint /bin/bash. Note that if you do not have the ubuntu image downloaded it will download it before running it.

Useful flags

  • -it This will not make the container you started shut down immediately, as it will create a pseudo-TTY session (-t) and keep STDIN open (-i)
  • --rm Automatically remove the container when it exit. Otherwise it will be stored and visible running docker ps -a.
  • --detach -d Run container in background and print container ID
  • --volume -v Bind mount a volume. Useful for accessing folders on your local disk inside your docker container, like configuration files or storage that should be persisted (database, logs etc.).

Learn More

A list of more useful Docker commands can be found in the docker-cheat-sheet.

Docker Tips and Tricks

A collection of useful tips and tricks for Docker.

Delete all containers

NOTE: This will remove ALL your containers.

docker container prune

OR, if you’re using an older docker client:

docker rm $(docker ps -a -q)

Delete all untagged containers

docker image prune

OR, if you’re using an older docker client:

docker rmi $(docker images | grep '^<none>' | awk '{print $3}')

See all space Docker take up

docker system df

Get IP address of running container

docker inspect [CONTAINER ID] | grep -wm1 IPAddress | cut -d '"' -f 4

Kill all running containers

docker kill $(docker ps -q)

Licenses and Attributions


Speak Your Mind