Last update: 2019-11-22

Publishing your first Docker container


Docker is a great way to deploy micro services, or any application that can be self-contained and is used for a specific task. I've been deploying Docker containers for a while now, along with managing them, but I had never created a container from scratch. I decided to change that and create a container for my Healthstone Monitoring System app, and I'll bring you along to show how easy it can be to convert a traditional app into a Docker container, then publish it for anyone to use.

Setting up Docker

The first thing to do is install and configure Docker. I'm using a CentOS Linux VM and you can see this article on how to set it up. Basically, these are the commands to run:

<bash>
yum install yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce
systemctl start docker
systemctl enable docker
docker login

Once done, you should have the Docker service running on your local machine, and be ready to create images.

Creating the Dockerfile

In order to create a container, you need to create a file with the name Dockerfile which contains the instructions for how the container should be made. So after downloading the Healthstone app to the current folder, I wrote the following file:

FROM centos:latest
USER root
COPY ./healthstone.zip /root/healthstone.zip
RUN yum update -y
RUN yum install -y httpd unzip python3 crontabs
RUN unzip /root/healthstone.zip -d /usr/share
RUN chown -R apache.apache /usr/share/healthstone/db
RUN chmod g+s /usr/share/healthstone/db
RUN chmod 755 /usr/share/healthstone/www/dashboard.py
RUN echo "*/1 * * * * /usr/share/healthstone/www/dashboard.py > /dev/null" >> /tmp/mycron && crontab /tmp/mycron
RUN ln -s /usr/share/healthstone/www /var/www/html/healthstone
RUN sed -i.bak '/AllowOverride None/d' /etc/httpd/conf/httpd.conf
RUN echo "<Directory />" >> /etc/httpd/conf/httpd.conf
RUN echo " AllowOverride All" >> /etc/httpd/conf/httpd.conf
RUN echo " Options FollowSymLinks" >> /etc/httpd/conf/httpd.conf
RUN echo " Require all granted" >> /etc/httpd/conf/httpd.conf
RUN echo "</Directory>" >> /etc/httpd/conf/httpd.conf
RUN echo "<html><meta http-equiv='refresh' content='0;url=/healthstone'/></html>" > /var/www/html/index.html
EXPOSE 80
CMD ["-D", "FOREGROUND"]
ENTRYPOINT ["/usr/sbin/httpd"]

This may seem like a lot, but let's go through each line. The first line instructs Docker to use the existing centos image from the Docker Hub, and pull the latest version of it. This will be our base, or first layer, for the container. Then, we specify that all the commands must be run as root, and we copy the package containing Healthstone. After that, there's a series of RUN commands which are simply bash shell commands that will be run inside the container. These would be specific to whichever application you're working with, but in my case I'm installing Apache, Python and Crontab, unzipping the package in /usr/share/healthstone, setting some folder permissions, creating symbolic links, and setting up some Apache configuration options.

Finally, at the end of the file, I tell Docker that port 80 should be exposed, and I end with the entry point, which is the command that should be run when the container is started. In this case it's Apache.

Building the container

To build the container, we use the build command along with the name we want for the container, and the folder where our Dockerfile is at, in this case the current folder:

<bash>
docker build -t healthstone ./ --no-cache

This will go through each step we specified above and if there's any error, we will see it here:

Once done, we can then run our container to test it out:

<bash>
docker run -p 80:80 healthstone

Publishing the container

Tagging and publishing the container requires you to have a Docker Hub account, and the name you give it depends on your login name and the name you want to give to your container, along with the tag name (usually the version). In my case, this is how I tagged it:

<bash>
docker tag healthstone dendory02/healthstone:2.1.8

Finally, you can push the tagged container to the Hub:

<bash>
docker push dendory02/healthstone:2.1.8

That's all there is to it. If you login to your Hub account, you should see your newly published container and you can add a description for it.


Looking to expand into the cloud? See what a certified AWS administrator can do for you.


 
© 2007-2019 Patrick Lambert - All resources on this site are provided under the MIT License - You can contact me at: contact@dendory.net