🐳Docker

In this section we will install docker, create images, build containers and much more. Let's Set SAIL!

To install Docker run the following commands:

sudo yum install docker -y
usermod -aG docker ec2-user // to provide permission to ec2-user
sudo su ec2-user // login as ec2-user
cd ~ // navigate to ~
sudo service docker start // to start docker

Now we have installed and configured docker perfectly, we can run docker ps command to see list of containers right now we do not have any containers. Lets build one container and install Tomcat server on it.

Create Docker Container

Before we go ahead and run customers lets learn few common docker commands:

docker –version
//This command is used to get the currently installed version of docker
 

docker pull
Usage: docker pull <image name>
This command is used to pull images from the docker repository(hub.docker.com)


docker run
Usage: docker run -it -d <image name>
//This command is used to create a container from an image


docker ps
//This command is used to list the running containers


docker ps -a
//This command is used to show all the running and exited containers


docker exec
Usage: docker exec -it <container id> bash
//This command is used to access the running container


docker stop
Usage: docker stop <container id>
//This command stops a running container


docker kill
Usage: docker kill <container id>
//This command kills the container by stopping its execution immediately. The difference between ‘docker kill’ and ‘docker stop’ is that ‘docker stop’ gives the container time to shutdown gracefully, in situations when it is taking too much time for getting the container to stop, one can opt to kill it


docker commit
Usage: docker commit <conatainer id> <username/imagename>
//This command creates a new image of an edited container on the local system

docker_commit - Docker Commands - Edureka

Now lets run our first docker command to pull Tomcat image from DOCKERHUB

docker pull tomcat:latest

Now we have Tomcat image ready let's build a container from this image and run it.

COMMANDS:

sudo docker run -d -p 8080:8080 --name tomcat <image id> //to check image ID run "docker images"
docker ps // to see newly created running container
docker exec -it <container_name> /bin/bash  // to login to container

If you wish to delete docker container first stop the container by running docker stop <Container ID> and delete by running docker rm <container id> Npw if you run docker ps -a , you will see the container is deleted. dokcer rmi <image ID> to delete image

Now you are logged into container. If you ls into your container you will see different directories but you only need to focus on these we have to put our build file in webapp/ directory but all the files required for build file to run are stored in webapp.dist

So we will copy contents of webapp.dist in webapp by running :

cp -r ./* /usr/local/tomcat/webapps

Now we have everything except the build file, But where is BUILD FILE?!! If you have been here from the start you might know that Jenkins from another server transferred our file to this server we are running our tomcat server. So we need to COPY build file from Host server >> Tomcat server>> webapps folder. Follow the steps:

  • Exit the container by typing exit in the terminal. Now you returned back to Server where we have docker installed with tomcat running on it.

  • If we ls we can see webapp.war which we have to copy to tomcat container>>webapp

  • run sudo docker cp webapp.war tomcat:/usr/local/tomcat/webapps/webapp.war

  • Now if you paste your IP:8080 on your browser you should see tomcat page

  • And if you access IP:8080/webapp/ you can see your website that you created and stored on Git.

Congratulations !! We have successfully deployed build file received by JENKINS server (through GITHUB code) on DOCKER >> Tomcat Server. I am so incredibly proud of you.

We did a lot of manual task like pulling image, building and runnning containers and copying war file from host to container but worry not we can automate all this hefty task by creating a DOCKERFILE in next section.

Last updated