-
Notifications
You must be signed in to change notification settings - Fork 4
How to Docker
Answer
docker-machine create --driver virtualbox Char
Explanation
docker-machine create | Docker Documentation:
Create a machine. Requires the
--driverflag 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.
Here is an example of using the
--virtualboxdriver to create a machine called dev.$ docker-machine create --driver virtualbox dev
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)
Answer
docker pull hello-world
Explanation
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/versethe 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.
-pis a ports mapping<HOST PORT>:<CONTAINER PORT>.
docker run | Docker Documentation:
Use Docker’s
--restartto 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 noDo 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-stoppedRestart the container unless it is explicitly stopped or Docker itself is stopped or restarted. alwaysAlways 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.
- 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
Answer
docker volume create --name hatchery
Explanation
docker volume create | Docker Documentation:
--name— Specify volume name.
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/mysqlis the default directory where MySQL container writes its files.
13. Print the environment variables of the spawning-pool container in one command, to be sure that you have configured your container properly.
Answer
docker inspect -f '{{.Config.Env}}' spawning-pool
14. Launch a wordpress container as a background task, just for fun. The container should be named lair, its 80 port should be bound to the 8080 port of the virtual machine, and it should be able to use the spawning-pool container as a database service. You can try to access lair on your machine via a web browser, with the IP address of the virtual machine as a URL. Congratulations, you just deployed a functional Wordpress website in two commands!
Answer
15. Launch a phpmyadmin container as a background task. It should be named roach-warden, its 80 port should be bound to the 8081 port of the virtual machine and it should be able to explore the database stored in the spawning-pool container.
Answer