from Zero to Swarm, your one stop Docker shop

 

Docker, the most popular container technology nowadays. By nature it’s an application container, means if you want apache you will get apache inside your container. Or if you want to run mysql inside your container its mysql only. Got the picture? Cool. In this post I ll show you how you can start with docker container. But before make your hands dirty, let’s see how docker work.

:: How its works ::

docker containers are based on image. Image is a read only entity which you cannot change. You can take a base image and in that image you can add other functionality/package. When you run a container your docker daemon first look for the image on your local machine, if it doesn’t find any image then it goes to the online image repository for docker call docker hub. Once it finds your image, it will download that image and make a local copy available. So next time when you run another container with the same image the docker will take that image from your local machine. So that’s the basic of how docker works. There are other things you should know about docker which I’ll tell you time to time. I’ll cover the below topics in this post:

  1. Installation
  2. Managing Docker container
  3. Docker volumes
  4. Docker networking
  5. Dockerfile
  6. Docker Hub
  7. docker-compose
  8. Docker Swarm

Let’s make our hands dirty with docker.

1: Installation


Docker comes with two edition ce (Community Edition) and ee (Enterprise Edition).  Community edition is the free edition which we will use in this tutorial. I ll use an ubuntu 18.04 LTS server for this lab.

Type the below command to install the docker-ce in your host.

curl -fsSL get.docker.com -o get-docker.sh

this will download a script called “get-docker.sh” in your current directory. Type the below command to run the script which will install the docker-ce in your host.

sudo sh get-docker.sh

after the installation run the below command. The command will add your current user to the docker group so that you can run docker commands without sudo privileges.

sudo usermod -aG docker your-user

After that, run the following command to check then docker version

docker –-version

or

docker version
1.1:  Run the first docker container

You have successfully installed the docker-ce in your host now it’s time to run our first container. Type the below command to run the “hello-world” docker container.

docker run hello-world

you will see some activity in your screen. Keep patient and watch carefully. After sometime you will see a message like the below one:

Hello from Docker!
This message shows that your installation appears to be
working correctly.

And also some other message which simply describes what happen when you run the docker run command.

1.2: The busybox container

Let’s run another container name “busybox”. You can pass any command during the runtime and it will give you the output of that command. Type the below command to run the busybox container.

docker run busybox echo "Hello docker"

you can pass any other command instead of echo command. Try to run the container again with other command you like.

2: Managing docker container


Now we will see the docker ps command. A very useful command to see the status of a container. We will see how to download docker image and how to run container using that downloaded image. We will also see how to enter and exit from a container. How to start stop killing and finally how to delete a container and image.

2.1: docker ps command

Run the below command to check the status of the container. So far we have run some container, Let’s check the status of our container.

docker ps

This command will print an empty list on your screen. Means no container is running at this stage. To see the previously run container, type the below command:

docker ps -a

This will give you a list of containers which you run before.

2.2: Docker images

As you already know that the container are images based and we already run some container. You can find the image information of those containers by typing the below command:

docker images

This will give you the list of downloaded images while running the container.

2.3: Download docker image

Docker download the respective image during the runtime of a container, but you can download your desire image if you want. You can also perform a search for the specific image. Type the below command to see a list of images docker have in its online repository docker hub. Let’s try to search the ubuntu image.

docker search ubuntu

You will see a list of Ubuntu images available online on docker hub. After getting your desire image it’s time to download that image. Type the below command to download the Ubuntu image.

docker pull ubuntu

The command will download the Ubuntu latest image available in docker hub. Check the newly downloaded image in your local repository. Again type the below command to see the local image list.

docker images

This will give you locally download image. if you want to learn more about your downloaded image then type the below command.

docker image inspect ubuntu

You will find the detail information about the image.

2.4: Run Ubuntu container with shell access

After download the Ubuntu image lets run a container and access that container using that image. Type the below command for create a running container.

docker run -it ubuntu bash

After running that command you will find yourself inside a running container. You can check the Ubuntu version by using the cat command.

cat /etc/lsb-release

Now let’s exit from the container shell. Type the exit command to exit from the container. Use the docker ps command to see the status of the container. Again, you will get an empty list. Do you know why?

2.5: Exit from the container

Every time you run the docker run command docker creates a container and put you in that container. But when you exit from that container the container stops. That’s the reason every time you get an empty list with the docker ps command. If you exit from the container the container will stop, but we want to keep our container running. Once again run another container.

docker run -it ubuntu bash

The container should start and you are now inside that container. Type the below key sequences for exit from the container without stopping it.

Ctrl+pq

Now check the docker ps command again. There should be a running container now.

2.6: Docker start/stop/kill

You can stop a running container and similarly you can start a stop container. We now have some stop containers in our system. Find the list of the stopped container by docker ps – a command. From that list you can start your container. Type the below command to start a stop container.

docker start <contaierid/names>

If you want to stop or kill the container you can stop it by typing the below command.

docker stop/kill <contaierid/names>

The stop command sends a shutdown signal to the container. It will try to shut down the container in a proper way. The kill command sends a kill signal to the container which then immediately kill the container. kill is faster than stop.

2.7: Delete the container

After running some busybox container you are going to have a lot of stop container. To remove those containers run the below command:

docker rm container_id/container_name

To delete all the container all at a times type the below command:

docker rm $(docker ps -a -f status=exited -q)
2.8: Delete docker image

You can delete docker images from your docker host by typing the below command

docker rmi <imagename>

3: Docker volumes


Now we will see how docker handles volume. Volumes are used by docker container for persistent data. Volumes can be mounted in different way and can be shared among the container. We will see how to mount a volume, share the mounted volume among the containers.

3.1: Volumes and bind mounts

Volumes in docker are responsible for handling the data. You can create a container and can delete a container within a second but do you think what happen if you have data in that container? How you will make your data persistent? Or how you can share your data among other containers?

Docker has two options for containers to store files on the host machine, so that the files are persisted even after the container stops. They are volumes, and bind mounts. If you’re running Docker on Linux, you can also use a tmpfs mount. In this lab we will see how you can use volumes and bind mount inside a container.

Type the below command to create a volume name data-vol1

docker volume create data-vol1

type the below command to check the volume.

docker volume ls

you should see your newly created volume data-v1ol1. You can also find your volume on /var/lib/docker/volumes directory. To know the size and other information about the volume type the below command.

docker system df -v

Let’s run a simple ubuntu container using this volume.

docker run -it --mount source=data-vol1,target=/data ubuntu

You should now be inside the container. Type the ls command. You should see a data directory name data inside your container. Now make a file in the data directory.

cd /data && touch my-file && echo This is a file inside volume > my-file

Exit from that container. The container will stop. If you want your data again run another container with the volume data-vol1. You will get your data again.

3.2: Sharing data between container

You can share one volume with multiple container. Just run the below command twice, you will get 2 container sharing the same volume data-vol1 inside data directory.

docker run -it --mount source=data-vol1,target=/data ubuntu

You can create, delete, edit files in that volume. After doing your editing, exit from the container.

3.3: Read-only mount

Containers sharing the same volume does not maintain the file locking feature. All containers using the volume can write data at a same time, as a result chance of file corruption is high. To overcome this issue, you can mount a volume as read-only. Type the below command to mount a volume read-only.

docker run -it --mount source=data-vol1,target=/data,readonly ubuntu

Now try to create a directory inside the /data directory and see what happen.

3.4: Remove volumes

To remove a volume, you should be very careful because inside your volume you have your data and once you remove the volume your data will also be removed. Before deleting the volume make sure that the volume is not used with any container (stop/running). Type the below command to remove a volume.

docker volume rm data-vol1

To delete all volumes, type the below command

docker volume prune
3.5: Bind mount

Bind mounts are relatively easy and have been around early days of Docker. When you use a bind mount, a file or directory on the host machine is mounted in a container. Follow the below step to perform a bind mount.

  1. Create a directory in your host machine data.
mkdir data

2. Run a container and mount the data directory to the container.

docker run -it --mount type=bind,source="$(pwd)"/data,target=/app ubuntu

4: Docker networking


Now we will see the bridge networking drives provided by docker. We will create our own network and we will see how a docker container communicates with the outside world with exposing ports.

4.1: Network types

Docker comes with different network drivers. The most common network types are bridge, overlay and macvlan.

docker0

By default, docker creates “docker0” interface on the host machine during installation time. This docker0 interface is a bridge device for docker. All traffic from the docker containers flows over it to the Docker daemon, which handles routing on behalf of the container. If you don’t specify any network type all containers will connect to this device.

Before doing anything, let’s see what types of networking driver are available in the system. Type the below command to list the currently available network on the docker host machine.

docker network ls
NETWORK ID         NAME        DRIVER      SCOPE
0a5048ffd124       bridge      bridge      local
b548da2c88a1       host        host        local
7c6ce4cef89c       none        null        local

You will see an output like above (the network ID will not be the same). The default bridge network is listed along with host and none.

Let’s see some more detail information on the default bridge network. Type the below command to get more information about the default bridge network.

docker network inspect bridge

You will see the subnet and the gateway information for this default network. Now let’s run another container and see how this output look like. This time we will run alpine image.

docker run -it --name node1 alpine

This will run a new container based on alpine image. Let’s see what is the IP address of this container by providing ifconfig command inside the container. Exit from the container by pressing Ctrl+pq and run another alpine container.

docker run -it --name node2 alpine

Check the IP address of the new container and also check that the both containers can communicate with each other. You can inspect the bridge network again for more information.

4.2: New bridge network

It is possible to create your own customize bridge network. Type the below command to create your own bridge network.

docker network create -d bridge sanognet

Check the network list again by typing the below command.

docker network ls
NETWORK ID     NAME     DRIVER    SCOPE 
0a5048ffd124   bridge   bridge    local 
b548da2c88a1   host     host      local 
7c6ce4cef89c   none     null      local
c640505a8203   sanognet bridge    local

As you can see the newly create network sanognet is available in the system. Let’s inspect the network by typing the below command. It will give you the IP and other information about the network.

docker network inspect sanognet

Let’s run a container and connect that container on our newly created network.

docker run -it --rm --name=node1 --net=sanognet alpine

check the IP address of the container.

4.3: Customize the IP address

It is also possible to create a new network with the choice of your IP address pool. Type the below command to create another bridge network with the customize IP address.

docker network create --subnet=192.168.10.0/24 --ip-range 192.168.10.0/29 bdnognet

Run a container and connect that container in this newly created network.

4.4: Bind container port

By default, all docker containers can connect to the outside world, but the outside world cannot connect to the container. To connect the outside world to your docker container you can use the –expose <port> command line flags. Run the below command to create a container using the nginx image with exposing the 8080 port. First let’s download the nginx image.

docker pull nginx

After downloading the image let’s run a container using this image.

docker run --name=web1 -p 80 -d nginx

Type the docker ps command and see the PORTS. You will see a random port is assigned by docker daemon. You can access this container from outside your host using this port number. Open a browser or curl to access this container.

http://<IP ADDRESS OF YOUR DOCKER HOST>:<PORT>

You can also define your port if you want. Just type the below command to assign your chosen port to the container.

docker run --name=web2 -p 8080:80 -d nginx

Type the docker ps command to find out your port.

5: Dockerfile


Docker has a variety of image collection in docker hub, which can be downloaded easily and can be used by anyone. But you can create your own custom image using the Dockerfile. Dockerfile is a set of instruction which simply tell docker how to build an image. In this lab we will see how we can create our own customized image using the Dockerfile.

5.1: The Dockerflle

Let’s create a custom image based on our previously downloaded image Ubuntu.

Before doing that let’s run a new container based on Ubuntu image. After running the container try the below command inside that container.

wget 
vim

both commands will give you same result command not found. Which means inside our Ubuntu container wget and vim packages are missing. Our goal is to create a new image using the Ubuntu image where vim and wget package will be available. Perform the below steps for creating our first docker image.

Exit from the container by typing the exit command. This will stop the container.

  1. Create a directory called my-image in your host computer and access that directory.

2. Inside that directory, create a file called Dockerfile and type the below text in that file.

FROM ubuntu
     RUN apt-get upgrade && apt-get update –y
     RUN apt-get install –y wget
     RUN apt-get install –y vim

3. Save and exit from that file.

4. Run the below command to build the image.

docker build -t my-image1 .

(don’t miss the dot at the end of this command)

5. This may take some times depend on your internet speed. Keep patient and watch carefully on your screen.

6. After finishing all the step, the script will stop and you will see a Successfully built message. Which means our image is ready to go. Type docker image to see our newly built image.

7. Let’s run a new container using our custom image. Type the below command to run a new container

docker run -it my-image1

8. Try to run wget and vim again. You will find these two packages are installed in our new container.

6: Docker Hub


In this lab we will explore the online docker repository called docker hub. We will also see how we can upload our image in the docker hub. You need a docker hub account for performing this lab.

6.1: Docker ID

Go to https://hub.docker.com/ and create a free docker hub account. After creating the account login with your newly created account. You should remember your username and credential as we are going to use that in this lab.

6.2: Docker login

Login to your docker account by typing the below command.

docker login

You should provide your username and credential to complete the login. After that you should see a Login Succeeded message.

6.3: Docker tag

Tag your image using the below command

docker tag my-image1:latest youruserid/curl-vim-image

Check the tagging by typing docker image command.

6.4: Push image to docker hub

Now your image is ready for upload. Type the below command to upload your image.

docker push youruserid/curl-vim-image

After some time, your image will be uploaded to the docker hub. Check your docker hub account to find the image. You will see an instruction about how to pull your image from the docker hub account.

6.5: Docker logout

After uploading the image, you can logout from your docker hub account by typing docker logout.

7: docker-compose


 

Compose is a tool used for running multi-container docker application. In this lab we will see how to install docker-compose. After that we will run a simple wordpress site in multiple docker container.

7.1: Installation

First, you need to install the docker-compose package. Fire up the below command to install docker-compose in your host machine.

sudo curl -L
https://github.com/docker/compose/releases/download/1.22.0/doc
ker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

Make the binary executable.

sudo chmod +x /usr/local/bin/docker-compose

check your docker-compose is working by typing the below command.

docker-compose –version

Showing the docker-compose version indicate that your installation is working.

7.2: docker-compose.yml

Docker-compose is installed and now it’s time to create the project.

  1. Create a directory named my-site (or any other name you like). This directory will be your project directory.

2. Create a file in that directory name docker-compose.yml.

3. Type/paste the below text in docker-compose.yml file.

version: '3.3'
services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: <yourpassword>
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: <yourpassword>
   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: <yourpassword>
volumes:
    db_data:

4. Save and exit from the file.

5. Type the below command to run the wordpress site.

docker-compose up –d

After some time, your wordpress site will be up and running. Check the site by open a browser and type your host IP address followed by 8000 port.

7.3: docker-compose down

if you don’t need the project or you want to down the project type the below commad.

docker-compose down

This will stop the containers and also delete it from your host, but the volume will remain in your host with all your data.

8: Docker swarm


In this lab we will see how to install Docker clustering service name Docker swarm. We will install a three node cluster, run service in that cluster and will see how to scale up/down services in the swarm. Docker engine should be installed in those participating cluster nodes.

8.1: Lab scenario

Docker swarm is made up of two main components.

  • manager nodes (handle the cluster management task)
  • worker nodes (execute container)

All nodes in the cluster should be able to talk to each other over the network. In this lab we will use one manager node and two worker node.

8.2: Initializing swarm

Login to your server which will be your manager node and type the below command and press enter:

docker swarm init --advertise-addr <MANAGER IP ADDRESS>

After that you should see an output like below (the node id and the token value may not be same). It’s simply telling you that the swarm has initialized and how you can add a worker in that swarm (bold and red).

Swarm initialized: current node (ujwq7vh4rmtf5yglx2cqqzbvx) is now a manager. 
To add a worker to this swarm, run the following command: 
docker swarm join --token <token> <MANAGER IP ADDRESS>:2377
To add a manager to this swarm, run 'docker swarm join-token
manager' and follow the instructions.

Type the below command to check the node status

docker node ls

Now our manager node is ready, let’s add our two worker node in the cluster.

8.3: Add worker

Copy the command docker swarm join –token…….:2377 from the previous command output. Login to the worker node and paste the command into your worker node command prompt. After executing the command, you should see an output like below.

This node joined a swarm as a worker.

Repeat the step in your second worker node. Back to the manger node and run the below command to see the status of the cluster nodes.

docker node ls
8.4: Run services

Our docker swarm cluster is up and running. It’s time to deploy services in that cluster. Let’s run a web service in that cluster. We will use katacoda/docker-http-server image. On the manager node type the below command to deploy the the web services.

docker service create --name webserver -p 80:80 katacoda/docker-http-server

The above command will create a web server container. Check the running services with the following command

docker service ps webserver

check the NODE column and see in which node the cluster creates the container.

8.5: Scale UP/Down

We create the cluster, join node to the cluster, run a service on that cluster now let’s scale up the services. Type the below command on the manager node to scale up the services across 6 containers.

docker service scale webserver=6

The above command will create 6 containers and run the same services inside that container. Type the below command to check in which node the cluster create the containers.

docker service ps webserver

Again follow the NODE column. If you want to scale down the cluster nodes just type the below command.

docker service scale webserver=2

After that check the service again.

8.6: Remove the Service

Type the below command on the manager node to remove the service.

docker service rm webserver

After removing the service got to each node run the docker ps command. You will get an empty list of containers.

8.7: Promote or demote node

You many need to bring a manager offline for maintenance purpose or for any other reason. In that case you can promote a worker node to manager role or a manager node to worker role. Type the below command from manager to make a worker node to manger.

docker node promote worker1

To demote a manager node to worker type the below command on a manager node.

docker node demote manager
8.8: Leave the swarm

If you need to remove any nodes from the swarm type the below command on that node.

docker swarm leave

check the node status from the manager node. The status of the node should be Down.

8.9: Join the node again

If you need to join the node again you need the token string. For getting the token string type the below command. The command is available for both manager and worker.

docker swarm join-token worker

copy the command from the command prompt and go to the node which you want to join. Paste the code and you will be online.

:: Conclusion ::

That’s all for today.  Docker is not only making the developer’s life easy, it can also make the sysadmin job simple. There are lots of images in the hub, which can be used by the sysadmin. Explore the hub and yes, if it’s not there you can now make your own image with Dockerfile. Enjoy Docker!

Thanks for reading the post. If you enjoyed the post, please share it with your network and let me know your thoughts in the comments. 

About the Author: Imtiaz is working in a financial organization in Bangladesh and having experience in system, network and security administration. Feel free to contact with him on LinkedIn or Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top
%d bloggers like this: