Skip to content

Guideline for creating a Docker container image, running the containerized application, and modifying and rebuilding the image after source code changes.

License

Notifications You must be signed in to change notification settings

nbahador/Generalized_Docker_Setup_Template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Running, Modifying, and Rebuilding a Containerized Application


Table of Contents

No. Topics
1 Repository Structure Template
2 Generalized Dockerfile Template
3 Dockerfile Example for Node.js Application
4 Build the Container Image
5 Run the Application
6 Updating the Source Code
7 Reference

Repository Structure Template

This is the general structure you should follow. Replace <application files> and <dependencies file> with the correct files for your application (e.g., app.py, index.js, pom.xml, etc.).

/app
  ├── Dockerfile            # Dockerfile for building the container image
  ├── .dockerignore         # (Optional) Files to exclude when building the image
  ├── <application files>   # Application source code (e.g., .py files, .js files, etc.)
  ├── <dependencies file>   # e.g., requirements.txt (Python), package.json (Node), pom.xml (Java), etc.
  └── README.md             # (Optional) Project description

Back to the topic in the Table ⬆


Generalized Dockerfile Template

Generalized Dockerfile Template with Example Comments

Step 1: Select an appropriate base image for your application

Below are examples for different programming environments:

  • Node.js:
    FROM node:14-alpine
  • Python:
    FROM python:3.8-slim
  • Java:
    FROM openjdk:11-jre-slim

Replace <base-image> with the appropriate base image for your application.


Step 2: Set the working directory inside the container

This is where your application files will reside within the container:

WORKDIR /app

Step 3: Copy the application source code into the container

This command copies all your application files into the working directory inside the container:

COPY . .

Step 4: Install application dependencies

Install the necessary dependencies for your application:

  • For Node.js:
    RUN npm install --production
  • For Python:
    RUN pip install -r requirements.txt
  • For Java (Maven example):
    RUN mvn install

Replace <dependency-install-command> with the appropriate command for your application (e.g., npm install, pip install, etc.).


Step 5: Specify the command to run your application once the container starts

This defines the entry point for your application. Below are examples for various programming languages:

  • Node.js:
    CMD ["node", "src/index.js"]
  • Python:
    CMD ["python", "src/app.py"]
  • Java:
    CMD ["java", "-jar", "target/app.jar"]

Replace <start-command> with the appropriate start command for your application.

Back to the topic in the Table ⬆


Dockerfile Example for Node.js Application

Here’s what the Dockerfile would look like for a Node.js app.

# Step 1: Use Node.js as the base image
FROM node:14-alpine

# Step 2: Set the working directory
WORKDIR /app

# Step 3: Copy all application files to the container
COPY . .

# Step 4: Install Node.js dependencies
RUN npm install --production

# Step 5: Start the Node.js application
CMD ["node", "src/index.js"]

Back to the topic in the Table ⬆


Build the Container Image

Build the container image using the docker build command. This command used the Dockerfile to build a new container image.

docker build -t docker-101 .

Back to the topic in the Table ⬆


Run the Application

Now that there's an image, let's run the application! To do so, use the docker run command. Start the container using the docker run command:

docker run -dp 3000:3000 docker-101

Using the -d and -p flags, the container runs in detached mode. With port 3000 mapped from host to container.

http://localhost:3000

Back to the topic in the Table ⬆


Updating the Source Code

After applying the changes to the source code, get the ID of the container by using the docker ps command.

docker ps

Use the docker stop command to stop the container with the ID from docker ps.

docker stop <the-container-id>

Once the container has stopped, you can remove it by using the docker rm command.

docker rm <the-container-id>

Now, start your updated app.

docker run -dp 3000:3000 docker-101

Back to the topic in the Table ⬆


Reference

All content in this repository is a summary of what I’ve learned from various tutorials provided in Play with Docker.

Download the sample Node-based application from Play with Docker.

Back to the topic in the Table ⬆


About

Guideline for creating a Docker container image, running the containerized application, and modifying and rebuilding the image after source code changes.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published