How to move my application to Containers (Docker)?

Sainadh
Geek Culture
Published in
5 min readAug 25, 2021
Photo by Bernd Dittrich on Unsplash

In this blog, you will come to know about how to build container images, moving your applications to docker and various ways to deploy your container and using Github Actions.

If you are new to containers click_here

Introduction

Most of the people think that container and docker are same. Yes they are right only to some extent, when you deep dive into this world, you explore a lot.

Docker is only a container runtime engine which acts as a client-server application. Docker uses runC to do operations on the containers.

runC is a lightweight, portable container runtime. It includes all the low-level code which interacts with Linux OS.

Docker uses this technique to create the containers and operate them accordingly. As docker became so popular we are using the same tool throughout this blog.

Choosing the right container

When you want to move your application to a container, you should choose the container that can run programming language used for building the application.

There are lots of containers in docker hub, which is a container repository where you can store your container images. There are many official containers available. Also choose the language version.

# pull the container image from the docker hub
$ docker pull <container_name>:<tag>

Building custom container Image

For this we have to write a Dockerfile. This file is supported by many container runtime engines.

In this file we must write some basic commands.

FROM: We have to mention on which base container we are building.

RUN: Runs the commands inside the container.

ENV: For writing the environmental variables required by the application

COPY: To copy the files from local system to container.

ADD: It is similar to copy, used to copy the compressed files and extract them directly to the container.

WORKDIR: To set any directory as root directory for the container.

EXPOSE: It is useful to tunnel the internal port to the host. Used to expose the application to real world with port number of the application.

ENTRYPOINT: Which runs the command in the container when you start it.

CMD: It is similar to ENTRYPOINT, CMD can be changed when we create the container whereas ENTRYPOINT cannot be changed.

Note the filename of Dockerfile must be Dockerfile.

Here I am building a docker image with small application known as URL_SHORTNER which converts the long url to short.

Programming language used is python.

Pull the application

docker pull python:latest

Dockerfile

# building the Dockerfile
FROM python:latest
# creating a directory for the application
RUN mkdir URL_SHORTNER
# my dockerfile is in the same workspace as my application# copying the files from local system to container
COPY . URL_SHORTNER/
#setting my working directory
WORKDIR URL_SHORTNER
#installing the required packages
RUN pip3 install requirements.txt
#Exposing the application
EXPOSE 8080
# final command which run after container starts#here we write the command arguments in string format inside the list
CMD ["python3", "app.py"]

Commands to build the image

# inside the application workspace
$ docker build -t myapp:v1 .

If you are facing any issues after creating the container, check your container logs.

$ docker logs <container_name>

Now push the image to container repository.

There are lots of container repositories available in the market, few are Docker hub, Quay, AWS container repository …etc.

Here I am using docker hub

# login to the docker hub using docker command$ docker login

Enter your credentials for the above command.

To push the image to docker hub or any other container repo, it should be certain format.

<container_repo_url>/<username>/<your_container_name>:<tag>

Here container_repo_url is optional for docker hub.

Docker hub: docker.io

Quay: quay.io

In AWS it will be displayed when you create a repository.

Commands to push the image to repository.

#attaching tag$ docker tag sainadh086/url-shortner:v1 myapp:v1#push the image to repo
$ docker push sainadh086/url-shortner:v1

Introducing Automation with Github Actions.

Creating docker images again and again after adding new features to our code takes some long time.

As most of us are using version control tools like git and storing the code in Github. So, integrate your Github Repository with Github Actions.

Where it automatically builds the container and pushes to the repository.

There are some pre-build workflows also available from Github to integrate with different applications.

Writing Github Actions file.

name: CI #naming the pipelineon: # select the branch and request type
push: # selecting push command as the request
branches: [ master ] # branch
pull_request:
branches: [ master ]
jobs: #multiple stages to run
build: # it is one the stage named build
#selecting the os the instance to run our configuration file
runs-on: ubuntu-latest

# Multiple commands or tasks to run
steps:
- uses: actions/checkout@v2 - name: build a docker image
run: docker build -t infra:v1 .
- name: pushing the image to docker hub
#here we require login credentials and we cannot show to everyone.
# We use secrets feature provided by github.
# here we read our secrets and pass them to the commands
shell: bash #using bash terminal
env: #passing the secrets to environmental variables
user_name: ${{ secrets.DOCKER_USER_NAME }}
user_password: ${{ secrets.DOCKER_USER_PASSWORD }}you, guys

run: | #we use this symbol for writing multiple commands
docker login -u $user_name -p $user_password
docker tag infra:v1 sainadh086/url-shortner:latest
docker push sainadh086/url-shortner:latest
echo "Success"

Now you successfully build an application, then containerised the app and moved it to the Container Repository. Now it is ready to be deployed.

Choose your Cloud Computing platform of your choice and deploy the application.

In the next blog, you will come to know about deploying container application into the AWS cloud using terraform.

Conclusion

You have learned a bit about containers, moving your application into a container, moving your container image to docker hub and integrating Github Actions with Github.

If you have any doubts, please comment below. Let me know what type of content you want to read or any problem you are facing with devops tools.

Thanks everyone have good day.

--

--

Sainadh
Geek Culture

Devops and automation export, explorer, opensource enthusiast, follow for more content related to devops and easy way to do the things.