# Day 12: Docker commands, Docker file & Jenkins on Docker container

Hello everyone!

In the last article, we learned the concepts of Docker. Today let's explore more about Docker commands and run Jenkins on an actual Docker container.

## Useful Docker commands

## **Basic**

1. docker version
    
2. docker -v
    
3. docker info
    
4. docker --help
    
5. docker login
    
    ### **Images**
    
6. docker images
    
7. docker pull
    
8. docker rmi : remove images
    
    ### Containers
    
    1. docker ps ::: shows all running containers
        
    2. docker ps -a ::: show all the hidden containers
        
    3. docker run ::: runs container with container id and tags like 'it'
        
    4. docker run -it ::: (when image is not there, pull directly and run)
        
    5. docker start
        
    6. docker stop
        
    7. docker attach ::: Go inside of container
        
    8. docker kill ::: Kills the running container
        
    9. docker rm ::: deletes the running container
        

### System

1. docker stats
    
2. docker system df disc memory
    
3. docker system prune: deletes all images with '-a' deletes all containers and files which are not used.
    

# Dockerfile

Docker is a tool that makes it easy to run applications in containers. Containers are like small packages that hold everything an application needs to run. To create these containers, developers use something called a Dockerfile.

A Dockerfile is like a set of instructions for making a container. It tells Docker what base image to use, what commands to run, and what files to include. For example, if you were making a container for a website, the Dockerfile might tell Docker to use an official web server image, copy the files for your website into the container, and start the web server when the container starts.

Here are some of the most commonly used commands in a Dockerfile:

* `FROM`: Specifies the base image that the new image will be built on top of. For example, you might use an official Node.js image as the base for an application that runs on Node.js.
    
* `RUN`: Executes a command in the image. This command is run during the image build process. For example, you might use the `RUN` command to install necessary packages or dependencies for your application.
    
* `COPY`: Copies files from the host machine to the image. For example, you might use the `COPY` command to copy the files for your application into the image.
    
* `ENV`: Sets an environment variable in the image. For example, you might use the `ENV` command to set a variable that holds the version of your application.
    
* `EXPOSE`: Specifies the ports that should be exposed on the container. For example, you might use the `EXPOSE` command to specify that port 8000 should be exposed on the container.
    
* `CMD`: Specifies the command that should be run when a container is created from the image. For example, you might use the `CMD` command to specify that your application should be started when the container is created.
    

[Examples of Docker file](https://docs.docker.com/engine/reference/builder/#dockerfile-examples)

### Let's build a container

1. First, we need to have Docker installed on our AWS instance.
    
2. `sudo yum install docker`
    
    ![Install Docker on RHEL 7 using yum command](https://www.cyberciti.biz/media/new/faq/2018/01/Install-Docker-on-RHEL-7-using-yum-command.jpg align="left")
    

Once Docker is installed and running, we are ready to pull Jenkins image from the Docker hub and create a container.

* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691253005992/b52eac52-f333-48b1-aede-269302f18412.png align="center")
    
    Go to the Docker hub and pull the image using the given command.
    
    `docker pull jenkins/jenkins`
    
* Check with the below command whether the image is pulled successfully or not.
    
    `docker images`
    
* Run this command to initiate the container from the docker image.
    
    ```plaintext
    docker run -p 8080:8080 -p 50000:50000 --restart=on-failure -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts-jdk11
    ```
    
* Check the status of the container using
    
    `docker ps-a`
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691253424840/9e00502c-f7dc-41af-b332-d49ed1e6ab13.png align="center")
    
* **Note -** Don't forget to save the login key which it shows while creating a container to login into the Jenkins portal.
    
* Search the server's IP address with port 8080 in the browser to access Jenkins portal. For example:
    
    `196.34.87.94:8080`
    
* It will open a login window, and enter the password shown while creating the container.
    
    Voila! You will be redirected to Jenkins.
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691253894198/dfb6a18b-5fbe-46f8-ae94-82b1f66f61ba.png align="center")
    

### Some troubles which I encountered in the process

* While trying to access Jenkins by `196.34.87.94:8080`
    
    I faced the following error :
    
    **This site can’t be reached** [**localhost**](http://localhost) **refused to connect Error?**
    
* I found out that there was already a service running on port 8080 by using the below cmd
    
    `sudo netstat -pna | grep 8080`
    
* Remove the service from running on port 8080 by using these commands.
    
    `sudo lsof -i TCP:8080`
    
    `sudo fuser -k 8080/tcp`
    
* Try to access `196.34.87.94:8080`
    
* If this doesn't work try to re-create the container.
    

**References**:

[More information on Docker](https://www.youtube.com/watch?v=LQjaJINkQXY&t=180s&ab_channel=AutomationStepbyStep)
