Introduction To Docker Light
Introduction To Docker Light
Conclusion ....................................................................................... 63
Other eBooks ............................................................................... 64
About the book
4
About the author
5
Sponsors
Materialize
DigitalOcean
If you are new to DigitalOcean, you can get a free $100 credit and spin
up your own servers via this referral link here:
6
DevDojo
The DevDojo is a resource to learn all things web development and web
design. Learn on your lunch break or wake up and enjoy a cup of coffee
with us to learn something new.
Join this developer community, and we can all learn together, build
together, and grow together.
Join DevDojo
7
Ebook PDF Generation Tool
8
Book Cover
9
License
MIT License
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
10
Introduction to Docker
It is more likely than not that Docker and containers are going to be
part of your IT career in one way or another.
After reading this eBook, you will have a good understanding of the
following:
What is Docker
What are containers
What are Docker Images
What is Docker Hub
How to installing Docker
How to work with Docker containers
How to work with Docker images
What is a Dockerfile
How to deploy a Dockerized app
Docker networking
What is Docker Swarm
How to deploy and manage a Docker Swarm Cluster
To make things even better you can use my referral link to get a free
$100 credit that you could use to deploy your virtual machines and test
the guide yourself on a few DigitalOcean servers:
Once you have your account here's how to deploy your first
11
Droplet/server:
https://www.digitalocean.com/docs/droplets/how-to/create/
I'll be using Ubuntu 21.04 so I would recommend that you stick to the
same so you could follow along.
However you can run Docker on almost any operating system including
Linux, Windows, Mac, BSD and etc.
12
What is a container?
13
What is a Docker image?
14
What is Docker Hub?
https://hub.docker.com
You can sign up for a free account. That way you could push your
Docker images from your local machine to DockerHub.
15
Installing Docker
Nowadays you can run Docker on Windows, Mac and of course Linux. I
will only be going through the Docker installation for Linux as this is my
operating system of choice.
Once your server is up and running, SSH to the Droplet and follow
along!
If you are not sure how to SSH, you can follow the steps here:
https://www.digitalocean.com/docs/droplets/how-to/connect-with-ssh/
The installation is really straight forward, you could just run the
following command, it should work on all major Linux distros:
After that, set up Docker so that you could run it as a non-root user with
the following command:
16
To test Docker run the following:
docker version
To get some more information about your Docker Engine, you can run
the following command:
docker info
With the docker info command, we can see how many running
containers that we've got and some server information.
The output that you would get from the docker version command
should look something like this:
https://docs.docker.com/docker-for-windows/install/
And:
https://docs.docker.com/docker-for-mac/install/
That is pretty much it! Now you have Docker running on your machine!
17
Working with Docker
containers
Once you have your Ubuntu Droplet ready, ssh to the server and
follow along!
So let's run our first Docker container! To do that you just need to run
the following command:
18
We just ran a container based on the hello-world Docker Image, as
we did not have the image locally, docker pulled the image from the
DockerHub and then used that image to run the container. All that
happened was: the container ran, printed some text on the screen
and then exited.
Then to see some information about the running and the stopped
containers run:
docker ps -a
You will see the following information for your hello-world container
that you just ran:
19
root@docker:~# docker ps -a
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS
NAMES
62d360207d08 hello-world "/hello" 5
minutes ago Exited (0) 5 minutes ago
focused_cartwright
In order to list the locally available Docker images on your host run the
following command:
docker images
20
Pulling an image from Docker Hub
First, we can pull the image from the docker hub with the docker pull
command:
Then we can get the image ID with the docker images command:
docker images
21
Note, you do not necessarily need to pull the image, this is just for
demo purposes. When running the docker run command, if the image
is not available locally, it will automatically be pulled from Docker Hub.
After that we can use the docker run command to spin up a new
container:
-p 80:80: this means that the traffic from the host on port 80
would be forwarded to the container. That way you could access
the Apache instance which is running inside your docker container
directly via your browser.
With the docker info command now we can see that we have 1 running
container.
22
root@docker:~# docker ps
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS
NAMES
7dd1d512b50e fd4f7e58ef4b "/entrypoint supervi…"
About a minute ago Up About a minute 443/tcp,
0.0.0.0:80->80/tcp, 9000/tcp pedantic_murdock
23
Stopping and restarting a Docker Container
Then you can stop the running container with the docker stop command
followed by the container ID:
24
Accessing a running container
If you need to attach to the container and run some commands inside
the container use the docker exec command:
That way you will get to a bash shell in the container and execute
some commands inside the container itself.
Then, to detach from the interactive shell, press CTRL+PQ. That way you
will not stop the container but just detach it from the interactive shell.
25
Deleting a container
To delete the container, first make sure that the container is not
running and then run:
docker rm CONTAINER_ID
If you would like to delete the container and the image all together, just
run:
With that you now know how to pull Docker images from the Docker
Hub, run, stop, start and even attach to Docker containers!
26
What are Docker Images
27
Working with Docker images
The docker run command downloads and runs images at the same
time. But we could also only download images if we wanted to with the
docker pull command. For example:
Or if you want to get a specific version you could also do that with:
Then to list all of your images use the docker images command:
docker images
For example, here's a link to the Ubuntu image that we've just
downloaded:
28
https://hub.docker.com/_/ubuntu
As Ubuntu 14.04 is really outdated, to delete the image use the docker
rmi command:
29
Modifying images ad-hoc
Install whatever packages needed then exit the container just press
CTRL+P+Q.
docker images ls
30
As you would notice your newly created image would not have a name
nor a tag, so in order to tag your image run:
Now if you list your images you would see the following output:
31
Pushing images to Docker Hub
Now that we have our new image locally, let's see how we could push
that new image to DockerHub.
For that you would need a Docker Hub account first. Then once you
have your account ready, in order to authenticate, run the following
command:
docker login
32
After that you should be able to see your docker image in your docker
hub account, in my case it would be here:
https://cloud.docker.com/repository/docker/bobbyiliev/php-apache
33
34
Modifying images with Dockerfile
We will go the Dockerfile a bit more in depth in the next blog post, for
this demo we will only use a simple Dockerfile just as an example:
FROM alpine
RUN apk update
All that this Dockerfile does is to update the base Alpine image.
Then you could again list your image and push the new image to the
Docker Hub!
35
Docker images Knowledge Check
Once you've read this post, make sure to test your knowledge with this
Docker Images Quiz:
https://quizapi.io/predefined-quizzes/common-docker-images-questions
Now that you know how to pull, modify, and push Docker images, we
are ready to learn more about the Dockerfile and how to use it!
36
What is a Dockerfile
https://docs.docker.com/engine/reference/builder/
37
Dockerfile example
FROM webdevops/php-apache-dev
MAINTAINER Bobby I.
COPY . /var/www/html
WORKDIR /var/www/html
EXPOSE 8080
38
Here is a quick rundown of the Dockerfile:
39
start
EXPOSE: Specify a port that you would like to access the container
on
40
Docker build
After the built is complete you can list your images with the docker
images command and also run it:
And again just like we did in the last step, we can go ahead and publish
our image:
docker login
Then you will be able to see your new image in your Docker Hub
account (https://hub.docker.com) you can pull from the hub directly:
For more information on the docker build make sure to check out the
41
official documentation here:
https://docs.docker.com/engine/reference/commandline/build/
42
Dockerfile Knowledge Check
Once you've read this post, make sure to test your knowledge with this
Dockerfile quiz:
https://quizapi.io/predefined-quizzes/basic-dockerfile-quiz
This is a really basic example, you could go above and beyond with
your Dockerfiles!
Now you know how to write a Dockerfile, how to build a new image from
a Dockerfile using the docker build command!
In the next step we will learn how to set up and work with the Docker
Swarm mode!
43
Docker Network
In order to list the currently available Docker networks you can use the
following command:
As you can see, we have 3 networks available out of the box already
with 3 of the network drivers that we've discussed above.
44
Creating a Docker network
To create a new Docker network with the default bridge driver you can
run the following command:
The above command would create a new network with the name of
myNewNetwork.
If you want to create a Docker network with a specific range, you can
do that by adding the --subnet= flag followed by the subnet that you
want to use.
45
Inspecting a Docker network
In order to get some information for an existing Docker network like the
driver that is being used, the subnet, the containers attached to that
network, you can use the docker network inspect command as
follows:
You can use the docker inspect command to inspect other Docker
objects like containers, images and etc.
46
Attaching containers to a network
To practice what you've just learned, let's create two containers and
add them to a Docker network so that they could communicate with
each other using their container names.
47
docker network inspect myNetwork
For more information about the power of the Docker network, make
sure to check the official documentation here.
48
What is Docker Swarm mode
The manager nodes dispatch tasks to the worker nodes and on the
other side Worker nodes just execute those tasks. For High Availability,
it is recommended to have 3 or 5 manager nodes.
49
Docker Services
In order to have Docker services, you must first have your Docker
swarm and nodes ready.
50
Building a Swarm
Then once you've got that ready, install docker just as we did in the
Introduction to Docker Part 1 and then just follow the steps here:
Step 1
Step 2
Then to get the command that you need to join the rest of the
managers simply run this:
51
docker swarm join-token manager
Note: This would provide you with the exact command that you need to
run on the rest of the swarm manager nodes. Example:
Step 3
To get the command that you need for joining workers just run:
The command for workers would be pretty similar to the command for
join managers but the token would be a bit different.
The output that you would get when joining a manager would look like
this:
52
Step 4
Then once you have your join commands, ssh to the rest of your
nodes and join them as workers and managers accordingly.
53
Managing the cluster
After you've run the join commands on all of your workers and
managers, in order to get some information for your cluster status you
could use these commands:
docker node ls
docker info
Output:
54
55
Promote a worker to manager
Also note that each manager also acts as a worker, so from your docker
info output you should see 6 workers and 3 manager nodes.
56
Using Services
docker service ls
Output:
Then in order to get a list of the running containers you need to use the
following command:
Output:
57
Then you can visit the IP address of any of your nodes and you should
be able to see the service! We can basically visit any node from the
swarm and we will still get the to service.
58
Scaling a service
We could try shutting down one of the nodes and see how the swarm
would automatically spin up a new process on another node so that it
matches the desired state of 5 replicas.
Output:
In the screenshot above, you can see how I've shutdown the droplet
called worker-2 and how the replica bobby-web.2 was instantly started
again on another node called worker-01 to match the desired state of 5
replicas.
Output:
59
This would automatically spin up 2 more containers, you can check this
with the docker service ps command:
Then as a test try starting the node that we've shutdown and check if it
picked up any tasks?
Tip: Bringing new nodes to the cluster does not automatically distribute
running tasks.
60
Deleting a service
Output:
Now you know how to initialize and scale a docker swarm cluster! For
more information make sure to go through the official Docker
documentation here.
61
Docker Swarm Knowledge Check
Once you've read this post, make sure to test your knowledge with this
Docker Swarm Quiz:
https://quizapi.io/predefined-quizzes/common-docker-swarm-interview-q
uestions
62
Conclusion
As a next step make sure to spin up a few servers, install Docker and
play around with all of the commands that you've learnt from this
eBook!
63
Other eBooks
Some other opensource eBooks that you might find helpful are:
64