Skip to content

How to Docker

VBrazhnik edited this page Jul 14, 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

How to check result?

docker-machine:

ls — List machines.

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)

How to check result?

docker-machine ls command output:

NAME   ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
Char   *        virtualbox   Running   tcp://192.168.99.100:2376           v18.05.0-ce   

Now the ACTIVE column of Char has * instead of -.

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

How to check result?

docker image:

ls — List images.

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.

Starting a Shell in the Docker Alpine Container:

Usually, an Alpine Linux image doesn't contain bash, Instead you can use /bin/ash, /bin/sh, ash or only sh.

9. 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 -y
apt-get upgrade -y
apt-get install -y build-essential
apt-get install -y git

Explanation

Start Debian container:

docker run -ti --rm debian

InstallingCompilers:

build-essential contains a list of packages which are essential for building Ubuntu packages including gcc compiler, make and other required tools.

Download for Linux and Unix | Git:

Debian/Ubuntu

For the latest stable version for your release of Debian/Ubuntu

# apt-get install git

10. Create a volume named hatchery.

Answer

docker volume create --name hatchery

Explanation

docker volume create | Docker Documentation:

--name — Specify volume name.

How to check result?

docker volume:

ls — List volumes.

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

Answer

docker volume ls

Explanation

docker volume:

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:10 -e MYSQL_ROOT_PASSWORD=Kerrigan -e MYSQL_DATABASE=zerglings -v hatchery:/var/lib/mysql mysql --default-authentication-plugin=mysql_native_password

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.

mysql | Docker Documentation:

MYSQL_ROOT_PASSWORD

This variable is mandatory and specifies the password that will be set for the MySQL root superuser account.

MYSQL_DATABASE

This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access (corresponding to GRANT ALL) to this database.

Wordpress latest does not works with mysql latest container:

MySQL 8 changed the password authentication method. You're looking for the mysql_native_password plugin https://dev.mysql.com/doc/refman/8.0/en/native-pluggable-authentication.html

Or you could use mysql 5.7

Alternative answer

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

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

docker run -d --name lair -p 8080:80 --link spawning-pool:mysql wordpress

Explanation

Communication across links

Links allow containers to discover each other and securely transfer information about one container to another container. When you set up a link, you create a conduit between a source container and a recipient container. The recipient can then access select data about the source. To create a link, you use the --link flag.

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

docker run --name roach-warden -d --link spawning-pool:db -p 8081:80 phpmyadmin/phpmyadmin

16. Look up the spawning-pool container’s logs in real time without running its shell.

Answer

docker logs spawning-pool

17. Display all the currently active containers on the Char virtual machine.

Answer

docker ps

18. Relaunch the overlord container.

Answer

docker restart overlord

19. Launch a container name Abathur. It will be a Python container, 2-slim version, its /root folder will be bound to a HOME folder on your host, and its 3000 port will be bound to the 3000 port of your virtual machine. You will personalize this container so that you can use the Flask micro-framework in its latest version. You will make sure that an html page displaying "Hello World" with <h1> tags can be served by Flask. You will test that your container is properly set up by accessing, via curl or a web browser, the IP address of your virtual machine on the 3000 port. You will also list all the necessary commands in your repository.

20. Create a local swarm, the Char virtual machine should be its manager.

docker swarm init --advertise-addr 192.168.99.100

21. Create another virtual machine with docker-machine using the virtualbox driver, and name it Aiur.

docker-machine create --driver virtualbox Aiur

22. Turn Aiur into a slave of the local swarm in which Char is leader (the command to take control of Aiur is not requested).

Answer

docker swarm join --token SWMTKN-1-4pot2tnaoqpd2aa2v6p2lsfc05r4419crclo98pfhq1a8cmj4a-4iztp020l8ts12f4rcl3rscnf 192.168.99.100:2377

Join nodes to a swarm | Docker Documentation

23. Create an overlay-type internal network that you will name overmind.

docker network create -d overlay overmind

24. Launch a rabbitmq SERVICE that will be named orbital-command. You should define a specific user and password for the RabbitMQ service, they can be whatever you want. This service will be on the overmind network.

docker service create -d --network overmind --name orbital-command -e RABBITMQ_DEFAULT_USER=root -e RABBITMQ_DEFAULT_PASS=root rabbitmq

25. List all the services of the local swarm.

docker service ls

26. Launch a 42school/engineering-bay service in two replicas and make sure that the service works properly (see the documentation provided at hub.docker.com). This service will be named engineering-bay and will be on the overmind network.

docker service create -d --network overmind --name engineering-bay --replicas 2 -e OC_USERNAME=orbital-command -e OC_PASSWD=orbital-command 42school/engineering-bay

27. Get the real-time logs of one the tasks of the engineering-bay service.

28. ... Damn it, a group of zergs is attacking orbital-command, and shutting down the engineering-bay service won’t help at all... You must send a troup of Marines to eliminate the intruders. Launch a 42school/marine-squad in two replicas, and make sure that the service works properly (see the documentation provided at hub.docker.com). This service will be named... marines and will be on the overmind network.

docker service create -d --network overmind --name marines --replicas 2 -e OC_USERNAME=orbital-command -e OC_PASSWD=orbital-command 42school/marine-squad

29. Display all the tasks of the marines service.

docker service ps marines

30. Increase the number of copies of the marines service up to twenty, because there’s never enough Marines to eliminate Zergs. (Remember to take a look at the tasks and logs of the service, you’ll see, it’s fun.)

docker service scale marines=20

31. Force quit and delete all the services on the local swarm, in one command.

docker service rm $(docker service ls -q)

32. Force quit and delete all the containers (whatever their status), in one command.

docker rm $(docker ps -a -q)

33. Delete all the container images stored on the Char virtual machine, in one command as well.

docker rmi $(docker images -a -q)

34. Delete the Aiur virtual machine without using rm -rf.

Clone this wiki locally