Hello, I think you read somewhere, something about docker and you find this blog and you opened up to skim through. You are at right place. So, let’s not waste our time and let’s dockerize your world. In this blog, You will not just learn about docker. I will also tell you how to get started and create docker container and run them on the elastic beanstalk in the later post. Building and running it on elastic beanstalk will give much more confidence and idea about why you should start using docker for your every project.
I always try to come up with hands-on tutorials because the problem with the only introduction to docker, It will really not serve the purpose. So, First I will explain you about Docker and how you can use docker for everything from development to deployment.
what is docker?
Package software into standardized units for development, shipment and deployment
Docker is really just a software to make and run container but the advantage of docker is it really works for every requirement possible from small to the large-scale enterprise application.
And if we define container image, it is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, run-time, system tools, system libraries, settings. Available for both Linux and Windows-based apps, the containerised software will always run the same, regardless of the environment. Containers isolate software from its surroundings, such as, differences between development and staging environments and help reduce conflicts between teams running different software on the same infrastructure.
Yeah, that was official definition from docker. Now, Let’s break it down and understand it. First, let me make it clear that running docker is not same as the running virtual machine. Docker is just way to package your application code with everything needed from pip packages to system libraries to config files for supervisor. So, It is guaranteed that it will run same on every machine which supports docker.
Many organisations is using container technology but docker is first one that gets so much buzz because of its easiness to configure and run it.
Key advantages are as follows:
- Main ability of Docker is to cut the size of development by providing a smaller footprint of the operating system via containers.
- With containers, it becomes easier for teams across different units, such as development, QA and Operations to work seamlessly across applications. It also removes “works on my machine” problem from the team because container image will be same across all environment whether it’s a dev, staging or production.
- You can deploy Docker containers anywhere, on any physical and virtual machines and even on the cloud. This provides a great benefit to scale application very easily.
- Since Docker containers are pretty lightweight, they are very easily scalable because we just have to provide container image file and it will run it.
- Also, It is less resource consuming because as you can see in feature image it is running on the same kernel. Internally, It is only the isolation of processes using low-level Linux kernel API: namespaces and cgroups which is there in the kernel for a long time. Now, We are really taking advantage of it.
Starter . . .
first, let’s install docker on your machine. I will not write all steps here but you can run the following script and it will install everything you needed. It will also install eb-cli which we are going to use to run Docker in the AWS cloud.
You can also follow official tutorial available.
You can verify the docker properly install or not buy running following command.
docker run hello-world
Don’t forget to sudo if it’s giving permission error.
Now, we can start to develop and run Docker containers. we are going to build basic flask application and run it on AWS elastic beanstalk docker environment. I will not provide my GitHub code link because you will just copy paste commands to run it without experimenting with it. 😉
Install flask package:
pip install flask
Build the Hello-Docker app
Now let’s build the hello-world flask app. Copy and paste following code into the file
app.py after creating the new folder.
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Flask Dockerized' if __name__ == '__main__': app.run(debug=True,host='0.0.0.0')
Now, Run following command to run flask locally.
If you receive following output we can move forward to build docker container of flask application.
Now, Create the new file and name it
Dockerfile . It is basically recipe file for docker engine what it has to do in order to run your application. Let’s first copy-paste following lines in
FROM ubuntu:latest MAINTAINER Aniket Patel "firstname.lastname@example.org" RUN apt-get update -y RUN apt-get install -y python-pip python-dev build-essential COPY . /app WORKDIR /app RUN pip install flask ENTRYPOINT ["python"] CMD ["app.py"] EXPOSE 5000
- you can see here that we are using
RUNto run command we ran in our terminal.
- You will also see that we use python as
ENTRYPOINTand we parse
CMDto run that flask app.
- you can see here that we are using
- we define expose port which will proxied to the outer layer. So, We can use our application.
Build the Docker Image
Now, Run the following command to build docker image locally. You will see something like the image below. It can take five minutes or more build Docker image for the first time.
docker build -t hello-docker:latest .
Run the Docker container locally
$ docker run -d -p 5000:5000 hello-docker
Now, what we did is we provide the same name to docker the same name as we provided while building the image. We are telling docker to map container 5000 port to the operating system we are running on. So now, If you visit the localhost:5000 you will get the same result you got while running flask locally.
Congratulations, You build your first docker image and successfully run it. But still, you have to understand we are going through this whole process just to run the simple application we can run easily on the operating system and to understand that you can follow next tutorial on introduction to elastic beanstalk.