-
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.
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
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 --default-authentication-plugin=mysql_native_password
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.
MYSQL_ROOT_PASSWORDThis variable is mandatory and specifies the password that will be set for the MySQL root superuser account.
MYSQL_DATABASEThis 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:21 -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
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
Answer
docker logs spawning-pool
Answer
docker ps
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.
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
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
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
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
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
docker service rm $(docker service ls -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -a -q)