40 Days Of Kubernetes (2/40)

Day 2/40

How to Dockerize a Project

Install Docker Desktop

To get started with Docker on your local machine, download the Docker Desktop client:

Docker Desktop Download

Getting Started with Docker

Clone…


This content originally appeared on DEV Community and was authored by Subham Nandi

Day 2/40

How to Dockerize a Project

Install Docker Desktop

To get started with Docker on your local machine, download the Docker Desktop client:

Getting Started with Docker

  1. Clone a Sample Repository: You can either use your own project or clone a sample repository using the following command:
   git clone https://github.com/docker/getting-started-app.git
  1. Navigate to the Project Directory: Change your working directory to the project folder:
   cd getting-started-app/
  1. Create a Dockerfile: Inside the project directory, create a new file named Dockerfile:
   touch Dockerfile
  1. Add Dockerfile Content: Open the Dockerfile in your preferred text editor and paste the following content:
   FROM node:18-alpine
   WORKDIR /app
   COPY . .
   RUN yarn install --production
   CMD ["node", "src/index.js"]
   EXPOSE 3000

This Dockerfile sets up a Node.js environment, installs dependencies, and specifies how to run the application.

  1. Build the Docker Image: Build the Docker image from the Dockerfile using the following command:
   docker build -t day02-todo .
  1. Verify the Docker Image: After building, check that the image was created and stored locally:
   docker images

Pushing the Docker Image to Docker Hub

  1. Log In to Docker Hub: Authenticate with Docker Hub to push your image:
   docker login
  1. Tag the Image: Tag your image to prepare it for pushing to a remote repository:
   docker tag day02-todo:latest username/new-reponame:tagname
  1. Push the Image: Push the tagged image to Docker Hub:
   docker push username/new-reponame:tagname

Pulling and Running the Docker Image

  1. Pull the Image in Another Environment: If you need to pull the image to a different environment, use:
   docker pull username/new-reponame:tagname
  1. Run the Docker Container: Start the container and map the application to port 3000:
   docker run -dp 3000:3000 --name docker-container-name username/new-reponame:tagname

After this, your app should be accessible at http://localhost:3000.

Additional Docker Commands

  • Access the Container Shell: To enter the running container, use:
  docker exec -it containername sh

or

  docker exec -it containerid sh
  • View Container Logs: To check the logs from your container:
  docker logs containername

or

  docker logs containerid


This content originally appeared on DEV Community and was authored by Subham Nandi


Print Share Comment Cite Upload Translate Updates
APA

Subham Nandi | Sciencx (2024-08-15T12:44:17+00:00) 40 Days Of Kubernetes (2/40). Retrieved from https://www.scien.cx/2024/08/15/40-days-of-kubernetes-2-40/

MLA
" » 40 Days Of Kubernetes (2/40)." Subham Nandi | Sciencx - Thursday August 15, 2024, https://www.scien.cx/2024/08/15/40-days-of-kubernetes-2-40/
HARVARD
Subham Nandi | Sciencx Thursday August 15, 2024 » 40 Days Of Kubernetes (2/40)., viewed ,<https://www.scien.cx/2024/08/15/40-days-of-kubernetes-2-40/>
VANCOUVER
Subham Nandi | Sciencx - » 40 Days Of Kubernetes (2/40). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/15/40-days-of-kubernetes-2-40/
CHICAGO
" » 40 Days Of Kubernetes (2/40)." Subham Nandi | Sciencx - Accessed . https://www.scien.cx/2024/08/15/40-days-of-kubernetes-2-40/
IEEE
" » 40 Days Of Kubernetes (2/40)." Subham Nandi | Sciencx [Online]. Available: https://www.scien.cx/2024/08/15/40-days-of-kubernetes-2-40/. [Accessed: ]
rf:citation
» 40 Days Of Kubernetes (2/40) | Subham Nandi | Sciencx | https://www.scien.cx/2024/08/15/40-days-of-kubernetes-2-40/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.