Skip to content

How to Docker

VBrazhnik edited this page Jul 6, 2018 · 12 revisions

01. Create a virtual machine with docker-machine using the virtualbox driver, and named Char.

Answer

docker-machine create --driver virtualbox Char

Explanation

docker-machine create | Docker Documentation:

Create a machine. Requires the --driver flag to indicate which provider (VirtualBox, DigitalOcean, AWS, etc.) the machine should be created on, and an argument to indicate the name of the created machine.

Example

Here is an example of using the --virtualbox driver to create a machine called dev.

$ docker-machine create --driver virtualbox dev

02. Get the IP address of the Char virtual machine.

Answer

docker-machine ip Char

Explanation

docker-machine ip | Docker Documentation:

Get the IP address of one or more machines.

$ docker-machine ip dev
192.168.99.104

$ docker-machine ip dev dev2
192.168.99.104
192.168.99.105

03. Define the variables needed by your virtual machine Char in the general env of your terminal, so that you can run the docker ps command without errors. You have to fix all four environment variables with one command, and you are not allowed to use your shell’s builtin to set these variables by hand.

Answer

eval $(docker-machine env Char)

Explanation

docker-machine env Char command output:

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/vbrazhni/.docker/machine/machines/Char"
export DOCKER_MACHINE_NAME="Char"
# Run this command to configure your shell: 
# eval $(docker-machine env Char)

04. Get the hello-world container from the Docker Hub, where it’s available.

Answer

docker pull hello-world

Explanation

Getting an image from Docker Hub

Docker Hub is the place where open Docker images are stored. When we ran our first image by typing

docker run --rm -p 8787:8787 rocker/verse

the software first checked if this image is available on your computer and since it wasn’t it downloaded the image from Docker Hub. So getting an image from Docker Hub works sort of automatically. If you just want to pull the image but not run it, you can also do

docker pull rocker/verse

05. Launch the hello-world container, and make sure that it prints its welcome message, then leaves it.

Answer

docker run hello-world

06. Launch an nginx container, available on Docker Hub, as a background task. It should be named overlord, be able to restart on its own, and have its 80 port attached to the 5000 port of Char. You can check that your container functions properly by visiting http://<ip-de-char>:5000 on your web browser.

Answer

docker run -d -p 5000:80 --name overlord --restart=always nginx

Explanation

docker run --help:

-d, --detach — Run container in background and print container ID.

--name string — Assign a name to the container.

-p, --publish list — Publish a container's port(s) to the host.

Docker workshop:

-p is a ports mapping <HOST PORT>:<CONTAINER PORT>.

docker run | Docker Documentation:

Restart policies (--restart)

Use Docker’s --restart to specify a container’s restart policy. A restart policy controls whether the Docker daemon restarts a container after exit. Docker supports the following restart policies:

Policy Result
no Do not automatically restart the container when it exits. This is the default.
on-failure[:max-retries] Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts.
unless-stopped Restart the container unless it is explicitly stopped or Docker itself is stopped or restarted.
always Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.

07. Get the internal IP address of the overlord container without starting its shell and in one command.

Answer

docker inspect -f '{{.NetworkSettings.IPAddress}}' overlord

Explanation

How to retrieve Docker container's internal IP address:

It is also possible to trip the default docker inspect docker command's output to get the IP address value only:

# docker inspect -f '{{ .NetworkSettings.IPAddress }}' e350390fd549
172.17.0.2

docker inspect --help:

-f, --format string — Format the output using the given Go template.

08. Launch a shell from an alpine container, and make sure that you can interact directly with the container via your terminal, and that the container deletes itself once the shell’s execution is done.

docker run -it --rm alpine /bin/sh

Explanation

docker run --help:

-i, --interactive — Keep STDIN open even if not attached.

-t, --tty — Allocate a pseudo-TTY.

--rm — Automatically remove the container when it exits.

  1. From the shell of a debian container, install via the container’s package manager everything you need to compile C source code and push it onto a git repo (of course, make sure before that the package manager and the packages already in the container are updated). For this exercise, you should only specify the commands to be run directly in the container.

Answer

apt-get update
apt-get upgrade
apt-get install -y build-essential
apt-get install -y git-core

10. Create a volume named hatchery.

Answer

docker volume create --name hatchery

Explanation

docker volume create | Docker Documentation:

--name — Specify volume name.

11. List all the Docker volumes created on the machine. Remember. VOLUMES.

Answer

docker volume ls

Explanation

docker volume --help:

ls — List volumes.

12. Launch a mysql container as a background task. It should be able to restart on its own in case of error, and the root password of the database should be Kerrigan. You will also make sure that the database is stored in the hatchery volume, that the container directly creates a database named zerglings, and that the container itself is named spawning-pool.

Answer

docker run -d --name spawning-pool --restart=on-failure:21 -e MYSQL_ROOT_PASSWORD=Kerrigan -e MYSQL_DATABASE=zerglings -v hatchery:/var/lib/mysql mysql

Explanation

docker run --help:

-e, --env list — Set environment variables.

Docker MySQL Persistence (Tech Tip #83):

/var/lib/mysql is the default directory where MySQL container writes its files.

Clone this wiki locally