Monthly Archives: March 2016

Docker: push image to Docker hub

Introduction

In the previous post, we created an Express application using a Dockerfile. We successfully ran this container at the end of that post (e.g we could see the ‘hello world’ string with our webbrowser. As it worked successfully, we will now push our own image to the Docker Hub so other people could benefit from what we did (ok ok I admit, this one is not immediately very useful, but hey…we just want to learn the procedures here a bit).

Uploading the image

Uploading is very straightforward. Just use the ‘docker push command’:

root@ubuntu-demo:/home/cloud-user/express_app# docker push wiwa1978/ubuntu-express-app
The push refers to a repository [docker.io/wiwa1978/ubuntu-express-app] (len: 1)
22d235d53e91: Pushed
f07e565d49b6: Pushed
9b040de63457: Pushed
985ecf42d9fd: Pushed
e89f5d8a7537: Pushed
e44fb8877894: Pushed
b1229835f0b0: Pushed
82743c3f2046: Pushed
f4daada71496: Pushed
3d825e628dcd: Pushed
609701a297c0: Pushed
0a85186b8534: Pushed
4f0fe115ac4f: Pushed
5a62bb966323: Pushed
afaba416316a: Pushed
1fef4ef98b66: Pushed
latest: digest: sha256:83e60421423d34a4d0a7582401bc7c5a44367d77e5acb3b135a7b1ad933e1d14 size: 26934

You will then see on the Docker Hub that the image was added under your account. See below:
dockerhub

Pushing image to existing docker repository on Docker Hub

One of the issues with previous approach is that the information on Docker Hub is empty, there is no description, no short description etc…See below screenshot:

dockerhub1

So I’ve been looking a bit into this. One way of solving this is to create your repository on Docker Hub by clicking the ‘Create Repository’ button and fill out the details. See below screenshot for an example:
create_repo

Now, the repository we just created through Docker Hub is empty. Obviously, as we did not push anything into the repository yet. Let’s do that now…

I first tried to simply push the image to Docker hub as follows:

root@ubuntu-demo:/home/cloud-user/express_app# docker push wiwa1978/ubuntu-express-info
The push refers to a repository [docker.io/wiwa1978/ubuntu-express-info] (len: 0)
Repository does not exist: wiwa1978/ubuntu-express-info

Obviously, that was stupid and Docker let me rightfully know that he did not find the repository. So I read further on some websites and found that I needed to tag the image. The instructions are as follows: In order to push a repository to the Docker Hub, you need to name your local image using your Docker Hub username, and the repository name that you created in the previous step. The syntax is docker tag /[:]. So here we go:

root@ubuntu-demo:/home/cloud-user/express_app# docker tag wiwa1978/ubuntu-express-app wiwa1978/ubuntu-express-info:v1

Let’s try to push again:

root@ubuntu-demo:/home/cloud-user/express_app# docker push wiwa1978/ubuntu-express-info:v1
The push refers to a repository [docker.io/wiwa1978/ubuntu-express-info] (len: 1)
22d235d53e91: Pushed
9b040de63457: Pushed
985ecf42d9fd: Pushed
e89f5d8a7537: Pushed
b1229835f0b0: Pushed
82743c3f2046: Pushed
f4daada71496: Pushed
3d825e628dcd: Pushed
609701a297c0: Pushed
0a85186b8534: Pushed
4f0fe115ac4f: Pushed
5a62bb966323: Pushed
afaba416316a: Pushed
1fef4ef98b66: Pushed
v1: digest: sha256:461609df290c536b627894e84cddb5013aea5721fe55f94bf9b587a5c96c7208 size: 26931

Ok, that seems to be pretty OK. Let’s now check if something changed on Docker Hub. See screenshot below:
dockerhub2
I was hoping to see my actual container files, but apparently that’s not the case. However, I’m sure the ‘push’ was successful added to the repository as my tag (v1) is being shown on the repository called ubuntu-express-info.

That’s it for now!

Docker: dockerize webapplication (with Dockerfile)

Introduction

In this post, we will create a simple Express web application and build our own image from that. In a next post, we will also upload the image to the docker hub repository so other people could use it as well.

Create Express webapplication

First, let’s create a simple Express web application. Note that we store every file in the folder /home/cloud-user/express-app.

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World wapptastic')
})

app.get('/info', function (req, res) {
  res.send('This is the info page')
})

var server = app.listen(3001, function () {

  var host = server.address().address
  var port = server.address().port

  console.log('Example app listening at http://%s:%s', host, port)

})

I believe the code is rather straightforward. Whenever someone is visiting the root of our webapplication, we will return back the string ‘Hello World wapptastic’ and whenever someone goes to the /info route, we will return back the string ‘This is the info page’.

As we have seen in previous posts, every Express app needs a package.json file to provide metadata and toresolve the dependencies. Here is the package.json file for our example:

root@ubuntu-demo:/home/cloud-user/express_app# cat package.json
{
  "name": "docker-ubuntu-express",
  "private": true,
  "version": "0.0.1",
  "description": "Express application displaying Hello world on Ubuntu using docker",
  "author": "Wim Wauters ",
  "dependencies": {
    "express": "3.2.4"
  }
}

The Docker part now…

Next up is the docker configuration. As mentioned before, the better way of creating your own images is by creating a Dockerfile. This file basically contains a set of instructions that tell Docker how to build our image.

Create the Dockerfile

FROM ubuntu:14.04

# Enable EPEL for Node.js
RUN apt-get update
RUN apt-get -y install build-essential
RUN apt-get -y install nodejs
RUN apt-get -y install npm
RUN apt-get -y install git
RUN apt-get -y install git-core

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE  3001
CMD ["nodejs", "/usr/src/app/index.js"]

Build the image

root@ubuntu-demo:/home/cloud-user/express_app# docker build -t wiwa1978/ubuntu-express-app .
Sending build context to Docker daemon 4.096 kB
Step 1 : FROM ubuntu:14.04
 ---> c9ea60d0b905
Step 2 : RUN apt-get update
 ---> Using cache
 ---> 4f0fe115ac4f
Step 3 : RUN apt-get -y install build-essential
 ---> Using cache
 ---> 0a85186b8534
Step 4 : RUN apt-get -y install nodejs
 ---> Using cache
 ---> 609701a297c0
Step 5 : RUN apt-get -y install npm
 ---> Using cache
 ---> 3d825e628dcd
Step 6 : RUN apt-get -y install git
 ---> Using cache
 ---> f4daada71496
Step 7 : RUN apt-get -y install git-core
 ---> Using cache
 ---> 82743c3f2046
Step 8 : RUN mkdir -p /usr/src/app
 ---> Using cache
 ---> b1229835f0b0
Step 9 : WORKDIR /usr/src/app
 ---> Using cache
 ---> e44fb8877894
Step 10 : COPY package.json /usr/src/app/
 ---> Using cache
 ---> e89f5d8a7537
Step 11 : RUN npm install
 ---> Using cache
 ---> 985ecf42d9fd
Step 12 : COPY . /usr/src/app
 ---> Using cache
 ---> 9b040de63457
Step 13 : EXPOSE 3001
 ---> Using cache
 ---> f07e565d49b6
Step 14 : CMD nodejs /usr/src/app/index.js
 ---> Using cache
 ---> 22d235d53e91
Successfully built 22d235d53e91

Running the ‘docker images’ command will show indeed that an image was created with name wiwa1978/ubuntu-express-app

root@ubuntu-demo:/home/cloud-user/express_app# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
wiwa1978/ubuntu-express-app   latest              22d235d53e91        5 minutes ago       408.6 MB
wiwa1978/fedora-httpd         latest              5fa2fee12007        57 minutes ago      573.3 MB
ubuntu                        14.04               c9ea60d0b905        6 days ago          188 MB
centos                        latest              28e524afdd05        11 days ago         196.7 MB
fedora                        latest              9bdb5101e5fc        5 weeks ago         204.7 MB
training/webapp               latest              54bb4e8718e8        11 months ago       348.8 MB

Running the image we just created

root@ubuntu-demo:/home/cloud-user/express_app# docker run -it -p 3001:3001 wiwa1978/ubuntu-express-app
Example app listening at http://0.0.0.0:3001

In the output, you can see that we have our app listing to port 3001. Note that we have mapped the port 3001 of our host system to the port 3001 within our container as in the index.js file we have set the TCP port for our webapp indeed to 3001. See below the screenshot as a proofpoint.

expressapp

Docker: building our own images

In the previous posts (like here , or here ), we always worked with images that already existed, that were created by someone else and made available on Docker hub.

In order to see the images that were already downloaded on our host system, run the ‘docker images’ command.

root@ubuntu-demo:/home/cloud-user# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              latest              c9ea60d0b905        6 days ago          188 MB
training/webapp     latest              54bb4e8718e8        11 months ago       348.8 MB

In our case, you’ll see we already pulled the ubuntu images (for this post) as well as the training/webapp images (for this post).

Docker will automatically pull an images when using the docker run images, but this could be a little bit annoying as it takes a while. So we could also preload certain images upfront so there is no need to download them anymore when running the docker container.

Building our own images

Let’s say we want to start from a base image, but tweak it to our own needs. As an example: let’s pull the latest
centos OS and install a LAMP server on it. We can do this as follows:

root@ubuntu-demo:/home/cloud-user# docker pull fedora
Using default tag: latest
latest: Pulling from library/fedora
6888fc827a3f: Pull complete
9bdb5101e5fc: Pull complete
Digest: sha256:1fa98be10c550ffabde65246ed2df16be28dc896d6e370dab56b98460bd27823
Status: Downloaded newer image for fedora:latest

Running the ‘docker images’ command again, shows that the images was pulled successfully and is now available on our host system.

root@ubuntu-demo:/home/cloud-user# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              latest              c9ea60d0b905        6 days ago          188 MB
centos              latest              28e524afdd05        11 days ago         196.7 MB
fedora              latest              9bdb5101e5fc        5 weeks ago         204.7 MB
training/webapp     latest              54bb4e8718e8        11 months ago       348.8 MB

Now, let’s go into the Fedora container…

root@ubuntu-demo:/home/cloud-user# docker run -it fedora /bin/bash
[root@115d35aa4bcb /]#

…and install the LAMP software

[root@115d35aa4bcb /]# yum install -y httpd
Yum command has been deprecated, redirecting to '/usr/bin/dnf install -y httpd'.
See 'man dnf' and 'man yum2dnf' for more information.
To transfer transaction metadata from yum to DNF, run:
'dnf install python-dnf-plugins-extras-migrate && dnf-2 migrate'

Last metadata expiration check: 0:01:39 ago on Wed Apr 13 13:17:07 2016.
Dependencies resolved.
============================================================================================================================================================
 Package                                     Arch                            Version                                 Repository                        Size
============================================================================================================================================================
Installing:
 apr                                         x86_64                          1.5.2-2.fc23                            fedora                           113 k
 apr-util                                    x86_64                          1.5.4-2.fc23                            fedora                            96 k
 fedora-logos-httpd                          noarch                          22.0.0-2.fc23                           fedora                            33 k
 httpd                                       x86_64                          2.4.18-1.fc23                           updates                          1.3 M
 httpd-filesystem                            noarch                          2.4.18-1.fc23                           updates                           25 k
 httpd-tools                                 x86_64                          2.4.18-1.fc23                           updates                           88 k
 mailcap                                     noarch                          2.1.45-1.fc23                           fedora                            37 k

Transaction Summary
============================================================================================================================================================
Install  7 Packages

Total download size: 1.7 M
Installed size: 6.7 M
Downloading Packages:
(1/7): apr-util-1.5.4-2.fc23.x86_64.rpm                                                                                     141 kB/s |  96 kB     00:00
(2/7): mailcap-2.1.45-1.fc23.noarch.rpm                                                                                     263 kB/s |  37 kB     00:00
(3/7): apr-1.5.2-2.fc23.x86_64.rpm                                                                                          137 kB/s | 113 kB     00:00
(4/7): httpd-filesystem-2.4.18-1.fc23.noarch.rpm                                                                             96 kB/s |  25 kB     00:00
(5/7): fedora-logos-httpd-22.0.0-2.fc23.noarch.rpm                                                                          212 kB/s |  33 kB     00:00
(6/7): httpd-tools-2.4.18-1.fc23.x86_64.rpm                                                                                 210 kB/s |  88 kB     00:00
(7/7): httpd-2.4.18-1.fc23.x86_64.rpm                                                                                       533 kB/s | 1.3 MB     00:02
------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                       394 kB/s | 1.7 MB     00:04
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Installing  : apr-1.5.2-2.fc23.x86_64                                                                                                                 1/7
  Installing  : apr-util-1.5.4-2.fc23.x86_64                                                                                                            2/7
  Installing  : httpd-tools-2.4.18-1.fc23.x86_64                                                                                                        3/7
  Installing  : fedora-logos-httpd-22.0.0-2.fc23.noarch                                                                                                 4/7
  Installing  : httpd-filesystem-2.4.18-1.fc23.noarch                                                                                                   5/7
  Installing  : mailcap-2.1.45-1.fc23.noarch                                                                                                            6/7
  Installing  : httpd-2.4.18-1.fc23.x86_64                                                                                                              7/7
Error unpacking rpm package httpd-2.4.18-1.fc23.x86_64
Error unpacking rpm package httpd-2.4.18-1.fc23.x86_64
error: unpacking of archive failed on file /usr/sbin/suexec;570e473c: cpio: cap_set_file
httpd-2.4.18-1.fc23.x86_64 was supposed to be installed but is not!
  Verifying   : httpd-2.4.18-1.fc23.x86_64                                                                                                              1/7
  Verifying   : apr-1.5.2-2.fc23.x86_64                                                                                                                 2/7
  Verifying   : apr-util-1.5.4-2.fc23.x86_64                                                                                                            3/7
  Verifying   : mailcap-2.1.45-1.fc23.noarch                                                                                                            4/7
  Verifying   : httpd-filesystem-2.4.18-1.fc23.noarch                                                                                                   5/7
  Verifying   : httpd-tools-2.4.18-1.fc23.x86_64                                                                                                        6/7
  Verifying   : fedora-logos-httpd-22.0.0-2.fc23.noarch                                                                                                 7/7

Installed:
  apr.x86_64 1.5.2-2.fc23             apr-util.x86_64 1.5.4-2.fc23    fedora-logos-httpd.noarch 22.0.0-2.fc23    httpd-filesystem.noarch 2.4.18-1.fc23
  httpd-tools.x86_64 2.4.18-1.fc23    mailcap.noarch 2.1.45-1.fc23

Failed:
  httpd.x86_64 2.4.18-1.fc23

Complete!
[root@115d35aa4bcb /]# exit

Once the necessary updates and software are installed, we can commit these changes to a new images. That’s shown below…

root@ubuntu-demo:/home/cloud-user# docker commit -m "Adding httpd" -a "Wim Wauters" 115d35aa4bcb wiwa1978/fedora-httpd
5fa2fee12007b467c5eb08192ecc5cbe145621c5905e78f42610c519c86b54a4

root@ubuntu-demo:/home/cloud-user# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
wiwa1978/fedora-httpd   latest              5fa2fee12007        About a minute ago   573.3 MB
ubuntu                  latest              c9ea60d0b905        6 days ago           188 MB
centos                  latest              28e524afdd05        11 days ago          196.7 MB
fedora                  latest              9bdb5101e5fc        5 weeks ago          204.7 MB
training/webapp         latest              54bb4e8718e8        11 months ago        348.8 MB

So here you can see we are creating a local image called wiwa1978/fedora-http. Size is about 574MB (as opposed to the original size of the fedora image of 205MB).

You could then go ahead and run the image using the usual docker run command. Am sure you agree this process of committing changes is a bit cumbersome at times.

Check out the next post to see a better way of creating our own images.