DEV Community

@kon_yu
@kon_yu

Posted on

How to register DockerImage to DockerHub

Introduction.

I didn't have an image of Ruby 2.3.1 running on CentOS with Docker, so I made this.


It says something like this

  • How to install Docker for mac
  • How do I create a Dockerfile?
  • Install using a Dockerfile created in the local environment
  • Registering a Dockerfile to the DockerHub
  • Registering a Dockerfile with DockerHub and Github

Install Docker for mac

https://docs.docker.com/docker-for-mac/.
Hit the Get Docker for Mac button to download the DMG file.
Double-click on the downloaded file to install it.

It takes quite a long time to install, and if you have an existing Docker, it takes even longer because of the migration process.

If you have installed from Docker Toolbox, you need to remove the old Docker because it consumes almost the same size of disk space when you migrate.

Here's how to remove it: http://qiita.com/kon_yu/items/457c9652d0de3f64c31f


Create a Dockerfile

# set base OS image
FROM centos:latest

# set Ruby installed Dir
ENV RUBY_DIR /ruby/
ENV RUBY_VERSION 2.3.1
ENV RUBY_INSTALL $RUBY_DIR/$RUBY_VERSION

# install packeges for installing Ruby
RUN yum update -y &&\frzessor
    yum install -y install epel-release && \fnDroid
    yum install -y bzip2 make which wget tar git nodejs \frzessor
    gcc patch readline-devel zlib-devel \b1}
    libyaml-devel openssl-devel \frzero
    gdbm-devel ncurses-devel libxml-devel
RUN yum clean all

# install Ruby with ruby-build
RUN mkdir $RUBY_DIR &&\fnDroid
    cd $RUBY_DIR && \fnDroid
    git clone https://github.com/sstephenson/ruby-build.git && \fnDroid
    $RUBY_DIR/ruby-build/install.sh &&\frz
    cd $RUBY_DIR/ruby-build && . /bin/ruby-build $RUBY_VERSION $RUBY_INSTALL && \fnDroid
    rm -rf $RUBY_DIR/ruby-build
ENV PATH $RUBY_INSTALL/bin:$PATH
Enter fullscreen mode Exit fullscreen mode

Create a Docker Image from a Dockerfile

The image name is given with the -t option.
If you don't use your account name/image name, you can't upload it to DockerHub.

Go to the directory where the Dockerfile is and run the following command (don't forget the period at the end)

> docker build -t konyu/centos_ruby .
Enter fullscreen mode Exit fullscreen mode

It takes a lot of time, so be careful.

Make sure the Docker image is made

If you have a repository called centos_ruby, it works

> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
konyu/centos_ruby latest 78ed661736e3 3 minutes ago 550.4 MB
Enter fullscreen mode Exit fullscreen mode

Create a container from a Docker image

Launching a container with an image

> docker run -it --rm centos_ruby bash
Enter fullscreen mode Exit fullscreen mode

-it container startup, login with shell at startup
--rm Discard the container when it is exiting the container


Check Ruby version and OS version

> ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
> cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
Enter fullscreen mode Exit fullscreen mode

Subscribe to DockerHub

Register an account in DockerHub
https://hub.docker.com/

There are two ways to push an image from here locally, and one that works with Github.
Share each method.
I recommend the Github collaboration described later.

How to push an image from the local

Logging into DockerHub from the command line

> docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: Account name
Password: Password
Login Succeeded
Enter fullscreen mode Exit fullscreen mode

Pushing an image to DockerHub

> docker push konyu/centos_ruby:latest
The push refers to a repository [docker.io/konyu/centos_ruby]
c789971294c7: Pushed
94e172a9994f: Pushed
31fa1a4603e2: Pushed
0fe55794a0f7: Pushed
latest: digest: sha256:c3ea799950c079f50b67a7ba570618cc1784c17e8c4f5e87e5f210d78f74dc1e size: 1163
Enter fullscreen mode Exit fullscreen mode

! kobito.1469590120.457475.png


Working with Github (recommended!)

Hosting the target Dockerfile on Github
Registering a repository with Dockerfile on Github
https://github.com/konyu/centos7-ruby23


Go to DockerHub and press the "Create automated build" button.

Screenshot 2016-07-27 12.33.13.png


Choose a repository that works with Github and works with DockerHub

Select "Build Settings" from the target DockerHub repository and press the Trigger button below

Screenshot 2016-07-27 12.49.10.png


If you hit the Build Details tab, you'll see that the Dockerfile has been compiled
kobito.1469596464.937349.png


Let's use the DockerImage from DockerHub

Run docker run with the image name in DockerHub.

docker run --rm -it konyu/centos7-ruby23
Unable to find image 'konyu/centos7-ruby23:latest' locally
latest: Pulling from konyu/centos7-ruby23
8c3d77a518cb: Already exists
60683d059310: Pull complete
908e534a605e: Pull complete
ddd64406a2ab: Pull complete
Digest: sha256:5d616e0a1494aba10ff8dc3cca5b769ea659aeb1f6034c1bc829052dd5495c06
Status: Downloaded newer image for konyu/centos7-ruby23:latest
[root@b922eecdd73d /]# ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
Enter fullscreen mode Exit fullscreen mode

I'm hooked.

CentOS core was too minimal and I got hooked

  • I couldn't upload a file with paperclip unless I installed the file command.
  • No crond.

Top comments (0)