docker

What is docker?

Docker is a platform which packages an application and all its dependencies together in the form of containers. This containerization aspect ensures that the application works in any environment.

As you can see in the diagram, each and every application runs on separate containers and has its own set of dependencies & libraries. This makes sure that each application is independent of other applications, giving developers surety that they can build applications that will not interfere with one another.

So a developer can build a container having different applications installed on it and give it to the QA team. Then the QA team would only need to run the container to replicate the developer’s environment.

Creating a docker image from Dockerfile, executing the image in a container and running the app

One way a docker image is created is by creating a Dockerfile, executing a set of shell/ Docker commands in that file and building your first image. The way that this is done is as follows;

Dockerfile

# First of all you have the Dockerfile, this might look like this. This will build your image
# of a specific app

FROM alpine:latest
WORKDIR /app
COPY . /app
EXPOSE 80
EXPOSE 443
CMD ["/app/main"]

Create/ execute docker image

# -- BUILDING THE IMAGE
# Once you have the Dockerfile in your work directory, you can build the image
# the way that this is done is as follows;
docker build -t '<IMAGENAME>' . # <IMAGENAME> is the name/tag of the image. the dot (.) represents the current directory/ directory where the Dockerfile resides in that is being build

# once the image has been succesfully build, check it out with the command
docker image ls  

root@docker:/home/corrosie814# docker image ls

REPOSITORY        TAG       IMAGE ID       CREATED       SIZE
portfolio         latest    7a1741fasf84   4 days ago    52.6MB
alpine            latest    d4ff818577bc   7 weeks ago   5.6MB

# the apps will show up, one contains a basic alpine image (which is being your container host OS, the other is your app)
#
# -- RUNNING THE IMAGE
# In order to run the image, in this case we have to expose port 80 and 443 in order to expose it, execute the following command
docker run -d -p 80:80 -p 443:443 portfolio

Last updated