Skip to content

Dockerízate, part 2

Share on twitter
Share on linkedin
Share on email
Share on whatsapp
Dockerízate - Part 2

Unlike the first post, here we are going to see how to use existing image containers and use Docker-Compose to manage them as services from a clean, virtualized environment.

If we have followed the steps of the previous post, we will start, as we have mentioned before, from a clean and virtualized environment with Docker, which we will manage with Vagrant. We will use a very extended system as an example: A non relational MongoDB database for our persistent information and a web application that will communicate with it. The web application will use Spring Boot for rapid prototyping, with an embedded Tomcat server. The scheme would be as follows:

Ports 27017 for MongoDB and 8085 for Tomcat will be used. Externally, from our local windows, these ports will be mapped to 27018 and 8087, respectively.

1. Let's get down to business. Now what?

First, we'll install the database. We looked at DockerHub for a current image of MongDB: (https://hub.docker.com/_/mongo/). To get the container, it's as easy as using the docker run command with the specific parameters of the image we want (which are explained on DockerHub's own page):

$> docker run --name mongodb -d mongo:latest
vagrant@ubuntu-xenial:~$ docker run --name mongodb -d mongo:latest
Unable to find image 'mongo:latest' locally
latest: Pulling from library/mongo
7b8b6451c85f: Pull complete
[...]
cd245aa9c426: Pull complete
Digest: sha256:0823cc2000223420f88b20d5e19e6bc252fa328c30d8261070e4645b02183c6a
Status: Downloaded newer image for mongo:latest
91c4a067c2d7c4552076a356d80e55cf6bf21feef64578ea7364f52989dda692
$>

Ready! We now have the first container with MongoDB running on our virtual machine. We can see the status with the "ls" command on the docker containers:

$> docker container ls
CONTAINER ID   IMAGE          COMMAND                  STATUS         PORTS               NAMES
91c4a067c2d7   mongo:latest "docker-entrypoint.s…" Up 8 seconds  27017/tcp        mongodb

Let's remember that the containers form the Docker software unit. When several containers are required in the same system, the previous Docker "run" option becomes small and unwieldy. Next, it is the turn of Docker-Compose. Therefore, before we continue, we stop the current container and remove it so that we don't have problems in the next steps:

$> docker stop mongodb
mongodb
$> docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

2. Curling up, Docker-Compose

Docker-Compose allows us to treat containers as if they were services. It allows us to configure and adapt them to each environment according to the needs (certification, production, etc.). The best way to understand it is to test how it works. Therefore, we are going to install it in our virtual machine:

$> sudo apt-get update
$> sudo curl -L https://github.com/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   617    0   617    0     0    897      0 --:--:-- --:--:-- --:--:--   896
100 11.2M  100 11.2M    0     0  1172k      0  0:00:09  0:00:09 --:--:-- 1284k
$> sudo chmod +x /usr/local/bin/docker-compose

To install the latest version of Docker-Compose, you can review the above commands at: https://github.com/docker/compose/releasesDocker-Compose uses a docker-compose file in YML format. Go to the /docker folder (which you have created in your home: /home/vagrant) and create a docker-compose.yml file with the following content

version: '3'
services:
    mongodb:
        image: mongo:latest
        container_name: "mongodb"
        volumes:
          - ./data/:/data/
        ports:
          - 27017:27017

Next, we execute the "up" command of Docker-Compose, which will read the docker-compose.yml file and raise the services with the indicated configuration. Then we use the "-d" parameter to start the services in the background:

 $> docker-compose up -d Creating mongodb ... done

Remember! If we want to see the logs, we can do it at any time from our container/service with the command "logs".

$> docker-compose logs -f mongodb
Attaching to mongodb
mongodb    | 2018-12-05T13:15:28.191+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
mongodb    | 2018-12-05T13:15:28.199+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=d2de9617fefd
[...]

Finally, we check that our MongoDB service is working:

$> docker-compose ps
 Name               Command             State            Ports
------------------------------------------------------------------------
mongodb   docker-entrypoint.sh mongod   Up      0.0.0.0:27017->27017/tcp

If we want to stop the services, we can do so at any time with the "stop" command:

$> docker-compose stop
Stopping mongodb ... done

Great! Now we have the MongoDB instance back up and running with a little more control over the configuration. On the other hand, port 27017 is open in the virtual machine. If we want to access it from outside the Vagrant, i.e. from our local system, we have to open this port in the Vagrantfile configuration:

Vagrant.configure("2") do |config|
  # Every Vagrant development environment requires a box.
  config.vm.box = "ubuntu/xenial64"

  # Share an additional folder to the guest VM.
  config.vm.synced_folder "./docker", "/home/vagrant/docker"
  config.vm.synced_folder "./shared", "/home/vagrant/shared"

  # Mongo
  config.vm.network "forwarded_port", guest: 27017, host: 27018

  # VirtualBox specific configuration 
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "6144"
    vb.cpus = "2"
  end
end

By changing the Vagrantfile and restarting the Vagrant, we will have port 27018 accessible from our local Windows system, which will in turn connect to port 27017 of the virtual machine where MongoDB is listening. To connect to MongoDB we can use any client (RoboMongo, for example).

3. Great, I want more.

In a few steps, and with Docker installed, we have a MongoDB database up and running without the need to install anything on our premises. We can go further, and set up a system as equipped as we want, adding containers such as an Apache Kafka, ElasticSearch, etc. Docker-Compose will be in charge of setting up the configuration, raising the instances and communicating between each service. In the next and last post, we will see how to generate a container of an own application and distribute it.

Share the article

Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on email
Email
Share on whatsapp
WhatsApp

A new generation of technological services and products for our customers