Skip to main content

Build a container

https://www.youtube.com/watch?v=SnSH8Ht3MIc


Some lightweight Linux distributions commonly used in Docker containers include Alpine Linux, BusyBox, and Scratch. Alpine Linux is a popular choice due to its minimal size and package management system. Here's how you can create a Docker container using Alpine Linux:
1. **Create a Dockerfile**: Create a Dockerfile similar to the one shown below. This example uses Alpine Linux as the base image:
```Dockerfile

Use Alpine Linux as the base image

FROM alpine:latest

Install any necessary dependencies using the Alpine package manager (apk)

RUN apk update && apk add your-dependency-package

Copy your application files into the container

COPY your-application /path/to/destination/in/container

Set the working directory

WORKDIR /path/to/destination/in/container

Define the command to run your application

CMD ["/path/to/destination/in/container/your-application"]
```
Replace `your-dependency-package`, `your-application`, and the destination paths with your specific dependencies and application details.
2. **Build the Docker Image**: Navigate to the directory containing your Dockerfile and execute the following command to build the Docker image using Alpine Linux as the base image:
```bash
docker build -t my-app-image .
```
Replace `my-app-image` with your chosen image name.
3. **Run the Docker Container**: To run the Docker container using the Alpine-based image, use the following command:
```bash
docker run -d my-app-image
```
The container will use Alpine Linux as the base image, which is much lighter than Ubuntu.
Alpine Linux is known for its minimalism and small footprint, making it an excellent choice for lightweight Docker containers. You can explore other lightweight base images, such as BusyBox or Scratch, based on your specific needs and container size requirements. Keep in mind that using smaller base images may require more manual configuration of dependencies and environment setup compared to using a more feature-rich base image like Ubuntu.

To deploy a Docker container with your image and expose it on port 5234, you can use the -p (or --publish) option when running the container. Here's how you can do it:

Assuming you have already built your Docker image as mentioned earlier, you can deploy and expose the container on port 5234 using the following command:

docker run -d -p 5234:5234 my-app-image

In this command:

  • -d runs the container in detached mode (in the background).

  • -p 5234:5234 specifies that you want to map port 5234 from the host to port 5234 inside the container. This means that any traffic directed to port 5234 on your host will be forwarded to the container.

Replace my-app-image with the name of your Docker image.

Now, your application running inside the Docker container should be accessible on port 5234 of your host machine. You can access it using a web browser or other tools by navigating to http://localhost:5234 or http://<your_host_ip>:5234, where <your_host_ip> is the IP address of your host machine.

Make sure that your application inside the Docker container is configured to listen on port 5234 for incoming requests. Additionally, if you need to use a different host port to expose the application (e.g., if port 5234 on your host is already in use), you can modify the -p option accordingly, like -p 8080:5234 to expose it on port 8080 of your host.