> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/docker/playing-a-bit-with-images-and-containers.md).

# playing a bit with images and containers

####

Docker is a tool that helps you run applications in containers—self-contained environments that include everything needed to run a piece of software (like code, libraries, dependencies). Here’s a simple guide to running, stopping, restarting, and deleting containers, as well as working with port mapping and accessing containers:

***

#### 1. **Running Containers**

Running a container means starting an instance of an image. When you run a container, Docker pulls the image (if it's not already on your system) and sets up everything needed to run your application.

**Command:**

```bash
docker run <image_name>
```

For example:

```bash
docker run ubuntu
```

* This command will start a container using the **ubuntu** image.
* If you want to interact with the container, you add `-it`:

```bash
docker run -it ubuntu
```

* `-i` means interactive (you can use the terminal inside the container), and `-t` allocates a terminal.
* This will open a terminal inside the container, and you can run commands as if you were inside a normal Linux system.

***

#### 2. **Stopping Containers**

Once a container is running, you can stop it. Stopping a container means halting its processes, but it doesn't delete it—just pauses it until you decide to start it again.

**Command:**

```bash
docker stop <container_name_or_id>
```

To find out which containers are running, you can use:

```bash
docker ps
```

It will list all active containers. You can use the container’s **ID** or **name** to stop it.

For example:

```bash
docker stop mycontainer
```

***

#### 3. **Restarting Containers**

Sometimes, you may need to restart a container (e.g., after changing configurations or settings). The `docker restart` command stops and starts the container in one step.

**Command:**

```bash
docker restart <container_name_or_id>
```

For example:

```bash
docker restart mycontainer
```

***

#### 4. **Deleting Containers**

When you're done with a container and no longer need it, you can delete it to free up resources. But you can only delete a **stopped** container.

**Command:**

```bash
docker rm <container_name_or_id>
```

Example:

```bash
docker rm mycontainer
```

This will delete the container, but it doesn't affect the image. You can always create a new container from the same image.

***

#### 5. **Port Mapping (-p 8080:80)**

Port mapping allows you to connect your containerized application to your host machine’s network. If you’re running a web server inside a container, you need to map the container’s port to a port on your computer so that you can access the web server via your browser.

**Command:**

```bash
docker run -p <host_port>:<container_port> <image_name>
```

For example:

```bash
docker run -p 8080:80 nginx
```

* This maps **port 80 inside the container** (where the Nginx web server is running) to **port 8080 on your host**.
* Now, when you open `http://localhost:8080` in your browser, it will show the Nginx web page running inside the container.

***

#### 6. **Entering a Running Container**

If you want to interact with a container that is already running, you can enter it using `docker exec`. This lets you run commands inside the container.

**Command:**

```bash
docker exec -it <container_name_or_id> bash
```

For example:

```bash
docker exec -it mycontainer bash
```

* This opens an interactive terminal inside the container where you can run commands like you’re using a normal Linux system.

***

#### Summary of Key Commands:

* **Run a container**: `docker run <image_name>`
* **Stop a container**: `docker stop <container_name_or_id>`
* **Restart a container**: `docker restart <container_name_or_id>`
* **Delete a container**: `docker rm <container_name_or_id>`
* **Map ports**: `docker run -p <host_port>:<container_port> <image_name>`
* **Enter a running container**: `docker exec -it <container_name_or_id> bash`

***

This gives you the basics to manage your Docker containers. As you dive deeper into Docker, you'll learn more about advanced topics, but these fundamental commands are the building blocks for working with containers.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/docker/playing-a-bit-with-images-and-containers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
