Skip to content

Commit

Permalink
Merge pull request #185 from ClubCedille/wiki_docker
Browse files Browse the repository at this point in the history
New Docker wiki documentation
  • Loading branch information
andrei22131 authored Jul 19, 2024
2 parents a2bfe7f + c663c39 commit 5423628
Show file tree
Hide file tree
Showing 32 changed files with 1,725 additions and 1 deletion.
53 changes: 53 additions & 0 deletions wiki/docs/en/learn-docker/bind-mounts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Docker isolates all content, code, and data within a container from your local filesystem. Sometimes, however, you may want the container to access a directory on your system. This is where bind mounts come in handy.

## Using Bind Mounts to Access Local Folders

If you want to access data on your system, you can use a bind mount. A bind mount lets you share a directory from your host's filesystem into the container, allowing the container to access and modify files in that directory.

### Example: Adding a Bind Mount to a Docker Compose Project

We will use the repository at [https://github.com/docker/bindmount-apps](https://github.com/docker/bindmount-apps) to demonstrate how to add a bind mount to a Docker Compose project.

**1. Clone the Repository:**

```
git clone https://github.com/docker/bindmount-apps
cd bindmount-apps
```

**2. Modify the `compose.yaml` File:**

To add a bind mount to this project, open the `compose.yaml` file and uncomment the following lines:

```yaml
services:
todo-app:
# ...
volumes:
- ./app:/usr/src/app
- /usr/src/app/node_modules
```
**Explanation:**
- The `volumes` element tells Compose to mount the local folder `./app` to `/usr/src/app` in the container for the `todo-app` service. This particular bind mount overwrites the static contents of the `/usr/src/app` directory in the container and creates what is known as a development container.
- The second instruction, `/usr/src/app/node_modules`, prevents the bind mount from overwriting the container's `node_modules` directory to preserve the packages installed in the container.

**3. Run the Docker Compose Application:**

Open a terminal in your project directory and run the following command:
```
docker-compose up -d
```

**Explanation:** This command will start the application with the configured bind mount. Docker will mount the `./app` directory from your host to `/usr/src/app` in the container.

**4. Developing with Bind Mounts:**

Now, you can take advantage of the container’s environment while you develop the app on your local system. Any changes you make to the `app` directory on your local system are reflected in the container.

For example, in your local directory, open `app/views/todos.ejs` in an IDE or text editor, update the `Enter your task` string, and save the file. Visit or refresh `http://localhost:3001` to see the changes.

## Summary

Bind mounts are useful for development environments where you need to interact with the container and your local filesystem simultaneously. They allow you to edit files on your host system and have those changes reflected inside the container in real-time. This makes it easier to develop and test your application without the need to rebuild the Docker image after every change.
145 changes: 145 additions & 0 deletions wiki/docs/en/learn-docker/container-registry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
## Working with Docker Hub

[Docker Hub](https://hub.docker.com/) is a cloud-based repository where Docker users and partners create, test, store, and distribute container images. It is the world's largest library and community for container images. In this guide, we'll show you how to interact with Docker Hub, including how to pull and push images, and highlight some of the useful features available on Docker Hub.

### Setting Up Docker Hub

**1. Create a Docker Hub Account:**

Before you can use Docker Hub, you'll need to create an account. Visit the [Docker Hub website](https://hub.docker.com/) and sign up for a free account.

**2. Log In to Docker Hub:**

Open your terminal and log in to Docker Hub using your account credentials:
```
docker login
```

You will be prompted to enter your Docker Hub username and password. Once authenticated, you can start interacting with Docker Hub.

### Pulling Images from Docker Hub

Docker Hub hosts a vast number of pre-built container images that you can use in your projects. To pull an image from Docker Hub, use the `docker pull` command.

**1. Search for an Image:**

To find an image, you can search Docker Hub using the following command:
```
docker search <image-name>
```

For example, to search for a Node.js image, you would use:
```
docker search node
```

This command lists available images with the name "node" in Docker Hub.

**2. Pull an Image:**

Once you find the image you need, you can pull it to your local machine:
```
docker pull <image-name>
```

For example, to pull the official Node.js image, you would use:
```
docker pull node
```

This command downloads the image from Docker Hub to your local system.

**3. Run the Pulled Image:**

After pulling the image, you can run it using the `docker run` command:
```
docker run -d -p 3000:3000 node
```

This command starts a container from the Node.js image and maps port 3000 on your local machine to port 3000 in the container.

### Pushing Images to Docker Hub

If you have created a custom Docker image that you want to share with others, you can push it to Docker Hub.

**1. Tag Your Image:**

Before you can push your image, you need to tag it with your Docker Hub username and repository name:
```
docker tag <local-image> <dockerhub-username>/<repository-name>:<tag>
```

For example, if your Docker Hub username is "myusername" and your repository name is "myapp", you would use:
```
docker tag myapp myusername/myapp:latest
```

**2. Push the Image:**

Once your image is tagged, you can push it to Docker Hub:
```
docker push <dockerhub-username>/<repository-name>:<tag>
```

Using the previous example, you would use:
```
docker push myusername/myapp:latest
```

This command uploads your image to Docker Hub, making it available for others to pull and use.

### Useful Features of Docker Hub

**1. Automated Builds:**

Docker Hub can automatically build images from a GitHub or Bitbucket repository. This is useful for continuous integration and continuous deployment (CI/CD) workflows. You can set up automated builds in the Docker Hub repository settings.

**2. Webhooks:**

Webhooks allow you to trigger actions after a successful push or pull of an image. You can use webhooks to integrate Docker Hub with other services, such as triggering a deployment process after an image is pushed.

**3. Organizations and Teams:**

Docker Hub supports organizations and teams, making it easier to manage permissions and collaborate on projects. You can create an organization, add team members, and assign roles to control who can access and modify your images.

**4. Official Images:**

Docker Hub hosts a variety of official images that are maintained by Docker and other trusted entities. These images are generally well-documented, regularly updated, and provide a good starting point for many applications.

**5. Repositories:**

You can create multiple repositories under your Docker Hub account to organize your images. Each repository can hold multiple versions of an image, tagged with different tags (e.g., `latest`, `v1.0`, `v2.0`).

**6. Image Insights:**

Docker Hub provides insights into your images, such as the number of pulls, stars, and the last updated time. This information can help you track the popularity and usage of your images.

### GitHub Container Registry

[GitHub Container Registry](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry) is another popular option for storing and managing container images that Club Cedille uses a lot. It is tightly integrated with GitHub, making it a convenient choice for developers who already use GitHub for source code management. Here are some key features:

**1. Integration with GitHub Repositories:**

- GitHub Container Registry allows you to store container images alongside your code repositories. This integration simplifies your workflow by keeping your code and container images in one place.

**2. Access Control and Permissions:**

- You can control who has access to your container images using GitHub's existing permission model. This makes it easy to manage access for your team members.

**3. Support for Public and Private Images:**

- Similar to Docker Hub, GitHub Container Registry supports both public and private images. You can choose to share your images with the community or restrict access to specific users or teams.

**4. GitHub Actions Integration:**

- You can use GitHub Actions to automate your workflows, including building and pushing images to the GitHub Container Registry. This is useful for CI/CD pipelines, ensuring that your images are always up-to-date.

**5. Free for Public Repositories:**

- GitHub Container Registry offers free usage for public repositories, making it an attractive option for open-source projects.

### Summary

Docker Hub is a powerful platform for sharing and distributing Docker images. By leveraging Docker Hub, you can streamline your development and deployment workflows, collaborate with others, and access a vast library of container images. Whether you're pulling official images for your projects or sharing your custom images with the community, Docker Hub is an essential tool in the Docker ecosystem.

If you're interested you could also check out one of the [Cedille Club's GitHub pipeline packages](https://github.com/orgs/ClubCedille/packages). GitHub pipeline packages are a set of tools and configurations that help automate various aspects of the software development lifecycle, such as building, testing, and deploying applications. By using these packages, developers can ensure consistent and repeatable processes, improve efficiency, and reduce the chances of errors during deployment.
90 changes: 90 additions & 0 deletions wiki/docs/en/learn-docker/containerize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
When working with containers, you usually need to create a `Dockerfile` to define your image and a `compose.yaml` file to define how to run it. These files specify the instructions for building and running your application within Docker containers. Docker provides a convenient command called `docker init` to help you create these files.

## Step-by-Step Guide to Containerizing Your Application

**1. Initialize Docker in Your Project:**

Open your project folder in the terminal and run the following command:
```
docker init
```

**Explanation:** This command initiates the process of creating the necessary Docker configuration files for your project. Docker will detect the language of your project and prompt you to select a language from the list. If your language isn't listed, you can select `Other`.

**2. Answer Docker Init Questions:**

The `docker init` command will walk you through a series of questions to configure your project with sensible defaults. These questions may include:

- Selecting the base image for your application.
- Specifying the port your application will use.
- Defining any additional dependencies or environment variables.

**Explanation:** Docker uses your responses to generate a `Dockerfile` and a `compose.yaml` file tailored to your application.

**3. Understand the Generated Files:**

- **Dockerfile:**

The `Dockerfile` is a text document that contains the instructions to build your Docker image. It typically includes commands to set up the base image, install dependencies, copy application files, and specify the entry point for your application.

Example `Dockerfile`:
```dockerfile
# Use an official Node.js runtime as a parent image
FROM node:14

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json to the container
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to the container
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Define the command to run the app
CMD ["npm", "start"]
```

- **Compose File (compose.yaml):**

The `compose.yaml` file defines the services that make up your application, along with their configurations. It specifies how to build and run multiple containers as part of a single application stack.

Example `compose.yaml`:
```yaml
version: '3.8'

services:
web:
build: .
ports:
- "3000:3000"
volumes:
- .:/usr/src/app
environment:
NODE_ENV: development
```

**Explanation:** This file defines a single service (`web`) that uses the `Dockerfile` in the current directory to build the image, maps port 3000 on the host to port 3000 in the container, and mounts the current directory to `/usr/src/app` inside the container.

**4. Run Your Dockerized Application:**

Once you have answered all the questions and Docker has generated the files, you can run your application with the following command:
```
docker-compose up -d
```

**Explanation:** This command builds the Docker image and starts the containers as defined in the `compose.yaml` file. The `-d` flag runs the containers in detached mode (in the background).

**5. Customize Your Configuration Files:**

While Docker tries to create the `Dockerfile` and `compose.yaml` file with sensible defaults, there may be cases where you need to make additional changes. You can refer to the [Dockerfile reference](https://docs.docker.com/engine/reference/builder/) and [Compose file reference](https://docs.docker.com/compose/compose-file/) in the Docker documentation for more details on customizing these files.

## Summary

Using Docker to containerize your application simplifies the process of managing and deploying your software. The `docker init` command helps you get started quickly by generating the necessary configuration files. With these files, you can build and run your application in a consistent and reproducible environment, making development and deployment more efficient.
Loading

0 comments on commit 5423628

Please sign in to comment.