Skip to main content

Command Palette

Search for a command to run...

Simple docker Project

Updated
3 min read

Task:

Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)

Steps:

  1. Launch AWS ubuntu ec2 instance.

  2. Open the security group of the instance and add a new inbound rule to allow all the IP addresses and ports to access the application.

    1. Install docker

      ⦿ Manual installation: Docker can be installed by following the instructions over here : Manual instructions

      ⦿ script-based installation: Script
      Run the below two comments:
      curl -fsSL get.docker.com -o get-docker.sh
      or wget -O get-docker.sh https://get.docker.dom

      sh get-docker.sh

      1. Docker allows communication to the Unix socket for the users who belong to the docker group. so let's add the current user to the docker group sudo usermod -aG docker <username>. Then logout and login.

        1. Clone the repository from Github to ubuntu server using below command:

          git clone <Repo_URL>

          1. Create a docker file

            vim Dockerfile

          2. The specific commands you can use in a dockerfile are:

            • FROM: The first thing we need to do is define from which image we want to build from. Creates a layer for Jar file

            • Next we create a directory to hold the application code inside the image, this will be the working directory for your application. WORKDIR a command is used to define the working directory of a Docker container at any given time.

            • COPY is a dockerfile command that copies files from a local source location to a destination in the Docker container.

            • RUN command to execute the command SPC and Builds your container

            • EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime.

            • CMD Specifies what command we want to run when our image is run inside of a container.

              To build an image using Dockerfile, Go to the directory that has your Dockerfile and run the following command:

              docker build -t <image-name> .

          3. Build the image using the Dockerfile and run the container

            Image created, now can create container using command-

            docker run -d - -name <container-name> -p <image-name>

            Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port inside the container and - - name flag for assigning name to docker container.

            Command to delete all running containers forcefully : docker container rm -f $(docker container ls -q )

          4. Once the container is created run the application in the laptop

            1. Push the image to a public or private repository (e.g. Docker Hub )

Use command “docker login” to login to dockerhub.

Push docker image to docker hub using command:

  • docker push <image-name>